数学运算
▪ (+) 加 (-) 减(*)乘( /)除
▪ (**)次方, (pow)次方, (square )平方
▪( sqrt)平方根
▪ (//)整除, (%)取余
▪ exp,( log )取对数
▪ @, matmul 矩阵相乘
▪ linear layer
运算类型
- 元素运算 + - * /
- 矩阵运算 @, matmul
- 维度运算 reduce_mean 、max、min、sum
+ -*/%//
import tensorflow as tf
b = tf.fill([2,2],2.)
a = tf.ones([2,2])
print("a : ", a)
print("b :", b)
print("a + b: ", a + b)
print("a - b: ", a - b)
print("a / b: ", a / b)
print("a * b: ", a * b)
print("b // a: ", b // a)
print("b % a: ",b % a)
tf.math.log / tf.exp
import tensorflow as tf
b = tf.fill([2,2],2.)
a = tf.ones([2,2])
print("a : ", a)
print("b :", b)
# print("a + b: ", a + b)
# print("a - b: ", a - b)
# print("a / b: ", a / b)
# print("a * b: ", a * b)
#
# print("b // a: ", b // a)
# print("b % a: ",b % a)
print("log(a):",tf.math.log(a))
#这里的log是以log10为底的
print("exp(a)",tf.exp(a))
补充:怎样才能有log2
print(“log(a):”,tf.math.log(b)/tf.math.log(2.))
pow, sqrt
# pow, sqrt
print("pow(b,3) b的3次方:",tf.pow(b, 3))
print("b**3 b的3次方:",b**3)
print("tf.sqrt(b): ",tf.sqrt(b))
@ matmul 矩阵的相乘
a1 = tf.ones([4,2,3])
a2 = tf.fill([4,3,5],2.)
print("a1.shape:",a1.shape)
print("a2.shape:",a2.shape)
print("(a1 @ a2).shape:",(a1 @ a2).shape)
print("(tf.matmul(a1,a2)).shape : ",(tf.matmul(a1,a2)).shape)
补充:如何计算矩阵
y[0][0] = x[0][0]*w[0][0] + x[0][1]*w[1][0] + b[0]
y[1][0] = x[0][0]*w[0][1] + x[0][1]*w[1][1] + b[1]
......
y[0][1] = x[0][1]*w[0][0] + x[1][1]*w[1][0] + b[0]
......