This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
'''Trains an LSTM on the IMDB sentiment classification task with soft attention. | |
Experiments with max_features=10000, max_len=80 | |
1) MLP-dropout-tanh attention: 83.59 at epoch 4 | |
2) MLP-dropout-relu attention: 83.26 at epoch 3 | |
3) MLP-tanh attention: 82.91 at epoch 4 | |
4) GlobalMaxPooling1D attention: 82.44 at epoch 7 | |
''' | |
from __future__ import print_function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.engine.topology import Layer | |
from keras import initializations | |
from keras import backend as K | |
class Attention(Layer): | |
'''Attention operation for temporal data. | |
# Input shape | |
3D tensor with shape: `(samples, steps, features)`. | |
# Output shape | |
2D tensor with shape: `(samples, features)`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Train a Bidirectional LSTM on the IMDB sentiment classification task. | |
Output after 4 epochs on CPU: ~0.8146 | |
Time per epoch on CPU (Core i7): ~150s. | |
''' | |
from __future__ import print_function | |
import numpy as np | |
np.random.seed(1337) # for reproducibility |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python 2.7.6 (default, Jun 22 2015, 17:58:13) | |
[GCC 4.8.2] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> runfile('/home/ameasure/Desktop/bilstm_example.py', wdir='/home/ameasure/Desktop') | |
Using Theano backend. | |
Loading data... | |
20000 train sequences | |
5000 test sequences | |
Pad sequences (samples x time) | |
X_train shape: (20000, 100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Example of text classification using a Convolution1D network with one hot | |
representation. Adapted from the imdb_cnn.py example. | |
Gets to 0.8292 test accuracy after 2 epochs. 153s/epoch on GTX660 GPU. | |
""" | |
from __future__ import print_function | |
import numpy as np |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import pandas as pd | |
import numpy as np | |
np.random.seed(42) | |
from sklearn.preprocessing import LabelEncoder | |
import theano | |
import keras |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import print_function | |
import numpy as np | |
from keras.datasets import reuters | |
from keras.models import Sequential | |
from keras.layers.embeddings import Embedding | |
from keras.layers.convolutional import Convolution2D, MaxPooling2D | |
from keras.layers.core import Dense, Dropout, Activation, Flatten, Reshape, Merge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def is_foot_injury(narrative): | |
narrative = narrative.lower() | |
words = re.findall('\w+', narrative) | |
if 'foot' in words: | |
return True | |
elif 'feet' in words: | |
return True | |
else: |