-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAttentionLayer.py
159 lines (144 loc) · 5.63 KB
/
AttentionLayer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import tensorflow as tf
from tensorflow.keras.models import load_model, Model
from tensorflow.python.keras import backend as K
# from keras.backend.tensorflow_backend import set_session
config = tf.compat.v1.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.2
K.set_session(tf.compat.v1.Session(config=config))
from keras import Input
import keras
from keras.optimizers import Adam
import keras.backend as K
import numpy as np
from keras.layers import *
from keras.layers.core import Dense, Dropout
from keras.models import Sequential, Model
from keras.layers.recurrent import LSTM
import pandas as pd
MAX_SENT_LENGTH = 100
MAX_SENTS = 15
MAX_NB_WORDS = 20000
EMBEDDING_DIM = 100
VALIDATION_SPLIT = 0.2
# data_train = pd.read_csv('labeledTrainData.tsv', sep='\t')
# data_train = pd.read_csv('C:/Users/dina_/Desktop/final/HHH-An-Online-Question-Answering-System-for-Medical-Questions/Data/Model_train_dev_test_dataset/Other_model_train_dev_test_dataset/train.csv', encoding= 'unicode_escape')
# print(data_train)
# print (data_train['qid1'])qid1
from nltk import tokenize
from keras.preprocessing.text import Tokenizer, text_to_word_sequence
from keras.preprocessing.sequence import pad_sequences
from keras.utils.np_utils import to_categorical
# reviews = []
# labels = []
# texts = []
# for idx in range(data_train.reviews.shape[0]):
# text = data_train.review[idx]
# texts.append(text)
# sentences = tokenize.sent_tokenize(text)
# reviews.append(sentences)
#
# labels.append(data_train.sentiment[idx])
# tokenizer = Tokenizer(num_words=MAX_NB_WORDS)
# tokenizer.fit_on_texts(texts)
#
# data = np.zeros((len(texts), MAX_SENTS, MAX_SENT_LENGTH), dtype='int32')
#
# for i, sentences in enumerate(reviews):
# for j, sent in enumerate(sentences):
# if j< MAX_SENTS:
# wordTokens = text_to_word_sequence(sent)
# k=0
# for _, word in enumerate(wordTokens):
# if k<MAX_SENT_LENGTH and tokenizer.word_index[word]<MAX_NB_WORDS:
# data[i,j,k] = tokenizer.word_index[word]
# k=k+1
#
# word_index = tokenizer.word_index
# print('Total %s unique tokens.' % len(word_index))
#
# labels = to_categorical(np.asarray(labels))
# print('Shape of data tensor:', data.shape)
# print('Shape of label tensor:', labels.shape)
#
# indices = np.arange(data.shape[0])
# np.random.shuffle(indices)
# data = data[indices]
# labels = labels[indices]
# nb_validation_samples = int(VALIDATION_SPLIT * data.shape[0])
#
# x_train = data[:-nb_validation_samples]
# y_train = labels[:-nb_validation_samples]
# x_val = data[-nb_validation_samples:]
# y_val = labels[-nb_validation_samples:]
#
# print('Number of positive and negative reviews in traing and validation set')
# print(y_train.sum(axis=0))
# print(y_val.sum(axis=0))
#
# embedding_layer = Embedding(len(word_index) + 1,
# EMBEDDING_DIM,
# input_length=MAX_SENT_LENGTH)
#
# sentence_input = Input(shape=(MAX_SENT_LENGTH,), dtype='int32')
# embedded_sequences = embedding_layer(sentence_input)
# l_lstm = Bidirectional(LSTM(100))(embedded_sequences)
# sentEncoder = Model(sentence_input, l_lstm)
#
# review_input = Input(shape=(MAX_SENTS,MAX_SENT_LENGTH), dtype='int32')
# review_encoder = TimeDistributed(sentEncoder)(review_input)
# l_lstm_sent = Bidirectional(LSTM(100))(review_encoder)
# preds = Dense(2, activation='softmax')(l_lstm_sent)
# model = Model(review_input, preds)
#
# model.compile(loss='categorical_crossentropy',
# optimizer='rmsprop',
# metrics=['acc'])
#
# print("model fitting - Hierachical LSTM")
# model.summary()
class AttentionLayer(Layer):
def __init__(self, **kwargs):
super(AttentionLayer, self).__init__(** kwargs)
def build(self, input_shape):
assert len(input_shape)==3
# W.shape = (time_steps, time_steps)
self.W = self.add_weight(name='att_weight',
shape=(input_shape[1], input_shape[1]),
initializer='uniform',
trainable=True)
super(AttentionLayer, self).build(input_shape)
def call(self, inputs, mask=None):
# inputs.shape = (batch_size, time_steps, seq_len)
x = K.permute_dimensions(inputs, (0, 2, 1))
# x.shape = (batch_size, seq_len, time_steps)
# general
a = K.softmax(K.tanh(K.dot(x, self.W)))
a = K.permute_dimensions(a, (0, 2, 1))
outputs = a * inputs
outputs = K.sum(outputs, axis=1)
return outputs
def compute_output_shape(self, input_shape):
return input_shape[0], input_shape[2]
# sentence_input = Input(shape=(MAX_SENT_LENGTH,), dtype='int32')
# embedded_sequences = embedding_layer(sentence_input)
# l_lstm = Bidirectional(GRU(100, return_sequences=True))(embedded_sequences)
# l_dense = TimeDistributed(Dense(200))(l_lstm)
# l_att = AttentionLayer()(l_dense)
# sentEncoder = Model(sentence_input, l_att)
#
# review_input = Input(shape=(MAX_SENTS,MAX_SENT_LENGTH), dtype='int32')
# review_encoder = TimeDistributed(sentEncoder)(review_input)
# l_lstm_sent = Bidirectional(GRU(100, return_sequences=True))(review_encoder)
# l_dense_sent = TimeDistributed(Dense(200))(l_lstm_sent)
# l_att_sent = AttentionLayer()(l_dense_sent)
# preds = Dense(2, activation='softmax')(l_att_sent)
# model = Model(review_input, preds)
# model.summary()
#
# model.compile(loss='categorical_crossentropy',
# optimizer='rmsprop',
# metrics=['acc'])
#
# print("model fitting - Hierachical attention network")
# model.fit(x_train, y_train, validation_data=(x_val, y_val),
# epochs=10, batch_size=50)