Creating and Displaying Random Tensors with TensorFlow

  • Share this:

Code introduction


This code defines a function that creates a random tensor with a specified shape using TensorFlow and prints out the tensor's shape and statistical information.


Technology Stack : TensorFlow, NumPy

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import tensorflow as tf
import numpy as np

def random_tensor_shape():
    # Generate a random tensor shape for TensorFlow
    shape = [np.random.randint(1, 10), np.random.randint(1, 10)]
    return shape

def create_random_tensor(arg1, arg2):
    # Create a random tensor of specified shape using TensorFlow
    with tf.device('/cpu:0'):
        tensor = tf.random.normal(arg1, arg2)
    return tensor

def print_tensor_info(tensor):
    # Print information about the tensor
    print("Shape:", tensor.shape)
    print("Mean:", np.mean(tensor.numpy()))
    print("Standard Deviation:", np.std(tensor.numpy()))