|
| 1 | +# author - Richard Liao |
| 2 | +# Dec 26 2016 |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +import cPickle |
| 6 | +from collections import defaultdict |
| 7 | +import re |
| 8 | + |
| 9 | +from bs4 import BeautifulSoup |
| 10 | + |
| 11 | +import sys |
| 12 | +import os |
| 13 | + |
| 14 | +os.environ['KERAS_BACKEND']='theano' |
| 15 | + |
| 16 | +from keras.preprocessing.text import Tokenizer, text_to_word_sequence |
| 17 | +from keras.preprocessing.sequence import pad_sequences |
| 18 | +from keras.utils.np_utils import to_categorical |
| 19 | + |
| 20 | +from keras.layers import Embedding |
| 21 | +from keras.layers import Dense, Input, Flatten |
| 22 | +from keras.layers import Conv1D, MaxPooling1D, Embedding, Merge, Dropout, LSTM, GRU, Bidirectional, TimeDistributed |
| 23 | +from keras.models import Model |
| 24 | + |
| 25 | +from keras import backend as K |
| 26 | +from keras.engine.topology import Layer, InputSpec |
| 27 | +from keras import initializations |
| 28 | + |
| 29 | +MAX_SENT_LENGTH = 100 |
| 30 | +MAX_SENTS = 15 |
| 31 | +MAX_NB_WORDS = 20000 |
| 32 | +EMBEDDING_DIM = 100 |
| 33 | +VALIDATION_SPLIT = 0.2 |
| 34 | + |
| 35 | +def clean_str(string): |
| 36 | + """ |
| 37 | + Tokenization/string cleaning for dataset |
| 38 | + Every dataset is lower cased except |
| 39 | + """ |
| 40 | + string = re.sub(r"\\", "", string) |
| 41 | + string = re.sub(r"\'", "", string) |
| 42 | + string = re.sub(r"\"", "", string) |
| 43 | + return string.strip().lower() |
| 44 | + |
| 45 | +data_train = pd.read_csv('~/Testground/data/imdb/labeledTrainData.tsv', sep='\t') |
| 46 | +print data_train.shape |
| 47 | + |
| 48 | +from nltk import tokenize |
| 49 | + |
| 50 | +reviews = [] |
| 51 | +labels = [] |
| 52 | +texts = [] |
| 53 | + |
| 54 | +for idx in range(data_train.review.shape[0]): |
| 55 | + text = BeautifulSoup(data_train.review[idx]) |
| 56 | + text = clean_str(text.get_text().encode('ascii','ignore')) |
| 57 | + texts.append(text) |
| 58 | + sentences = tokenize.sent_tokenize(text) |
| 59 | + reviews.append(sentences) |
| 60 | + |
| 61 | + labels.append(data_train.sentiment[idx]) |
| 62 | + |
| 63 | +tokenizer = Tokenizer(nb_words=MAX_NB_WORDS) |
| 64 | +tokenizer.fit_on_texts(texts) |
| 65 | + |
| 66 | +data = np.zeros((len(texts), MAX_SENTS, MAX_SENT_LENGTH), dtype='int32') |
| 67 | + |
| 68 | +for i, sentences in enumerate(reviews): |
| 69 | + for j, sent in enumerate(sentences): |
| 70 | + if j< MAX_SENTS: |
| 71 | + wordTokens = text_to_word_sequence(sent) |
| 72 | + k=0 |
| 73 | + for _, word in enumerate(wordTokens): |
| 74 | + if k<MAX_SENT_LENGTH and tokenizer.word_index[word]<MAX_NB_WORDS: |
| 75 | + data[i,j,k] = tokenizer.word_index[word] |
| 76 | + k=k+1 |
| 77 | + |
| 78 | +word_index = tokenizer.word_index |
| 79 | +print('Total %s unique tokens.' % len(word_index)) |
| 80 | + |
| 81 | +labels = to_categorical(np.asarray(labels)) |
| 82 | +print('Shape of data tensor:', data.shape) |
| 83 | +print('Shape of label tensor:', labels.shape) |
| 84 | + |
| 85 | +indices = np.arange(data.shape[0]) |
| 86 | +np.random.shuffle(indices) |
| 87 | +data = data[indices] |
| 88 | +labels = labels[indices] |
| 89 | +nb_validation_samples = int(VALIDATION_SPLIT * data.shape[0]) |
| 90 | + |
| 91 | +x_train = data[:-nb_validation_samples] |
| 92 | +y_train = labels[:-nb_validation_samples] |
| 93 | +x_val = data[-nb_validation_samples:] |
| 94 | +y_val = labels[-nb_validation_samples:] |
| 95 | + |
| 96 | +print('Number of positive and negative reviews in traing and validation set') |
| 97 | +print y_train.sum(axis=0) |
| 98 | +print y_val.sum(axis=0) |
| 99 | + |
| 100 | +GLOVE_DIR = "/ext/home/analyst/Testground/data/glove" |
| 101 | +embeddings_index = {} |
| 102 | +f = open(os.path.join(GLOVE_DIR, 'glove.6B.100d.txt')) |
| 103 | +for line in f: |
| 104 | + values = line.split() |
| 105 | + word = values[0] |
| 106 | + coefs = np.asarray(values[1:], dtype='float32') |
| 107 | + embeddings_index[word] = coefs |
| 108 | +f.close() |
| 109 | + |
| 110 | +print('Total %s word vectors.' % len(embeddings_index)) |
| 111 | + |
| 112 | +embedding_matrix = np.random.random((len(word_index) + 1, EMBEDDING_DIM)) |
| 113 | +for word, i in word_index.items(): |
| 114 | + embedding_vector = embeddings_index.get(word) |
| 115 | + if embedding_vector is not None: |
| 116 | + # words not found in embedding index will be all-zeros. |
| 117 | + embedding_matrix[i] = embedding_vector |
| 118 | + |
| 119 | +embedding_layer = Embedding(len(word_index) + 1, |
| 120 | + EMBEDDING_DIM, |
| 121 | + weights=[embedding_matrix], |
| 122 | + input_length=MAX_SENT_LENGTH, |
| 123 | + trainable=True) |
| 124 | + |
| 125 | +sentence_input = Input(shape=(MAX_SENT_LENGTH,), dtype='int32') |
| 126 | +embedded_sequences = embedding_layer(sentence_input) |
| 127 | +l_lstm = Bidirectional(LSTM(100))(embedded_sequences) |
| 128 | +sentEncoder = Model(sentence_input, l_lstm) |
| 129 | + |
| 130 | +review_input = Input(shape=(MAX_SENTS,MAX_SENT_LENGTH), dtype='int32') |
| 131 | +review_encoder = TimeDistributed(sentEncoder)(review_input) |
| 132 | +l_lstm_sent = Bidirectional(LSTM(100))(review_encoder) |
| 133 | +preds = Dense(2, activation='softmax')(l_lstm_sent) |
| 134 | +model = Model(review_input, preds) |
| 135 | + |
| 136 | +model.compile(loss='categorical_crossentropy', |
| 137 | + optimizer='rmsprop', |
| 138 | + metrics=['acc']) |
| 139 | + |
| 140 | +print("model fitting - Hierachical LSTM") |
| 141 | +print model.summary() |
| 142 | +model.fit(x_train, y_train, validation_data=(x_val, y_val), |
| 143 | + nb_epoch=10, batch_size=50) |
| 144 | + |
| 145 | +# building Hierachical Attention network |
| 146 | +embedding_matrix = np.random.random((len(word_index) + 1, EMBEDDING_DIM)) |
| 147 | +for word, i in word_index.items(): |
| 148 | + embedding_vector = embeddings_index.get(word) |
| 149 | + if embedding_vector is not None: |
| 150 | + # words not found in embedding index will be all-zeros. |
| 151 | + embedding_matrix[i] = embedding_vector |
| 152 | + |
| 153 | +embedding_layer = Embedding(len(word_index) + 1, |
| 154 | + EMBEDDING_DIM, |
| 155 | + weights=[embedding_matrix], |
| 156 | + input_length=MAX_SENT_LENGTH, |
| 157 | + trainable=True) |
| 158 | + |
| 159 | +class AttLayer(Layer): |
| 160 | + def __init__(self, **kwargs): |
| 161 | + self.init = initializations.get('normal') |
| 162 | + #self.input_spec = [InputSpec(ndim=3)] |
| 163 | + super(AttLayer, self).__init__(**kwargs) |
| 164 | + |
| 165 | + def build(self, input_shape): |
| 166 | + assert len(input_shape)==3 |
| 167 | + #self.W = self.init((input_shape[-1],1)) |
| 168 | + self.W = self.init((input_shape[-1],)) |
| 169 | + #self.input_spec = [InputSpec(shape=input_shape)] |
| 170 | + self.trainable_weights = [self.W] |
| 171 | + super(AttLayer, self).build(input_shape) # be sure you call this somewhere! |
| 172 | + |
| 173 | + def call(self, x, mask=None): |
| 174 | + eij = K.tanh(K.dot(x, self.W)) |
| 175 | + |
| 176 | + ai = K.exp(eij) |
| 177 | + weights = ai/K.sum(ai, axis=1).dimshuffle(0,'x') |
| 178 | + |
| 179 | + weighted_input = x*weights.dimshuffle(0,1,'x') |
| 180 | + return weighted_input.sum(axis=1) |
| 181 | + |
| 182 | + def get_output_shape_for(self, input_shape): |
| 183 | + return (input_shape[0], input_shape[-1]) |
| 184 | + |
| 185 | +sentence_input = Input(shape=(MAX_SENT_LENGTH,), dtype='int32') |
| 186 | +embedded_sequences = embedding_layer(sentence_input) |
| 187 | +l_lstm = Bidirectional(GRU(100, return_sequences=True))(embedded_sequences) |
| 188 | +l_dense = TimeDistributed(Dense(200))(l_lstm) |
| 189 | +l_att = AttLayer()(l_dense) |
| 190 | +sentEncoder = Model(sentence_input, l_att) |
| 191 | + |
| 192 | +review_input = Input(shape=(MAX_SENTS,MAX_SENT_LENGTH), dtype='int32') |
| 193 | +review_encoder = TimeDistributed(sentEncoder)(review_input) |
| 194 | +l_lstm_sent = Bidirectional(GRU(100, return_sequences=True))(review_encoder) |
| 195 | +l_dense_sent = TimeDistributed(Dense(200))(l_lstm_sent) |
| 196 | +l_att_sent = AttLayer()(l_dense_sent) |
| 197 | +preds = Dense(2, activation='softmax')(l_att_sent) |
| 198 | +model = Model(review_input, preds) |
| 199 | + |
| 200 | +model.compile(loss='categorical_crossentropy', |
| 201 | + optimizer='rmsprop', |
| 202 | + metrics=['acc']) |
| 203 | + |
| 204 | +print("model fitting - Hierachical attention network") |
| 205 | +model.fit(x_train, y_train, validation_data=(x_val, y_val), |
| 206 | + nb_epoch=10, batch_size=50) |
0 commit comments