数学运算

TensorFlow2教程完整教程目录(更有python、go、pytorch、tensorflow、爬虫、人工智能教学等着你):https://www.cnblogs.com/nickchen121/p/10840284.html

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)>
posted @ 2019-05-11 18:07  B站-水论文的程序猿  阅读(1253)  评论(0编辑  收藏  举报