Debug School

rakesh kumar
rakesh kumar

Posted on

list out the checklist of Tensor Creation Ops in tensor flow

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)
Enter fullscreen mode Exit fullscreen mode

output

tf.Tensor(5, shape=(), dtype=int32)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

output

<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[0., 0., 0.],
       [0., 0., 0.]], dtype=float32)>
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

output

<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 1],
       [1, 1],
       [1, 1]], dtype=int32)>
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

output

<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[7, 7],
       [7, 7]], dtype=int32)>
Enter fullscreen mode Exit fullscreen mode

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].
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

output

<tf.Tensor: shape=(5,), dtype=int32, numpy=array([1, 3, 5, 7, 9], dtype=int32)>
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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.

One dimensional Tensor

Image description

Image description

Two dimensional Tensors

Image description

Image description

Top comments (0)