Loading notebooks/convolutional_sentiment_classifier_LT.ipynb +1 −1 Changes for notebooks/convolutional_sentiment_classifier_LT.ipynb: 1 added line, 1 removed line. Original line number Diff line number Diff line %% Cell type:markdown id: tags: # Convolutional Sentiment Classifier %% Cell type:markdown id: tags: In this notebook, we build a *convolutional* neural net to classify IMDB movie reviews by their sentiment. %% Cell type:markdown id: tags: #### Load dependencies %% Cell type:code id: tags: ``` python import keras from keras.datasets import imdb from keras.preprocessing.sequence import pad_sequences from keras.models import Sequential from keras.layers import Dense, Dropout, Embedding from keras.layers import SpatialDropout1D, Conv1D, GlobalMaxPooling1D # new! from keras.callbacks import ModelCheckpoint import os from sklearn.metrics import roc_auc_score import matplotlib.pyplot as plt %matplotlib inline ``` %% Cell type:markdown id: tags: #### Set hyperparameters %% Cell type:code id: tags: ``` python # output directory name: output_dir = 'model_output/conv' # training: epochs = 4 batch_size = 32 batch_size = 128 # vector-space embedding: n_dim = 64 n_unique_words = 5000 max_review_length = 400 pad_type = trunc_type = 'pre' drop_embed = 0.2 # new! # convolutional layer architecture: n_conv = 256 # filters, a.k.a. kernels k_conv = 3 # kernel length # dense layer architecture: n_dense = 256 dropout = 0.2 ``` %% Cell type:markdown id: tags: #### Load data %% Cell type:code id: tags: ``` python #(x_train, y_train), (x_valid, y_valid) = imdb.load_data(num_words=n_unique_words, skip_top=n_words_to_skip) import pickle x_train = pickle.load(open('../data/imdb/x_train.pkl', 'rb')) y_train = pickle.load(open('../data/imdb/y_train.pkl', 'rb')) x_valid = pickle.load(open('../data/imdb/x_valid.pkl', 'rb')) y_valid = pickle.load(open('../data/imdb/y_valid.pkl', 'rb')) ``` %% Cell type:markdown id: tags: #### Preprocess data %% Cell type:code id: tags: ``` python x_train = pad_sequences(x_train, maxlen=max_review_length, padding=pad_type, truncating=trunc_type, value=0) x_valid = pad_sequences(x_valid, maxlen=max_review_length, padding=pad_type, truncating=trunc_type, value=0) ``` %% Cell type:markdown id: tags: #### Design neural network architecture %% Cell type:code id: tags: ``` python model = Sequential() model.add(Embedding(n_unique_words, n_dim, input_length=max_review_length)) # CODE HERE model.add(Dense(n_dense, activation='relu')) model.add(Dropout(dropout)) model.add(Dense(1, activation='sigmoid')) ``` %% Cell type:code id: tags: ``` python model.summary() ``` %% Cell type:markdown id: tags: #### Configure model %% Cell type:code id: tags: ``` python model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) ``` %% Cell type:code id: tags: ``` python modelcheckpoint = ModelCheckpoint(filepath=output_dir+"/weights.{epoch:02d}.hdf5") if not os.path.exists(output_dir): os.makedirs(output_dir) ``` %% Cell type:markdown id: tags: #### Train! %% Cell type:code id: tags: ``` python # 89.1% validation accuracy in epoch 2 # ...with second convolutional layer is essentially the same at 89.0% model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_valid, y_valid), callbacks=[modelcheckpoint]) ``` %% Cell type:markdown id: tags: #### Evaluate %% Cell type:code id: tags: ``` python model.load_weights(output_dir+"/weights.01.hdf5") # zero-indexed ``` %% Cell type:code id: tags: ``` python y_hat = model.predict_proba(x_valid) ``` %% Cell type:code id: tags: ``` python plt.hist(y_hat) _ = plt.axvline(x=0.5, color='orange') ``` %% Cell type:code id: tags: ``` python "{:0.2f}".format(roc_auc_score(y_valid, y_hat)*100.0) ``` %% Cell type:code id: tags: ``` python ``` Loading
notebooks/convolutional_sentiment_classifier_LT.ipynb +1 −1 Changes for notebooks/convolutional_sentiment_classifier_LT.ipynb: 1 added line, 1 removed line. Original line number Diff line number Diff line %% Cell type:markdown id: tags: # Convolutional Sentiment Classifier %% Cell type:markdown id: tags: In this notebook, we build a *convolutional* neural net to classify IMDB movie reviews by their sentiment. %% Cell type:markdown id: tags: #### Load dependencies %% Cell type:code id: tags: ``` python import keras from keras.datasets import imdb from keras.preprocessing.sequence import pad_sequences from keras.models import Sequential from keras.layers import Dense, Dropout, Embedding from keras.layers import SpatialDropout1D, Conv1D, GlobalMaxPooling1D # new! from keras.callbacks import ModelCheckpoint import os from sklearn.metrics import roc_auc_score import matplotlib.pyplot as plt %matplotlib inline ``` %% Cell type:markdown id: tags: #### Set hyperparameters %% Cell type:code id: tags: ``` python # output directory name: output_dir = 'model_output/conv' # training: epochs = 4 batch_size = 32 batch_size = 128 # vector-space embedding: n_dim = 64 n_unique_words = 5000 max_review_length = 400 pad_type = trunc_type = 'pre' drop_embed = 0.2 # new! # convolutional layer architecture: n_conv = 256 # filters, a.k.a. kernels k_conv = 3 # kernel length # dense layer architecture: n_dense = 256 dropout = 0.2 ``` %% Cell type:markdown id: tags: #### Load data %% Cell type:code id: tags: ``` python #(x_train, y_train), (x_valid, y_valid) = imdb.load_data(num_words=n_unique_words, skip_top=n_words_to_skip) import pickle x_train = pickle.load(open('../data/imdb/x_train.pkl', 'rb')) y_train = pickle.load(open('../data/imdb/y_train.pkl', 'rb')) x_valid = pickle.load(open('../data/imdb/x_valid.pkl', 'rb')) y_valid = pickle.load(open('../data/imdb/y_valid.pkl', 'rb')) ``` %% Cell type:markdown id: tags: #### Preprocess data %% Cell type:code id: tags: ``` python x_train = pad_sequences(x_train, maxlen=max_review_length, padding=pad_type, truncating=trunc_type, value=0) x_valid = pad_sequences(x_valid, maxlen=max_review_length, padding=pad_type, truncating=trunc_type, value=0) ``` %% Cell type:markdown id: tags: #### Design neural network architecture %% Cell type:code id: tags: ``` python model = Sequential() model.add(Embedding(n_unique_words, n_dim, input_length=max_review_length)) # CODE HERE model.add(Dense(n_dense, activation='relu')) model.add(Dropout(dropout)) model.add(Dense(1, activation='sigmoid')) ``` %% Cell type:code id: tags: ``` python model.summary() ``` %% Cell type:markdown id: tags: #### Configure model %% Cell type:code id: tags: ``` python model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) ``` %% Cell type:code id: tags: ``` python modelcheckpoint = ModelCheckpoint(filepath=output_dir+"/weights.{epoch:02d}.hdf5") if not os.path.exists(output_dir): os.makedirs(output_dir) ``` %% Cell type:markdown id: tags: #### Train! %% Cell type:code id: tags: ``` python # 89.1% validation accuracy in epoch 2 # ...with second convolutional layer is essentially the same at 89.0% model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_valid, y_valid), callbacks=[modelcheckpoint]) ``` %% Cell type:markdown id: tags: #### Evaluate %% Cell type:code id: tags: ``` python model.load_weights(output_dir+"/weights.01.hdf5") # zero-indexed ``` %% Cell type:code id: tags: ``` python y_hat = model.predict_proba(x_valid) ``` %% Cell type:code id: tags: ``` python plt.hist(y_hat) _ = plt.axvline(x=0.5, color='orange') ``` %% Cell type:code id: tags: ``` python "{:0.2f}".format(roc_auc_score(y_valid, y_hat)*100.0) ``` %% Cell type:code id: tags: ``` python ```