Assignment Solution
In this lesson, we will discuss the solutions for the assignments in this chapter.
We'll cover the following...
Problem one
You’ll need to create the model with more words. In the project, we only used 10,000 words, so increase this number.
Problem two
Try to change the hyperparameters and increase the accuracy of the model.
Below is the code snippet for the above two problem statements.
Press + to interact
# Import the required librariesfrom tensorflow.keras.datasets import imdbfrom tensorflow.python.keras.callbacks import ModelCheckpointfrom tensorflow.python.keras.callbacks import EarlyStoppingfrom tensorflow.python.keras.preprocessing import sequencefrom tensorflow.python.keras.layers import Embedding,SimpleRNN,Densefrom tensorflow.python.keras.models import Sequential# Load the Dataset((XT,YT),(Xt,Yt)) = imdb.load_data(num_words=30000)print("The length of the Training Dataset is ", len(XT))print("The length of the Testing Dataset is ", len(Xt))# Perform the paddingX_train = sequence.pad_sequences(XT,maxlen=500)X_test = sequence.pad_sequences(Xt,maxlen=500)# Create the Model Architecturemodel = Sequential()model.add(Embedding(30000,128))model.add(SimpleRNN(64))model.add(Dense(1,activation='sigmoid'))# Compile the Modelmodel.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['acc'])# Create the Callbackscheckpoint = ModelCheckpoint("best_model.h5", monitor='val_loss', verbose=0, save_best_only=True, save_weights_only=False)earlystop = EarlyStopping(monitor='val_acc',patience=1)# Train the Modelhist = model.fit(X_train,YT,validation_split=0.2,epochs=10,batch_size=128,callbacks=[checkpoint,earlystop])# Evaluate the Model on Test Datasetmodel.evaluate(X_test,Yt)
Explanation:
- We used a similar code which we discussed in the previous