在 Python 中,可以使用内置函数 type()
来查看某个变量的数据类型。例如:
# 定义一个整型变量
a = 5
# 查看变量 a 的数据类型
print(type(a))
输出结果为:
<class 'int'>
-----------------------------------------------------------------------------------------------------------------------
在 TensorFlow 中,可以使用 tf.Tensor
类型对象的 dtype
和 shape
属性来查看张量的类型和形状。下面是一个例子:
import tensorflow as tf
# 创建一个张量
x = tf.constant([1, 2, 3, 4])
# 打印张量的类型和形状
print("Type:", x.dtype)
print("Shape:", x.shape)
上面的代码将输出以下结果:
Type: <dtype: 'int32'>
Shape: (4,)
-------------------------------------------------------------------------------------------------------------------
pytorch:
查看形状 torch.size()