add IMDB bidirectional LSTM demo jupyter notebook

This commit is contained in:
Leon Chen
2016-10-12 11:47:30 -04:00
parent c3199283f3
commit 323b530116
@@ -0,0 +1,290 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bidirection LSTM - IMDB sentiment classification\n",
"\n",
"see **https://github.com/fchollet/keras/blob/master/examples/imdb_bidirectional_lstm.py**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"WEIGHTS_FILEPATH = 'imdb_bidirectional_lstm.hdf5'\n",
"MODEL_ARCH_FILEPATH = 'imdb_bidirectional_lstm.json'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"import numpy as np\n",
"np.random.seed(1337) # for reproducibility\n",
"\n",
"from keras.preprocessing import sequence\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense, Dropout, Embedding, LSTM, Input, Bidirectional\n",
"from keras.datasets import imdb\n",
"from keras.callbacks import EarlyStopping, ModelCheckpoint\n",
"\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading data...\n",
"25000 train sequences\n",
"25000 test sequences\n",
"Pad sequences (samples x time)\n",
"X_train shape: (25000, 200)\n",
"X_test shape: (25000, 200)\n"
]
}
],
"source": [
"max_features = 20000\n",
"maxlen = 200 # cut texts after this number of words (among top max_features most common words)\n",
"\n",
"print('Loading data...')\n",
"(X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features)\n",
"print(len(X_train), 'train sequences')\n",
"print(len(X_test), 'test sequences')\n",
"\n",
"print(\"Pad sequences (samples x time)\")\n",
"X_train = sequence.pad_sequences(X_train, maxlen=maxlen)\n",
"X_test = sequence.pad_sequences(X_test, maxlen=maxlen)\n",
"print('X_train shape:', X_train.shape)\n",
"print('X_test shape:', X_test.shape)\n",
"y_train = np.array(y_train)\n",
"y_test = np.array(y_test)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"model = Sequential()\n",
"model.add(Embedding(max_features, 64, input_length=maxlen))\n",
"model.add(Bidirectional(LSTM(32)))\n",
"model.add(Dropout(0.5))\n",
"model.add(Dense(1, activation='sigmoid'))\n",
"\n",
"# try using different optimizers and different optimizer configs\n",
"model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 25000 samples, validate on 25000 samples\n",
"Epoch 1/10\n",
"24960/25000 [============================>.] - ETA: 0s - loss: 0.5654 - acc: 0.7185Epoch 00000: val_acc improved from -inf to 0.80596, saving model to imdb_bidirectional_lstm.hdf5\n",
"25000/25000 [==============================] - 147s - loss: 0.5654 - acc: 0.7184 - val_loss: 0.4700 - val_acc: 0.8060\n",
"Epoch 2/10\n",
"24960/25000 [============================>.] - ETA: 0s - loss: 0.3137 - acc: 0.8826Epoch 00001: val_acc improved from 0.80596 to 0.86944, saving model to imdb_bidirectional_lstm.hdf5\n",
"25000/25000 [==============================] - 144s - loss: 0.3137 - acc: 0.8826 - val_loss: 0.3071 - val_acc: 0.8694\n",
"Epoch 3/10\n",
"24960/25000 [============================>.] - ETA: 0s - loss: 0.1935 - acc: 0.9345Epoch 00002: val_acc did not improve\n",
"25000/25000 [==============================] - 140s - loss: 0.1936 - acc: 0.9344 - val_loss: 0.3334 - val_acc: 0.8625\n",
"Epoch 4/10\n",
"24960/25000 [============================>.] - ETA: 0s - loss: 0.1390 - acc: 0.9560Epoch 00003: val_acc did not improve\n",
"25000/25000 [==============================] - 138s - loss: 0.1390 - acc: 0.9560 - val_loss: 0.3481 - val_acc: 0.8649\n",
"Epoch 5/10\n",
"24960/25000 [============================>.] - ETA: 0s - loss: 0.0919 - acc: 0.9728Epoch 00004: val_acc did not improve\n",
"Epoch 00004: early stopping\n",
"25000/25000 [==============================] - 139s - loss: 0.0918 - acc: 0.9729 - val_loss: 0.5343 - val_acc: 0.8450\n"
]
},
{
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7f8140f16a20>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Model saving callback\n",
"checkpointer = ModelCheckpoint(filepath=WEIGHTS_FILEPATH, monitor='val_acc', verbose=1, save_best_only=True)\n",
"\n",
"# Early stopping\n",
"early_stopping = EarlyStopping(monitor='val_acc', verbose=1, patience=2)\n",
"\n",
"# train\n",
"batch_size = 128\n",
"nb_epoch = 10\n",
"model.fit(X_train, y_train, \n",
" validation_data=[X_test, y_test],\n",
" batch_size=batch_size, nb_epoch=nb_epoch, verbose=1,\n",
" callbacks=[checkpointer, early_stopping])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"with open(MODEL_ARCH_FILEPATH, 'w') as f:\n",
" f.write(model.to_json())"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"**sample data**"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"word_index = imdb.get_word_index()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"word_dict = {idx: word for word, idx in word_index.items()}"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"\"and you could just imagine being there robert is an amazing actor and now the same being director father came from the same scottish island as myself so i loved the fact there was a real connection with this film the witty remarks throughout the film were great it was just brilliant so much that i bought the film as soon as it was released for retail and would recommend it to everyone to watch and the fly fishing was amazing really cried at the end it was so sad and you know what they say if you cry at a film it must have been good and this definitely was also congratulations to the two little boy's that played the of norman and paul they were just brilliant children are often left out of the praising list i think because the stars that play them all grown up are such a big profile for the whole film but these children are amazing and should be praised for what they have done don't you think the whole story was so lovely because it was true and was someone's life after all that was shared with us all\""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"' '.join([word_dict[idx-3] for idx in X_train[0] if idx >= 3])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"with open('imdb_dataset_word_index_top20000.json', 'w') as f:\n",
" f.write(json.dumps({word: idx for word, idx in word_index.items() if idx < max_features}))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sample_test_data = []\n",
"for i in np.random.choice(range(X_test.shape[0]), size=1000, replace=False):\n",
" sample_test_data.append({'values': X_test[i].tolist(), 'label': y_test[i].tolist()})\n",
" \n",
"with open('imdb_dataset_test.json', 'w') as f:\n",
" f.write(json.dumps(sample_test_data))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}