TensorFlow and NumPy Random Matrix Generator

  • Share this:

Code introduction


This function generates a random matrix of specified number of rows and columns using TensorFlow and NumPy.


Technology Stack : TensorFlow, NumPy

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import tensorflow as tf
import numpy as np

def generate_random_matrix(rows, cols):
    """
    Generate a random matrix using TensorFlow and NumPy.
    """
    # Define the random matrix using TensorFlow's random_normal function
    matrix = tf.random.normal([rows, cols])
    
    # Convert the TensorFlow tensor to a NumPy array
    numpy_matrix = matrix.numpy()
    
    return numpy_matrix