Skip to content

Instantly share code, notes, and snippets.

View ameasure's full-sized avatar

Alexander Measure ameasure

View GitHub Profile
@ameasure
ameasure / imdb_soft_attention_lstm
Created February 11, 2017 19:47
imdb_soft_attention_lstm.py
# -*- 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
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)`.
'''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
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)
# -*- 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
import datetime
import pandas as pd
import numpy as np
np.random.seed(42)
from sklearn.preprocessing import LabelEncoder
import theano
import keras
@ameasure
ameasure / gist:944439a04546f4c02cb9
Created July 8, 2015 02:47
reuters_multi_cnn.py
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
@ameasure
ameasure / foot_coder.py
Created August 14, 2014 19:48
Simple rule based foot injury autocoder
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: