Tensorflow Fundamentals
import tensorflow as tf
print(tf.__version__)
scalar = tf.constant(7)
scalar
scalar.ndim
vector = tf.constant([10, 10])
vector
vector.ndim
matrix = tf.constant([[10, 7],
[7, 10]])
matrix
matrix.ndim
another_matrix = tf.constant([[10., 7.],
[3., 2.],
[8., 9.]], dtype=tf.float16)
another_matrix
another_matrix.ndim
tensor = tf.constant(
[
[
[1, 2, 3],
[4, 5, 6],
],
[
[7, 8, 9],
[10, 11, 12]
],
[
[13, 14, 15],
[16, 17, 18]
]
]
)
tensor
tensor.ndim
tf.Variable
changable_tensor = tf.Variable([10, 7])
unchangable_tensor = tf.constant([10, 7])
changable_tensor, unchangable_tensor
%%script echo "TypeError: 'ResourceVariable' object does not support item assignment"
# Lets try change one of the elements in our changable tensor
changable_tensor[0] = 7
changable_tensor
changable_tensor[0].assign(7)
changable_tensor
%%script echo "AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'assign'"
# Let's try change our un-changable tensor
unchangable_tensor[0].assign(7)
unchangable_tensor
random_1 = tf.random.Generator.from_seed(42) # Set seed for reproducibility
random_1 = random_1.normal(shape=(3, 2)) # probability is constant
random_2 = tf.random.Generator.from_seed(42)
random_2 = random_2.normal(shape=(3,2))
# Are they equal?
random_1, random_2, random_1 == random_2
not_shuffled = tf.constant(
[
[10, 7],
[3, 4],
[2, 5]
]
)
# Shuffle our non-shuffled tensor
shuffled=tf.random.shuffle(not_shuffled, seed=42, name="Shuffle")
not_shuffled.ndim, shuffled
tf.random.set_seed(42)
tf.random.shuffle(not_shuffled, seed=42)
tf.random.set_seed(54)
tf.random.shuffle(not_shuffled)
tf.random.set_seed(54) # global level random seed
tf.random.shuffle(not_shuffled, seed=32) # operation level random seed
tf.ones([10, 7])
tf.zeros(shape=(3, 4))
import numpy as np
numpy_A= np.arange(1, 25, dtype=np.int32) # create a NumPy array between 1 and 25.
numpy_A
A = tf.constant(numpy_A, shape=(2, 3, 4))
A
A.ndim
Getting information from tensors
- Shape
- Rank
- Axis or dimension
- Size
Tensor Attributes
Attribute | Meaning | Code |
---|---|---|
Shape | The length(number of elements) of each of the dimensions of a tensor | tensor.shape |
Rank | The number of tensor dimension. A scalar has rank 0, a vector has rank 1, a matrix is rank 2, a tensor has rank n. | tensor.ndim |
Axis or dimension | A particular dimension of a tensor | tensor[0], tensor[:,1] |
Size | The total number of items in the tensor | tf.size(tensor) |
rank_4_tensor = tf.zeros(shape=[2, 3, 4, 5])
rank_4_tensor, rank_4_tensor.ndim
rank_4_tensor.shape, rank_4_tensor.ndim, tf.size(rank_4_tensor)
def TensorAttributes(tensor):
tf.print("Datatypes of every element: ", tensor.dtype)
tf.print("Number of dimensions (rank): ", tensor.ndim)
tf.print("Shape of tensor: ", tensor.shape)
tf.print("Elements along the 0 axis: ", tensor.shape[0])
tf.print("Elements along the last axis: ", tensor.shape[-1])
tf.print("Total number of elements in our tensor: ", tf.size(tensor))
TensorAttributes(rank_4_tensor)
rank_4_tensor[:1, :1, :1]