Load Movie Review Text Data

# Set the number of features we wantnumber_of_features=1000# Load data and target vector from movie review data(train_data,train_target),(test_data,test_target)=imdb.load_data(num_words=number_of_features)# Convert movie review data to a one-hot encoded feature matrixtokenizer=Tokenizer(num_words=number_of_features)train_features=tokenizer.sequences_to_matrix(train_data,mode='binary')test_features=tokenizer.sequences_to_matrix(test_data,mode='binary')

Create Neural Network Architecture With Weight Regularization

In Keras, we can add a weight regularization by including using including kernel_regularizer=regularizers.l2(0.01) a later. In this example, 0.01 determines how much we penalize higher parameter values.

Train Neural Network

# Train neural networkhistory=network.fit(train_features,# Featurestrain_target,# Target vectorepochs=3,# Number of epochsverbose=0,# No outputbatch_size=100,# Number of observations per batchvalidation_data=(test_features,test_target))# Data for evaluation