tf.math

Reason is the light and the light of life.

Jerry Su Nov 23, 2020 4 mins
  • +, -, *, /

  • **, pow, square

  • sqrt

  • //, %

  • exp, log

  • @, matmul

  • linear layer

element-wise

  • +, -, *, /

matrix-wise

  • @, matmul

dim-wise

  • reduce_mean/max/min/sum
import tensorflow as tf

1. element-wise

a = tf.ones([2, 2])
b = tf.fill([2, 2], 2.)
a, b
(<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, a - b, a * b, a / b
(<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
(<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)>)
tf.math.log(a)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[0., 0.],
       [0., 0.]], dtype=float32)>
tf.exp(a)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[2.7182817, 2.7182817],
       [2.7182817, 2.7182817]], dtype=float32)>
tf.pow(b, 3)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[8., 8.],
       [8., 8.]], dtype=float32)>
b**3
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[8., 8.],
       [8., 8.]], dtype=float32)>
tf.sqrt(b)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1.4142135, 1.4142135],
       [1.4142135, 1.4142135]], dtype=float32)>

2. matrix-wise

  • ( b, 2, 3 ) @ ( b, 3, 5 ) = ( b, 2, 5 ) , b is batch_size
a@b
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[4., 4.],
       [4., 4.]], dtype=float32)>
tf.matmul(a, b)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[4., 4.],
       [4., 4.]], dtype=float32)>
c = tf.ones([4, 2, 3])
d = tf.fill([4, 3, 5], 2.)
c, d
(<tf.Tensor: shape=(4, 2, 3), dtype=float32, numpy=
 array([[[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]]], dtype=float32)>,
 <tf.Tensor: shape=(4, 3, 5), dtype=float32, numpy=
 array([[[2., 2., 2., 2., 2.],
         [2., 2., 2., 2., 2.],
         [2., 2., 2., 2., 2.]],

        [[2., 2., 2., 2., 2.],
         [2., 2., 2., 2., 2.],
         [2., 2., 2., 2., 2.]],

        [[2., 2., 2., 2., 2.],
         [2., 2., 2., 2., 2.],
         [2., 2., 2., 2., 2.]],

        [[2., 2., 2., 2., 2.],
         [2., 2., 2., 2., 2.],
         [2., 2., 2., 2., 2.]]], dtype=float32)>)
c@d
<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)>
# with broadcasting
e = tf.ones([4, 2, 3])
f = tf.fill([3, 5], 2.)
e.shape, f.shape
(TensorShape([4, 2, 3]), TensorShape([3, 5]))
ff = tf.broadcast_to(f, [4, 3, 5])
ff.shape
TensorShape([4, 3, 5])
e@ff
<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)>

3. dim-wise

!pip install torch
Collecting torch
  Downloading torch-1.7.0-cp38-cp38-manylinux1_x86_64.whl (776.8 MB)
     |██████▉                         | 165.9 MB 8.3 MB/s eta 0:01:147  |                                | 2.4 MB 567 kB/s eta 0:22:45     |                               | 4.6 MB 4.1 MB/s eta 0:03:09     |                               | 8.4 MB 4.1 MB/s eta 0:03:08     |                               | 9.6 MB 4.1 MB/s eta 0:03:08     |                               | 10.8 MB 7.2 MB/s eta 0:01:47     |                               | 11.3 MB 7.2 MB/s eta 0:01:46     |                               | 13.6 MB 7.2 MB/s eta 0:01:46     |                               | 14.6 MB 7.2 MB/s eta 0:01:46     |                               | 16.8 MB 7.2 MB/s eta 0:01:46     |                               | 18.1 MB 7.2 MB/s eta 0:01:46     |                               | 20.1 MB 7.2 MB/s eta 0:01:45     |                               | 22.6 MB 12.7 MB/s eta 0:01:00     |                               | 23.1 MB 12.7 MB/s eta 0:01:00     |                               | 24.8 MB 12.7 MB/s eta 0:01:00     |█▍                              | 33.4 MB 12.7 MB/s eta 0:00:59     |█▋                              | 40.2 MB 18.1 MB/s eta 0:00:41     |██▍                             | 57.8 MB 41.0 MB/s eta 0:00:18     |██▊                             | 65.2 MB 41.0 MB/s eta 0:00:18     |██▊                             | 66.2 MB 41.0 MB/s eta 0:00:18     |███                             | 73.9 MB 20.0 MB/s eta 0:00:36     |███▌                            | 83.7 MB 7.2 MB/s eta 0:01:37     |███▌                            | 85.1 MB 7.2 MB/s eta 0:01:37     |███▊                            | 89.2 MB 7.2 MB/s eta 0:01:36     |███▉                            | 93.6 MB 7.2 MB/s eta 0:01:36     |███▉                            | 94.3 MB 13.9 MB/s eta 0:00:49     |████▏                           | 100.3 MB 13.9 MB/s eta 0:00:49     |████▎                           | 104.7 MB 13.9 MB/s eta 0:00:49     |████▍                           | 105.5 MB 13.9 MB/s eta 0:00:49     |████▌                           | 110.3 MB 13.0 MB/s eta 0:00:52     |████▊                           | 113.3 MB 13.0 MB/s eta 0:00:51     |█████                           | 120.0 MB 13.0 MB/s eta 0:00:51     |█████                           | 122.0 MB 12.1 MB/s eta 0:00:55     |█████                           | 122.7 MB 12.1 MB/s eta 0:00:55     |█████▎                          | 127.2 MB 12.1 MB/s eta 0:00:54     |█████▋                          | 137.0 MB 17.5 MB/s eta 0:00:37     |█████▊                          | 137.8 MB 17.5 MB/s eta 0:00:37     |██████▏                         | 148.9 MB 7.1 MB/s eta 0:01:30     |██████▏                         | 149.4 MB 7.1 MB/s eta 0:01:29     |██████▍                         | 154.0 MB 10.8 MB/s eta 0:00:58     |██████▍                         | 156.0 MB 10.8 MB/s eta 0:00:58     |██████▌                         | 158.4 MB 10.8 MB/s eta 0:00:58     |██████▋                         | 160.9 MB 10.8 MB/s eta 0:00:58     |██████▊                         | 163.9 MB 10.8 MB/s eta 0:00:57     |██████▉                         | 164.6 MB 10.8 MB/s eta 0:00:57     |██████▉                         | 165.2 MB 8.3 MB/s eta 0:01:14


Read more:

Related posts: