Learn Basic Operations in TensorFlow2 in One Hour

https://iamarookie.blog.csdn.net/article/details/117755839

Basic Operations in TensorFlow2 – Part 3

Merge and Split

Learn Basic Operations in TensorFlow2 in One Hour

tf.concat

tf.concat helps us perform concatenation operations.

Format:

tf.concat(
    values, axis, name='concat'
)

Parameters:

  • values: a tensor or tensor list – axis: the dimension to operate on – name: the name of the operation, defaults to “concat”
    Example:
part_1 = tf.zeros([5, 3])
print(part_1)

part_2 = tf.ones([5, 3])
print(part_2)

# Vertical concatenation
result_1 = tf.concat([part_1, part_2], axis=0)
print(result_1)

# Horizontal concatenation
result_2 = tf.concat([part_1, part_2], axis=1)
print(result_2)

Output:

tf.Tensor(
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]], shape=(10, 3), dtype=float32)
tf.Tensor(
[[0. 0. 0. 1. 1. 1.]
 [0. 0. 0. 1. 1. 1.]
 [0. 0. 0. 1. 1. 1.]
 [0. 0. 0. 1. 1. 1.]
 [0. 0. 0. 1. 1. 1.]], shape=(5, 6), dtype=float32)

tf.stack

tf.stack can create a new dimension to merge two tensors.

Learn Basic Operations in TensorFlow2 in One Hour

Format:

tf.stack(
    values, axis=0, name='stack'
)

Parameters:

  • values: a tensor list – axis: the dimension to operate on – name: the name of the operation, defaults to “stack”
    Example:
part_1 = tf.zeros([5, 3])
print(part_1)

part_2 = tf.ones([5, 3])
print(part_2)

# Head stacking
result_1 = tf.stack([part_1, part_2], axis=0)
print(result_1)

# Tail stacking
result_2 = tf.stack([part_1, part_2], axis=2)
print(result_2)

Output:

tf.Tensor(
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[[0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]], shape=(2, 5, 3), dtype=float32)
tf.Tensor(
[[[0. 1.]
  [0. 1.]
  [0. 1.]]

 [[0. 1.]
  [0. 1.]
  [0. 1.]]

 [[0. 1.]
  [0. 1.]
  [0. 1.]]

 [[0. 1.]
  [0. 1.]
  [0. 1.]]

 [[0. 1.]
  [0. 1.]
  [0. 1.]]], shape=(5, 3, 2), dtype=float32)

tf.unstack

tf.unstack is a matrix decomposition function. Format:

# unstack
tf.unstack(
    value, num=None, axis=0, name='unstack'
)

Parameters:

  • values: a tensor with dimension greater than 0 – num: the length of the axis – axis: the dimension to operate on – name: the name of the operation, defaults to “unstack”
    Example:
a = tf.stack([tf.zeros([5, 3]), tf.ones([5, 3])], axis=0)
print(a)

b = tf.unstack(a, axis=0)
print(b)

Output:

tf.Tensor(
[[[0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]]

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

tf.split

tf.split() can divide a tensor into several sub-tensors.

Learn Basic Operations in TensorFlow2 in One Hour

Format:

tf.split(
    value, num_or_size_splits, axis=0, num=None, name='split'
)

Parameters:

  • value: the tensor to be split – num_or_size_splits: how many parts to split into – axis: the dimension to operate on – num: used when num_or_size_splits cannot be achieved – name: the name of the operation, defaults to “split”
    Example:
# split
a = tf.stack([tf.zeros([5, 3]), tf.ones([5, 3])], axis=0)
print(a)

b = tf.split(a, 2)
print(b)

Output:

tf.Tensor(
[[[0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]]

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

Data Statistics

Learn Basic Operations in TensorFlow2 in One Hour

tf.norm

tf.norm helps us calculate the norm of vectors, matrices, and tensors.

Format:

tf.norm(
    tensor, ord='euclidean', axis=None, keepdims=None, name=None
)

Parameters:

  • tensor: the input tensor – ord: the order of the norm – axis: the dimension to operate on – keep_dims: if True, the axes specified in axis will be kept with size 1 – name: the name of the operation
    Example:
a = tf.fill([2, 2], 2.0)
print(a)

# sqrt(2^2 * 4) = sqrt(16) = 4
b = tf.norm(a)
print(b)

# [2 + 2, 2 + 2] = [4, 4]
c = tf.norm(a, ord=1, axis= 0)
print(c)

# [sqrt(2^2 + 2^2), sqrt(2^2 + 2^2)] = [sqrt(8), sqrt(8)]
d = tf.norm(a, ord=2, axis= 0)
print(d)

Output:

tf.Tensor(
[[2. 2.]
 [2. 2.]], shape=(2, 2), dtype=float32)
tf.Tensor(4.0, shape=(), dtype=float32)
tf.Tensor([4. 4.], shape=(2,), dtype=float32)
tf.Tensor([2.828427 2.828427], shape=(2,), dtype=float32)

reduce_min/max/mean

Calculates the minimum / maximum / average of elements across dimensions of a tensor.

Format:

tf.math.reduce_min / reduce_max / reduce_mean(
    input_tensor, axis=None, keepdims=False, name=None
)

Parameters:

  • input_tensor: the input tensor – axis: the dimension, defaults to calculating across all dimensions – keepdims: if true, keep dimensions, defaults to False – name: the name of the operation
    Example:
a = tf.reshape(tf.range(9), [3, 3])
print(a)

min = tf.reduce_min(a)
print(min)

max = tf.reduce_max(a)
print(max)

Output:

tf.Tensor(
[[0 1 2]
 [3 4 5]
 [6 7 8]], shape=(3, 3), dtype=int32)
tf.Tensor(0, shape=(), dtype=int32)
tf.Tensor(8, shape=(), dtype=int32)

argmax / argmin

tf.argmax/tf.argmin help us find the index of the maximum / minimum value.

Format:

tf.math.argmax(
    input, axis=None, output_type=tf.dtypes.int64, name=None
)

Parameters:

  • input: the input tensor – axis: the dimension to operate on – output_type: the output data type, defaults to int64 – name: the name of the operation
    Example:
# argmax / argmin
a = tf.reshape(tf.range(9), [3, 3])
print(a)

max = tf.argmax(a)
print(max)

min = tf.argmin(a)
print(min)

Output:

tf.Tensor(
[[0 1 2]
 [3 4 5]
 [6 7 8]], shape=(3, 3), dtype=int32)
tf.Tensor([2 2 2], shape=(3,), dtype=int64)
tf.Tensor([0 0 0], shape=(3,), dtype=int64)

tf.equal

tf.equal helps us determine if two tensors are equal. Returns True / False.

Learn Basic Operations in TensorFlow2 in One Hour

Format:

tf.math.equal(
    x, y, name=None
)

Example:

a = tf.zeros(5, dtype=tf.float32)
print(a)

b = tf.range(5, dtype=tf.float32)
print(b)

print(tf.equal(a, b))

Output:

tf.Tensor([0. 0. 0. 0. 0.], shape=(5,), dtype=float32)
tf.Tensor([0. 1. 2. 3. 4.], shape=(5,), dtype=float32)
tf.Tensor([ True False False False False], shape=(5,), dtype=bool)

tf.unique

tf.unique helps us find unique values in a tensor.

Format:

tf.unique(
    x, out_idx=tf.dtypes.int32, name=None
)

Parameters:

  • input: the input tensor – output_type: the output data type, defaults to int32 – name: the name of the operation
    Example:
a = tf.range(5)
print(tf.unique(a))

b = tf.constant([4, 2, 2, 4, 3])
print(tf.unique(b))

Output:

Unique(y=<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>, idx=<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>)
Unique(y=<tf.Tensor: shape=(3,), dtype=int32, numpy=array([4, 2, 3])>, idx=<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 1, 0, 2])>)

Learn Basic Operations in TensorFlow2 in One Hour

Learn Basic Operations in TensorFlow2 in One Hour

Learn Basic Operations in TensorFlow2 in One Hour

Leave a Comment