You can download this code by clicking the button below.
This code is now available for download.
This function creates a random neural network model based on Keras, which accepts input shape and number of classes as parameters and returns the compiled model.
Technology Stack : Keras, Numpy
Code Type : Create a random neural network model
Code Difficulty : Intermediate
import numpy as np
from keras.layers import Input, Dense, Dropout, Flatten
from keras.models import Sequential
from keras.utils import to_categorical
def create_random_model(input_shape, num_classes):
# Initialize the model
model = Sequential()
# Add an input layer
model.add(Input(shape=input_shape))
# Add a dense layer with 128 units and ReLU activation
model.add(Dense(128, activation='relu'))
# Add a dropout layer for regularization
model.add(Dropout(0.5))
# Flatten the output of the previous layer
model.add(Flatten())
# Add a dense layer with the number of classes as units and softmax activation
model.add(Dense(num_classes, activation='softmax'))
# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
return model