Slice

In [126]:
import tensorflow as tf
x = tf.constant([[[1., 2., 3.], [4., 5.,6 ], [7. , 8.,9 ]],
                 [[10., 11.,12], [13., 14., 15], [16., 17., 18]]])
print(x)


res = tf.slice(x, [0, 1, 0], [2, 2, 2])
print("\n")
print(res)
tf.Tensor(
[[[ 1.  2.  3.]
  [ 4.  5.  6.]
  [ 7.  8.  9.]]

 [[10. 11. 12.]
  [13. 14. 15.]
  [16. 17. 18.]]], shape=(2, 3, 3), dtype=float32)


tf.Tensor(
[[[ 4.  5.]
  [ 7.  8.]]

 [[13. 14.]
  [16. 17.]]], shape=(2, 2, 2), dtype=float32)

Gather

In [99]:
x = tf.constant([3, 5, 1, 6, 8, 7])
tf.gather(x, [2])
<tf.Tensor: shape=(1,), dtype=int32, numpy=array([1])>
In [100]:
x = tf.constant([3, 5, 1, 6, 8, 7])
tf.gather(x, [0,3])
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 6])>
In [128]:
x = tf.constant([3, 5, 1, 6, 8, 7])
tf.gather(x, [[2, 0], [2, 5]]).numpy()
array([[1, 3],
       [1, 7]])
In [132]:
x = tf.constant([     [10.0, 11.0, 12.0],
                      [20.0, 21.0, 22.0],
                      [30.0, 31.0, 32.0]])
y = tf.gather(x, indices=[2], axis=1)
print(y)
tf.Tensor(
[[12.]
 [22.]
 [32.]], shape=(3, 1), dtype=float32)
In [136]:
x = tf.constant([     [10.0, 11.0, 12.0],
                      [20.0, 21.0, 22.0],
                      [30.0, 31.0, 32.0]])
y = tf.gather(x, indices=[1])
z = tf.gather(y, indices=[0], axis=1)
print(z)
tf.Tensor([[20.]], shape=(1, 1), dtype=float32)

Find Min/Max

In [139]:
x = tf.constant([
  [[1, 2], [3, 4]],
  [[1, 2], [3, 4]]
])
tf.reduce_min(x)
<tf.Tensor: shape=(), dtype=int32, numpy=1>
In [146]:
x = tf.constant([[9,2,10,4],
                 [5,6,7,8]])
print(tf.reduce_min(x))
tf.Tensor(2, shape=(), dtype=int32)
In [147]:
x = tf.constant([
  [[1, 2], [3, 4]],
  [[1, 2], [3, 4]]
])
tf.reduce_max(x)
<tf.Tensor: shape=(), dtype=int32, numpy=4>

Index

In [148]:
x = tf.constant([[9,2,10,4],[5,6,7,8]])
print(tf.math.argmin(x))
tf.Tensor([1 0 1 0], shape=(4,), dtype=int64)
In [141]:
x = tf.constant([[2, 20, 30, 3, 6],
                 [3, 11, 16, 1, 8],
                 [14, 45, 23, 5, 27]])
print(tf.math.argmin(x))
tf.Tensor([0 1 1 1 0], shape=(5,), dtype=int64)
In [145]:
x = tf.constant([[9,2,10,4],[5,6,7,8]])
print(tf.math.argmax(x))
tf.Tensor([0 1 0 1], shape=(4,), dtype=int64)

Minimum from two tensors

In [37]:
x = tf.constant([0., 0., 0., 0.])
y = tf.constant([-5., -2., 0., 3.])
tf.math.minimum(x, y)
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([-5., -2.,  0.,  0.], dtype=float32)>
In [38]:
x = tf.constant([0., 0., 0., 0.])
y = tf.constant([-5., -2., 0., 3.])
tf.math.maximum(x, y)
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([0., 0., 0., 3.], dtype=float32)>

Concatenation

In [39]:
x = [[1, 2, 3], [4, 5, 6]]
y = [[7, 8, 9], [10, 11, 12]]
tf.concat([x, y], 0)
<tf.Tensor: shape=(4, 3), dtype=int32, numpy=
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])>
In [40]:
x = [[1, 2, 3], [4, 5, 6]]
y = [[7, 8, 9], [10, 11, 12]]
tf.concat([x, y], 1)
<tf.Tensor: shape=(2, 6), dtype=int32, numpy=
array([[ 1,  2,  3,  7,  8,  9],
       [ 4,  5,  6, 10, 11, 12]])>

initialization

In [41]:
x = tf.zeros([3, 4], tf.int32)
print(x)
tf.Tensor(
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]], shape=(3, 4), dtype=int32)
In [42]:
x = tf.ones([3, 4], tf.int32)
print(x)
tf.Tensor(
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]], shape=(3, 4), dtype=int32)
In [ ]:

Loss Function

In [150]:
y_true = [0., 1.]
y_pred = [1., 1.]
# Using 'auto'/'sum_over_batch_size' reduction type.
mse = tf.keras.losses.MeanSquaredError()
mse(y_true, y_pred).numpy()
0.5
In [55]:
y_true = [[0, 1, 0], [0, 0, 1]]
y_pred = [[0.05, 0.95, 0], [0.1, 0.8, 0.1]]
# Using 'auto'/'sum_over_batch_size' reduction type.
cce = tf.keras.losses.CategoricalCrossentropy()
cce(y_true, y_pred).numpy()
1.1769392

Activation Function

In [56]:
softmax = tf.nn.softmax([-1, 0., 1.])
softmax
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0.09003057, 0.24472848, 0.66524094], dtype=float32)>
In [57]:
x = tf.constant([0.0, 1.0, 50.0, 100.0])
tf.math.sigmoid(x)
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([0.5      , 0.7310586, 1.       , 1.       ], dtype=float32)>
In [58]:
a = tf.constant([-3.0,-1.0, 0.0,1.0,3.0], dtype = tf.float32)
b = tf.keras.activations.tanh(a)
In [59]:
a = tf.constant([-20, -1.0, 0.0, 1.0, 20], dtype = tf.float32)
b = tf.keras.activations.sigmoid(a)
b.numpy()
array([2.0611537e-09, 2.6894143e-01, 5.0000000e-01, 7.3105860e-01,
       1.0000000e+00], dtype=float32)
In [69]:
inputs = tf.constant([[1.0,2,4,5],[5,4,3,2]])
outputs = tf.keras.activations.softmax(inputs)
print(outputs)
tf.Tensor(
[[0.01275478 0.03467109 0.25618666 0.6963875 ]
 [0.6439142  0.2368828  0.08714432 0.0320586 ]], shape=(2, 4), dtype=float32)
In [ ]: