目錄python
import tensorflow as tf
b = tf.fill([2, 2], 2.) a = tf.ones([2, 2])
a+b
<tf.Tensor: id=6, shape=(2, 2), dtype=float32, numpy= array([[3., 3.], [3., 3.]], dtype=float32)>
a-b
<tf.Tensor: id=8, shape=(2, 2), dtype=float32, numpy= array([[-1., -1.], [-1., -1.]], dtype=float32)>
a*b
<tf.Tensor: id=10, shape=(2, 2), dtype=float32, numpy= array([[2., 2.], [2., 2.]], dtype=float32)>
a/b
<tf.Tensor: id=12, shape=(2, 2), dtype=float32, numpy= array([[0.5, 0.5], [0.5, 0.5]], dtype=float32)>
b // a
<tf.Tensor: id=14, shape=(2, 2), dtype=float32, numpy= array([[2., 2.], [2., 2.]], dtype=float32)>
b % a
<tf.Tensor: id=16, shape=(2, 2), dtype=float32, numpy= array([[0., 0.], [0., 0.]], dtype=float32)>
a
<tf.Tensor: id=5, shape=(2, 2), dtype=float32, numpy= array([[1., 1.], [1., 1.]], dtype=float32)>
tf.math.log(a) # 只有以e爲底的log
<tf.Tensor: id=23, shape=(2, 2), dtype=float32, numpy= array([[0., 0.], [0., 0.]], dtype=float32)>
tf.exp(a)
<tf.Tensor: id=21, shape=(2, 2), dtype=float32, numpy= array([[2.7182817, 2.7182817], [2.7182817, 2.7182817]], dtype=float32)>
tf.math.log(8.)/tf.math.log(2.) # 以2爲底
<tf.Tensor: id=43, shape=(), dtype=float32, numpy=3.0>
tf.math.log(100.)/tf.math.log(10.) # 以10爲底
<tf.Tensor: id=49, shape=(), dtype=float32, numpy=2.0>
b
<tf.Tensor: id=2, shape=(2, 2), dtype=float32, numpy= array([[2., 2.], [2., 2.]], dtype=float32)>
tf.pow(b, 3)
<tf.Tensor: id=53, shape=(2, 2), dtype=float32, numpy= array([[8., 8.], [8., 8.]], dtype=float32)>
b**3
<tf.Tensor: id=56, shape=(2, 2), dtype=float32, numpy= array([[8., 8.], [8., 8.]], dtype=float32)>
tf.sqrt(b)
<tf.Tensor: id=58, shape=(2, 2), dtype=float32, numpy= array([[1.4142135, 1.4142135], [1.4142135, 1.4142135]], dtype=float32)>
a, b
(<tf.Tensor: id=5, shape=(2, 2), dtype=float32, numpy= array([[1., 1.], [1., 1.]], dtype=float32)>, <tf.Tensor: id=2, shape=(2, 2), dtype=float32, numpy= array([[2., 2.], [2., 2.]], dtype=float32)>)
a@b
<tf.Tensor: id=62, shape=(2, 2), dtype=float32, numpy= array([[4., 4.], [4., 4.]], dtype=float32)>
tf.matmul(a, b)
<tf.Tensor: id=64, shape=(2, 2), dtype=float32, numpy= array([[4., 4.], [4., 4.]], dtype=float32)>
a = tf.ones([4, 2, 3]) # 4做爲batch處理 b = tf.fill([4, 3, 5], 2.) # 4做爲batch處理
a@b
<tf.Tensor: id=72, shape=(4, 2, 5), dtype=float32, numpy= array([[[6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.]], [[6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.]], [[6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.]], [[6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.]]], dtype=float32)>
tf.matmul(a, b)
<tf.Tensor: id=74, shape=(4, 2, 5), dtype=float32, numpy= array([[[6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.]], [[6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.]], [[6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.]], [[6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.]]], dtype=float32)>
a.shape
TensorShape([4, 2, 3])
b.shape
TensorShape([4, 3, 5])
bb = tf.broadcast_to(b, [4, 3, 5])
a@bb
<tf.Tensor: id=78, shape=(4, 2, 5), dtype=float32, numpy= array([[[6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.]], [[6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.]], [[6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.]], [[6., 6., 6., 6., 6.], [6., 6., 6., 6., 6.]]], dtype=float32)>
x = tf.ones([4, 2]) W = tf.ones([2, 1]) b = tf.constant(0.1) # 自動broadcast爲[4,1] x@W + b
<tf.Tensor: id=88, shape=(4, 1), dtype=float32, numpy= array([[2.1], [2.1], [2.1], [2.1]], dtype=float32)>
out = x@W + b tf.nn.relu(out)
<tf.Tensor: id=95, shape=(4, 1), dtype=float32, numpy= array([[2.1], [2.1], [2.1], [2.1]], dtype=float32)>