Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import keras
- import numpy as np
- from keras.datasets import imdb
- from matplotlib import pyplot as plt
- from keras.models import Model
- from keras.layers import Flatten,Dense,Input,Conv1D,MaxPooling1D,Dropout,GlobalAveragePooling1D
- from keras.models import Model
- from keras.layers.embeddings import Embedding
- from keras.preprocessing import sequence
- top_words = 5000
- (x_train,y_train),(x_test,y_test) = imdb.load_data(num_words=top_words)
- def data_analysis():
- print("Training data shape:")
- print(f"X_train:{x_train.shape}")
- print(f"Y_train:{y_train.shape}")
- print("Average length review:")
- lengths = [len(x) for x in x_train]
- print(f"Mean:{np.mean(lengths)} std:{np.std(lengths)}")
- plt.boxplot(lengths)
- plt.title(f"Mean:{np.mean(lengths)} std:{np.std(lengths)}")
- plt.show()
- #data_analysis()
- max_words = 500
- x_train = sequence.pad_sequences(x_train,maxlen=max_words)
- x_test = sequence.pad_sequences(x_test,maxlen=max_words)
- def test_dense():
- inputs = Input((max_words,))
- h = Embedding(top_words,128,input_length=max_words)(inputs)
- h = Flatten()(h)
- h = Dense(256,activation='relu')(h)
- outputs = Dense(1,activation='sigmoid')(h)
- model = Model(inputs=inputs,outputs=outputs)
- model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
- model.summary()
- history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=20, batch_size=128, verbose=2)
- scores = model.evaluate(x_test, y_test, verbose=0)
- print("Accuracy: %.2f%%" % (scores[1]*100))
- # Plot training & validation accuracy values
- plt.plot(history.history['acc'])
- plt.plot(history.history['val_acc'])
- plt.title('Model accuracy')
- plt.ylabel('Accuracy')
- plt.xlabel('Epoch')
- plt.legend(['Train', 'Test'], loc='upper left')
- plt.show()
- # Plot training & validation loss values
- plt.plot(history.history['loss'])
- plt.plot(history.history['val_loss'])
- plt.title('Model loss')
- plt.ylabel('Loss')
- plt.xlabel('Epoch')
- plt.legend(['Train', 'Test'], loc='upper left')
- plt.show()
- def test_cnn():
- inputs = Input((max_words,))
- h = Embedding(top_words,128,input_length=max_words)(inputs)
- h = Conv1D(filters=8,kernel_size=3,padding='same',activation='relu')(h)
- h = MaxPooling1D(pool_size=2)(h)
- h = Conv1D(filters=16,kernel_size=3,padding='same',activation='relu')(h)
- h = GlobalAveragePooling1D()(h)
- h = Dropout(0.5)(h)
- outputs = Dense(1,activation='sigmoid')(h)
- model = Model(inputs=inputs,outputs=outputs)
- model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
- model.summary()
- history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=20, batch_size=128)
- scores = model.evaluate(x_test, y_test, verbose=0)
- print("Accuracy: %.2f%%" % (scores[1]*100))
- # Plot training & validation accuracy values
- plt.plot(history.history['acc'])
- plt.plot(history.history['val_acc'])
- plt.title('Model accuracy')
- plt.ylabel('Accuracy')
- plt.xlabel('Epoch')
- plt.legend(['Train', 'Test'], loc='upper left')
- plt.show()
- # Plot training & validation loss values
- plt.plot(history.history['loss'])
- plt.plot(history.history['val_loss'])
- plt.title('Model loss')
- plt.ylabel('Loss')
- plt.xlabel('Epoch')
- plt.legend(['Train', 'Test'], loc='upper left')
- plt.show()
- #test_dense()
- test_cnn()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement