数据类型
1、list
2、nd.array
无法进行GPU的计算
3、tf.Tensor
3.1可以包含int, float,double, bool, string
tf.constant(1)
tf.constant(2.)
tf.constant(1.1,dtype = tf.int32) #报错
tf.constant(2.,dtype = tf.double)
tf.constant([True,Flase])
tf.constant('hello')
with tf.device('cpu'):
a = tf.constant([1])
with tf.device('gpu'):
b = tf.range(4)
a.device #返回所在GPU还是CPU运行
aa = a.gpu() #cpu和GPU的转换
b.numpy() #将tenson装换为numpy
a.ndim
tf.rank(b)
3.2、类型转换:
isinstance(a, tf.Tensor) #判断是否为tf
tf.is_tensor(b) #推荐使用
a.dtype() #查看类型
a = np.arange(5) #int64
aa = tf.convert_to_tensor(a, dtype = tf.int32)
tf.cast(aa, dtype= tf.float32)
b = tf.constant([0,1])
tf.cast(b, dtype = tf.bool) #bool类型的转换
tf.Variable:针对可以优化的参数,如w, b
a = tf.range(5)
b = tf.Variable(a)
b.dtype #tf.int32
b.name #'Variable: 0 '
b = tf.Variable(a, name = 'input_data')
b.trainable #True
isinstance(b, tf.Tensor) #False
isinstance(b, tf.Variable) #True
tf.is_tensor(b) #True
# 因此在上述过程中,推荐使用is_tensor()
a.numpy()