Tensor Creation Operations in TensorFlow are used to create tensors, which are the fundamental data structures in TensorFlow. These operations allow you to create tensors with various values, shapes, and data types. Here are some common tensor creation ops with examples and their outputs:
tf.constant: This operation creates a constant tensor with the specified value.
import tensorflow as tf
# Create a constant tensor with value 5
constant_tensor = tf.constant(5)
In this example, constant_tensor will be a scalar tensor with the value 5.
print(constant_tensor)
output
tf.Tensor(5, shape=(), dtype=int32)
tf.zeros: This operation creates a tensor filled with zeros.
import tensorflow as tf
# Create a tensor filled with zeros, shape=(2, 3), dtype=tf.float32
zeros_tensor = tf.zeros(shape=(2, 3), dtype=tf.float32)
# Print the zeros tensor
print(zeros_tensor)
output
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[0., 0., 0.],
[0., 0., 0.]], dtype=float32)>
tf.ones: This operation creates a tensor filled with ones.
import tensorflow as tf
# Create a tensor filled with ones, shape=(3, 2), dtype=tf.int32
ones_tensor = tf.ones(shape=(3, 2), dtype=tf.int32)
# Print the ones tensor
print(ones_tensor)
output
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 1],
[1, 1],
[1, 1]], dtype=int32)>
tf.fill: This operation creates a tensor filled with a specified value.
import tensorflow as tf
# Create a tensor filled with the value 7, shape=(2, 2)
filled_tensor = tf.fill(dims=(2, 2), value=7)
# Print the filled tensor
print(filled_tensor)
output
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[7, 7],
[7, 7]], dtype=int32)>
tf.linspace: This operation creates a tensor with values linearly spaced between a start and stop value.
linspace_tensor = tf.linspace(start=0.0, stop=1.0, num=5)
linspace_tensor will be a 1D tensor containing values [0.0, 0.25, 0.5, 0.75, 1.0].
tf.range: This operation creates a tensor with values within a specified range.
import tensorflow as tf
# Create a tensor with values in the range [start, limit) with a step of delta
range_tensor = tf.range(start=1, limit=10, delta=2)
# Print the range tensor
print(range_tensor)
output
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([1, 3, 5, 7, 9], dtype=int32)>
In this example, we create a tensor named range_tensor with values starting from 1 (inclusive) up to, but not including, 10, with a step of 2. The output shows the tensor as a 1D tensor containing the values [1, 3, 5, 7, 9].
tf.random: TensorFlow provides various random number generation ops, such as tf.random.normal, tf.random.uniform, and tf.random.shuffle, to create tensors with random values.
random_normal_tensor = tf.random.normal(shape=(2, 2), mean=0.0, stddev=1.0)
random_normal_tensor will be a 2x2 tensor with random values following a normal distribution with mean 0 and standard deviation 1.
These are just a few examples of tensor creation ops in TensorFlow. Depending on your needs, you can create tensors with specific shapes, data types, and values, which are essential for building and working with TensorFlow computational graphs in machine learning and deep learning applications.
Top comments (0)