-
Notifications
You must be signed in to change notification settings - Fork 5
/
text-classification-with-LSTM.py
229 lines (189 loc) · 7.92 KB
/
text-classification-with-LSTM.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#%%
from __future__ import print_function # Use a function definition from future version (say 3.x from 2.7 interpreter)
import requests
import os
def download(url, filename):
""" utility function to download a file """
response = requests.get(url, stream=True)
with open(filename, "wb") as handle:
for data in response.iter_content():
handle.write(data)
locations = ['Tutorials/SLUHandsOn', 'Examples/LanguageUnderstanding/ATIS/BrainScript','data/atis']
data = {
'train': { 'file': 'atis.train.ctf', 'location': 2 },
'test': { 'file': 'atis.test.ctf', 'location': 2 },
'query': { 'file': 'query.wl', 'location': 2 },
'slots': { 'file': 'slots.wl', 'location': 2 }
}
#%%
for item in data.values():
location = locations[item['location']]
path = os.path.join(location, item['file'])
print(path)
if os.path.exists(path):
print("Reusing locally cached:", item['file'])
# Update path
item['file'] = path
elif os.path.exists(item['file']):
print("Reusing locally cached:", item['file'])
else:
print("Starting download:", item['file'])
url = "https://github.com/Microsoft/CNTK/blob/v2.0/%s/%s?raw=true"%(location, item['file'])
download(url, item['file'])
print("Download completed")
#%%
import math
import numpy as np
import cntk as C
#%%
# Select the right target device when this notebook is being tested:
if 'TEST_DEVICE' in os.environ:
if os.environ['TEST_DEVICE'] == 'cpu':
C.device.try_set_default_device(C.device.cpu())
else:
C.device.try_set_default_device(C.device.gpu(0))
#%%
# Test for CNTK version
if not C.__version__ == "2.0":
raise Exception("this notebook was designed to work with 2.0. Current Version: " + C.__version__)
#%%
# setting seed
np.random.seed(0)
C.cntk_py.set_fixed_random_seed(1)
C.cntk_py.force_deterministic_algorithms()
# number of words in vocab, slot labels, and intent labels
vocab_size = 943 ; num_labels = 129 ; num_intents = 26
# model dimensions
input_dim = vocab_size
label_dim = num_labels
emb_dim = 150
hidden_dim = 300
# Create the containers for input feature (x) and the label (y)
x = C.sequence.input_variable(vocab_size)
y = C.sequence.input_variable(num_labels)
def create_model():
with C.layers.default_options(initial_state=0.1):
return C.layers.Sequential([
C.layers.Embedding(emb_dim, name='embed'),
C.layers.Recurrence(C.layers.LSTM(hidden_dim), go_backwards=False),
C.layers.Dense(num_labels, name='classify')
])
#%%
# peek
z = create_model()
print(z.embed.E.shape)
print(z.classify.b.value)
#%%
# Pass an input and check the dimension
z = create_model()
print(z(x).embed.E.shape)
#%%
def create_reader(path, is_training):
return C.io.MinibatchSource(C.io.CTFDeserializer(path, C.io.StreamDefs(
query = C.io.StreamDef(field='S0', shape=vocab_size, is_sparse=True),
intent_unused = C.io.StreamDef(field='S1', shape=num_intents, is_sparse=True),
slot_labels = C.io.StreamDef(field='S2', shape=num_labels, is_sparse=True)
)), randomize=is_training, max_sweeps = C.io.INFINITELY_REPEAT if is_training else 1)
#%%
# peek
reader = create_reader(data['train']['file'], is_training=True)
reader.streams.keys()
#%%
def create_criterion_function(model):
labels = C.placeholder(name='labels')
ce = C.cross_entropy_with_softmax(model, labels)
errs = C.classification_error (model, labels)
return C.combine ([ce, errs]) # (features, labels) -> (loss, metric)
criterion = create_criterion_function(create_model())
criterion.replace_placeholders({criterion.placeholders[0]: C.sequence.input_variable(num_labels)})
#%%
def create_criterion_function_preferred(model, labels):
ce = C.cross_entropy_with_softmax(model, labels)
errs = C.classification_error (model, labels)
return ce, errs # (model, labels) -> (loss, error metric)
#%%
def train_test(train_reader, test_reader, model_func, max_epochs=10):
# Instantiate the model function; x is the input (feature) variable
model = model_func(x)
# Instantiate the loss and error function
loss, label_error = create_criterion_function_preferred(model, y)
# training config
epoch_size = 18000 # 18000 samples is half the dataset size
minibatch_size = 70
# LR schedule over epochs
# In CNTK, an epoch is how often we get out of the minibatch loop to
# do other stuff (e.g. checkpointing, adjust learning rate, etc.)
# (we don't run this many epochs, but if we did, these are good values)
lr_per_sample = [0.003]*4+[0.0015]*24+[0.0003]
lr_per_minibatch = [lr * minibatch_size for lr in lr_per_sample]
lr_schedule = C.learning_rate_schedule(lr_per_minibatch, C.UnitType.minibatch, epoch_size)
# Momentum schedule
momentum_as_time_constant = C.momentum_as_time_constant_schedule(700)
# We use a the Adam optimizer which is known to work well on this dataset
# Feel free to try other optimizers from
# https://www.cntk.ai/pythondocs/cntk.learner.html#module-cntk.learner
learner = C.adam(parameters=model.parameters,
lr=lr_schedule,
momentum=momentum_as_time_constant,
gradient_clipping_threshold_per_sample=15,
gradient_clipping_with_truncation=True)
# Setup the progress updater
progress_printer = C.logging.ProgressPrinter(tag='Training', num_epochs=max_epochs)
# Uncomment below for more detailed logging
#progress_printer = ProgressPrinter(freq=100, first=10, tag='Training', num_epochs=max_epochs)
# Instantiate the trainer
trainer = C.Trainer(model, (loss, label_error), learner, progress_printer)
# process minibatches and perform model training
C.logging.log_number_of_parameters(model)
t = 0
for epoch in range(max_epochs): # loop over epochs
epoch_end = (epoch+1) * epoch_size
while t < epoch_end: # loop over minibatches on the epoch
data = train_reader.next_minibatch(minibatch_size, input_map={ # fetch minibatch
x: train_reader.streams.query,
y: train_reader.streams.slot_labels
})
trainer.train_minibatch(data) # update model with it
t += data[y].num_samples # samples so far
trainer.summarize_training_progress()
while True:
minibatch_size = 500
data = test_reader.next_minibatch(minibatch_size, input_map={ # fetch minibatch
x: test_reader.streams.query,
y: test_reader.streams.slot_labels
})
if not data: # until we hit the end
break
trainer.test_minibatch(data)
trainer.summarize_test_progress()
#%%
def do_train_test():
global z
z = create_model()
train_reader = create_reader(data['train']['file'], is_training=True)
test_reader = create_reader(data['test']['file'], is_training=False)
train_test(train_reader, test_reader, z)
#%%
do_train_test()
#%%
z.classify.b.value
# load dictionaries
query_wl = [line.rstrip('\n') for line in open(data['query']['file'])]
slots_wl = [line.rstrip('\n') for line in open(data['slots']['file'])]
query_dict = {query_wl[i]:i for i in range(len(query_wl))}
slots_dict = {slots_wl[i]:i for i in range(len(slots_wl))}
# let's run a sequence through
seq = 'BOS flights from new york to seattle EOS'
# seq = 'BOS i want round trip flights from new york to toronto EOS'
# seq = 'BOS flights from new york to paris EOS'
w = [query_dict[w] for w in seq.split()] # convert to word indices
print(w)
onehot = np.zeros([len(w),len(query_dict)], np.float32)
for t in range(len(w)):
onehot[t,w[t]] = 1
#x = C.sequence.input_variable(vocab_size)
pred = z(x).eval({x:[onehot]})[0]
print(pred.shape)
best = np.argmax(pred,axis=1)
print(best)
list(zip(seq.split(),[slots_wl[s] for s in best]))