标签:was expec sub tee cannot col error: input type
(1)不是同类型的不能运算
1 a = tf.fill([2,2],2) #创建的是 dtype=int32 2 print(a) 3 4 b = tf.ones([2,2]) #创建的是dtype=float32 5 print(b) 6 7 print(a+b)
cannot compute AddV2 as input #1(zero-based) was expected to be a int32 tensor but is a float tensor [Op:AddV2] name: add/
(2)同类型计算
a = tf.fill([2,2],2.) print("a:\n",a) b = tf.ones([2,2]) print("b: \n",b) print("a+b:\n",a+b) print("a-b:\n",a-b) print("a*b:\n",a*b) print("a/b:\n",a/b)
输出:
a: tf.Tensor( [[2. 2.] [2. 2.]], shape=(2, 2), dtype=float32) b: tf.Tensor( [[1. 1.] [1. 1.]], shape=(2, 2), dtype=float32) a+b: tf.Tensor( [[3. 3.] [3. 3.]], shape=(2, 2), dtype=float32) a-b: tf.Tensor( [[1. 1.] [1. 1.]], shape=(2, 2), dtype=float32) a*b: tf.Tensor( [[2. 2.] [2. 2.]], shape=(2, 2), dtype=float32) a/b: tf.Tensor( [[2. 2.] [2. 2.]], shape=(2, 2), dtype=float32)
在TF中只有以e为底的对数,要想计算其他底数的对数可以使用除法公式,logab = logeb / logea
1 a = tf.ones([2,2]) 2 # b1 = tf.log(a) # AttributeError: module ‘tensorflow‘ has no attribute ‘log‘ 3 b1 = tf.math.log(a) 4 print(b1) 5 6 b2 = tf.exp(a) #e的a次方 7 print(b2)
输出:
tf.Tensor( [[0. 0.] [0. 0.]], shape=(2, 2), dtype=float32)
tf.Tensor( [[2.7182817 2.7182817] [2.7182817 2.7182817]], shape=(2, 2), dtype=float32)
1 b = tf.fill([2,2],2.) #如果不设成浮点型,计算sqrt的时候会出现问题 2 print(b) 3 4 b1 = tf.pow(b,3) # b的3次方 5 print(b1) 6 7 b2 = b**3 # b的三次方 8 print(b2) 9 10 b3 = tf.sqrt(b) #b的开方,即b的0.5次方 11 print(b3)
输出:
tf.Tensor( [[2. 2.] [2. 2.]], shape=(2, 2), dtype=float32)
tf.Tensor( [[8. 8.] [8. 8.]], shape=(2, 2), dtype=float32)
tf.Tensor( [[8. 8.] [8. 8.]], shape=(2, 2), dtype=float32)
tf.Tensor( [[1.4142135 1.4142135] [1.4142135 1.4142135]], shape=(2, 2), dtype=float32)
标签:was expec sub tee cannot col error: input type
原文地址:https://www.cnblogs.com/pengzhonglian/p/11908756.html