1、tensor的加减乘除求余取整(+-*/%//)
注,这里的乘(*)是矩阵的对应元素相乘
一个/是数学中除法,两个//是取整
b = tf.fill([2, 2], 2.)
a = tf.ones([2, 2])
a + b, a - b, a* b, a/b
Out[137]:
(<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[3., 3.],
[3., 3.]], dtype=float32)>,
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[-1., -1.],
[-1., -1.]], dtype=float32)>,
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[2., 2.],
[2., 2.]], dtype=float32)>,
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[0.5, 0.5],
[0.5, 0.5]], dtype=float32)>)
b//a, b%a
Out[138]:
(<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[2., 2.],
[2., 2.]], dtype=float32)>,
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[0., 0.],
[0., 0.]], dtype=float32)>)
2、求对数和指数(tf.math.log、tf.exp)
注: (其中
e
e
e为无理数,其值大约等于
2.718281828...
2.718281828...
2.718281828...)
t
f
.
m
a
t
h
.
l
o
g
(
a
)
=
l
o
g
e
(
a
)
tf.math.log(a) = log_e(a)
tf.math.log(a)=loge(a)
t
f
.
e
x
p
(
a
)
=
e
a
tf.exp(a) = e^a
tf.exp(a)=ea
a
Out[139]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1., 1.],
[1., 1.]], dtype=float32)>
tf.math.log(a)
Out[140]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[0., 0.],
[0., 0.]], dtype=float32)>
tf.exp(a)
Out[141]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[2.7182817, 2.7182817],
[2.7182817, 2.7182817]], dtype=float32)>
注,不能实现 l o g 10 ( a ) log_{10}(a) log10(a) 或者 l o g 2 ( a ) log_2(a) log2(a) ,但是可以等于:
l o g 10 ( a ) = l o g e a / l o g e 10 log_{10}(a) = log_ea/log_e10 log10(a)=logea/loge10 l o g 2 ( a ) = l o g e a / l o g e 2 log_{2}(a) = log_ea/log_e2 log2(a)=logea/loge2
tf.math.log(8.)/tf.math.log(2.)
Out[143]: <tf.Tensor: shape=(), dtype=float32, numpy=3.0>
tf.math.log(100.)/tf.math.log(10.)
Out[144]: <tf.Tensor: shape=(), dtype=float32, numpy=2.0>
3、平方和开方(tf.pow()或 ** 和tf.sqrt())
b
Out[145]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[2., 2.],
[2., 2.]], dtype=float32)>
tf.pow(b, 3)
Out[146]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[8., 8.],
[8., 8.]], dtype=float32)>
b**3
Out[147]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[8., 8.],
[8., 8.]], dtype=float32)>
tf.sqrt(b)
Out[148]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1.4142135, 1.4142135],
[1.4142135, 1.4142135]], dtype=float32)>
4、矩阵乘 @ 或 tf.matmul
@ 或 tf.matmul - 矩阵相乘,前一个矩阵的列和后一行矩阵的行要相等
a, b
Out[149]:
(<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1., 1.],
[1., 1.]], dtype=float32)>,
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[2., 2.],
[2., 2.]], dtype=float32)>)
a @ b
Out[150]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[4., 4.],
[4., 4.]], dtype=float32)>
tf.matmul(a, b)
Out[151]:
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[4., 4.],
[4., 4.]], dtype=float32)>
注,多维情况下也可以进行矩阵乘运算
例如,有两个矩阵,他们的维度分别是[4, 2, 3],[4, 3, 5],多维的矩阵乘一般只把最后面两个维度进行相乘,即在这里,只进行4个[2, 3]@[3, 5], 最后结果维度为[4, 2, 5]。
a = tf.ones([4, 2, 3])
b = tf.fill([4, 3, 5], 2.)
a@ b
Out[154]:
<tf.Tensor: 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)
Out[155]:
<tf.Tensor: 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)>
5、用broadcasting进行矩阵乘
a = tf.fill([4, 2, 3], 2.)
b = tf.ones([3, 5])
a.shape, b.shape
Out[161]: (TensorShape([4, 2, 3]), TensorShape([3, 5]))
bb = tf.broadcast_to(b, [4, 3, 5])
a@ bb
Out[168]:
<tf.Tensor: 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)>