常用的数学运算有:
- 加减乘除
- 平方,开方,幂方
- 平方根
- 地板除,取余
- 次幂,取对数
- 矩阵乘
运算类型:
- element-wise (元素相关)
- + - * \ - matrix-wise (矩阵相关)
- @ matmul - dim-wise (维度相关)
- reduce_mean/max/min/sum
+ - * / // %
a = tf.fill([2,2], 3.)
b = tf.fill([2,2], 2.)
a+b, a-b, a*b, a/b
# (<tf.Tensor: id=26, shape=(2, 2), dtype=int32, numpy=
# array([[5, 5],
# [5, 5]])>, <tf.Tensor: id=27, shape=(2, 2), dtype=int32, numpy=
# array([[1, 1],
# [1, 1]])>, <tf.Tensor: id=28, shape=(2, 2), dtype=int32, numpy=
# array([[6, 6],
# [6, 6]])>, <tf.Tensor: id=31, shape=(2, 2), dtype=float64, numpy=
# array([[1.5, 1.5],
# [1.5, 1.5]])>)
a//b, a%b
# (<tf.Tensor: id=12, shape=(2, 2), dtype=int32, numpy=
# array([[1, 1],
# [1, 1]])>, <tf.Tensor: id=13, shape=(2, 2), dtype=int32, numpy=
# array([[1, 1],
# [1, 1]])>)
tf.math.log && tf.exp
tensorflow 2.0 alpha 中 tf.math.log && tf.exp
log 是以自然对数为底。
a = tf.fill([2,2], 3.)
# 指数对数
tf.math.log(a)
# <tf.Tensor: id=118, shape=(2, 2), dtype=float32, numpy=
# array([[1.0986123, 1.0986123],
# [1.0986123, 1.0986123]], dtype=float32)>
tf.exp(a)
# <tf.Tensor: id=164, shape=(2, 2), dtype=float32, numpy=
# array([[20.085537, 20.085537],
# [20.085537, 20.085537]], dtype=float32)>
log 但是 没有以其他 为底的 API
实现 以2,10 为底:
l
o
g
e
b
l
o
g
e
a
=
l
o
g
a
b
\frac{log_eb}{log_ea}=log_ab
logealogeb=logab
l
o
g
2
x
=
l
o
g
x
l
o
g
2
log_2x = \frac{log x}{log 2}
log2x=log2logx
tf.math.log(8.)/tf.math.log(2.)
# <tf.Tensor: id=304, shape=(), dtype=float32, numpy=3.0>
l o g 10 x = l o g x l o g 10 log_{10}x = \frac{log x}{log 10} log10x=log10logx
tf.math.log(1000.)/tf.math.log(10.)
# <tf.Tensor: id=407, shape=(), dtype=float32, numpy=3.0>
pow , sqrt
tf.pow(b, 3)
# <tf.Tensor: id=514, shape=(2, 2), dtype=float32, numpy=
# array([[8., 8.],
# [8., 8.]], dtype=float32)>
b**3
# <tf.Tensor: id=573, shape=(2, 2), dtype=float32, numpy=
# array([[8., 8.],
# [8., 8.]], dtype=float32)>
tf.sqrt(b)
# <tf.Tensor: id=634, shape=(2, 2), dtype=float32, numpy=
# array([[1.4142135, 1.4142135],
# [1.4142135, 1.4142135]], dtype=float32)>
@ tf.matmul
a,b
# (<tf.Tensor: id=64, shape=(2, 2), dtype=float32, numpy=
# array([[3., 3.],
# [3., 3.]], dtype=float32)>,
# <tf.Tensor: id=67, shape=(2, 2), dtype=float32, numpy=
# array([[2., 2.],
# [2., 2.]], dtype=float32)>)
tf.matmul(a,b)
# <tf.Tensor: id=925, shape=(2, 2), dtype=float32, numpy=
# array([[12., 12.],
# [12., 12.]], dtype=float32)>
a @ b
# <tf.Tensor: id=1008, shape=(2, 2), dtype=float32, numpy=
# array([[12., 12.],
# [12., 12.]], dtype=float32)>
高维 tensor 低两维作矩阵运算
a = tf.ones([4,1,2])
b = tf.fill([4,2,2], 2.)
(a@b).shape # TensorShape([4, 1, 2])
需 手动 broadcasting
a = tf.ones([4,5,6])
b = tf.fill([6,4], 2.)
bb = tf.broadcast_to(b, [4,6,4])
(a@bb).shape # TensorShape([4, 5, 4])
liner layer
为什么要写成矩阵形式 而非标量形式?
并行加速 且符合其数学逻辑
- y = w ∗ x + b y = w*x + b y=w∗x+b
-
Y
=
W
@
X
+
b
Y = W@X +b
Y=W@X+b
- [b, 2] → [b, 3]
Y = X @ W + b Y = X@W + b Y=X@W+b
x = tf.ones([4, 2])
W = tf.ones([2, 1])
b = tf.constant(0.1)
Y = x@W + b
# <tf.Tensor: id=1765, shape=(4, 1), dtype=float32, numpy=
# array([[2.1],
# [2.1],
# [2.1],
# [2.1]], dtype=float32)>
o u t = r e l u ( X @ W + b ) out = relu(X@W + b) out=relu(X@W+b)
out = x@W + b
out = tf.nn.relu(out)
# <tf.Tensor: id=1953, shape=(4, 1), dtype=float32, numpy=
# array([[2.1],
# [2.1],
# [2.1],
# [2.1]], dtype=float32)>