Random Sequence Generator with One-Hot Encoding

  • Share this:

Code introduction


This code defines a function that generates a random sequence of a specified length and batch size, and converts these sequences to one-hot encoding format.


Technology Stack : The code uses the following packages and technologies: numpy, keras.utils

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
def random_sequence_generator(length, batch_size):
    import numpy as np
    from keras.utils import to_categorical
    
    # Generate random sequences
    sequences = np.random.randint(0, 10, size=(length, batch_size))
    
    # Convert sequences to one-hot encoding
    sequences_one_hot = to_categorical(sequences, num_classes=10)
    
    return sequences_one_hot