在tensorflow中所有的数据都通过张量的形式表现
可以简单理解为张量就是多维数组
张量不保存数值只是保存这个数字的计算结果
# tf.constant是计算常数的方法,计算结果是一个张量,保存在变量之中
a = tf.constant([1.0,2.0],name='a')
b = tf.constant([2.0,3.0],name='b')
# tf.add是计算加和的方法
result = tf.add(a,b,name='add')
print(result)
'''
输出:
Tensor("add:0", shape=(2,), dtype=float32)
'''
tensorflow中的张量和numpy中的数组不一样
张量保存的是一个结构:名字,纬度,类型
# 数据类型一定要保持一致,tensorflow是没有将int自动转成float来计算的方法
# tf.constant是计算常数的方法,计算结果是一个张量,保存在变量之中
a = tf.constant([1.0,2.0],name='a')
b = tf.constant([2,3],name='b')
# tf.add是计算加和的方法
result = a+b
'''
输出:
ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int32: 'Tensor("b_1:0", shape=(2,), dtype=int32)'
'''
tensorflow支持14种不同的数据类型
实数:tf.float32, tf.float64
整数:tf.int8, tf.int16, tf.int32, tf.int64, tf.uint8
布尔型:tf.bool
复数:tf.complex64, tf.complex128