吴裕雄--天生自然TensorFlow2教程:数学运算

import tensorflow as tf

b = tf.fill([2, 2], 2.)
a = tf.ones([2, 2])
a+b
a-b
a*b
a/b
b // a
b % a
tf.math.log(a)  # 只有以e为底的log
tf.exp(a)
tf.math.log(8.)/tf.math.log(2.)  # 以2为底
tf.math.log(100.)/tf.math.log(10.)  # 以10为底
tf.pow(b, 3)
b**3
tf.sqrt(b)
a@b
tf.matmul(a, b)
a = tf.ones([4, 2, 3])  # 4作为batch处理
b = tf.fill([4, 3, 5], 2.)  # 4作为batch处理
a@b
tf.matmul(a, b)
bb = tf.broadcast_to(b, [4, 3, 5])
a@bb
x = tf.ones([4, 2])
W = tf.ones([2, 1])
b = tf.constant(0.1)  # 自动broadcast为[4,1]

x@W + b
out = x@W + b
tf.nn.relu(out)

猜你喜欢

转载自www.cnblogs.com/tszr/p/12124045.html