目录

* Outline <https://www.cnblogs.com/nickchen121/p/10849481.html#outline>
* Operation type
<https://www.cnblogs.com/nickchen121/p/10849481.html#operation-type>
* +-*/%// <https://www.cnblogs.com/nickchen121/p/10849481.html#section>
* tf.math.log, tf.exp
<https://www.cnblogs.com/nickchen121/p/10849481.html#tf.math.log-tf.exp>
* pow, sqrt <https://www.cnblogs.com/nickchen121/p/10849481.html#pow-sqrt>
* @, matmul <https://www.cnblogs.com/nickchen121/p/10849481.html#matmul>
* With broadcasting
<https://www.cnblogs.com/nickchen121/p/10849481.html#with-broadcasting>
* Y = X@W +b <https://www.cnblogs.com/nickchen121/p/10849481.html#y-xw-b>
Outline

* +-*/
* **,pow,square
* sqrt
* //,%
* exp,log
* @,matmul
* linear layer
Operation type

* element-wise
* +-*/
* matrix-wise
* @,matmul
* [b,3,4]@[b,4,5] = b([3,4][4,5])=[b,3,5]
* dim-wise
* reduce_mean/max/min/sum
+-*/%//
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)>
tf.math.log, tf.exp
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>
pow, sqrt
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)>
@, matmul
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)>
With broadcasting
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)>
Y = X@W +b
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)>

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信