-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_linear_classifier.py
248 lines (191 loc) · 7.87 KB
/
tf_linear_classifier.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python3
import os
import numpy as np
import tensorflow as tf
from tf_model_base import TfModelBase
import warnings
import pdb
from defs import EMBED_SIZE, N_CASES, N_CLASSES, MAX_LENGTH
__author__ = 'Tucker Leavitt'
# Ignore the TensorFlow warning
# Converting sparse IndexedSlices to a dense Tensor of unknown shape.
# This may consume a large amount of memory.
warnings.filterwarnings("ignore", category=UserWarning)
# class BiRNNConfig():
# feature_dim = EMBED_SIZE + N_CASES
# n_features = 2
# keep_prob = 0.5
# max_pool = False
class TfLinearClassifier(TfModelBase):
"""Defines a Bidirectional RNN in which the final hidden state is used as
the basis for a softmax classifier predicting a label
Parameters
----------
vocab : list
The full vocabulary. `_convert_X` will convert the data provided
to `fit` and `predict` methods into a list of indices into this
list of items.
embedding : 2d np.array or None
If `None`, then a random embedding matrix is constructed.
Otherwise, this should be a 2d array aligned row-wise with
`vocab`, with each row giving the input representation for the
corresponding word. For instance, to roughly duplicate what
is done by default, one could do
`np.array([np.random.randn(h) for _ in vocab])`
where n is the embedding dimensionality (`embed_dim`).
embed_dim : int
Dimensionality of the inputs/embeddings. If `embedding`
is supplied, then this value is set to be the same as its
column dimensionality. Otherwise, this value is used to create
the embedding Tensor (see `_define_embedding`).
max_length : int
Maximum sequence length.
train_embedding : bool
Whether to update the embedding matrix when training.
hidden_activation : tf.nn activation
E.g., tf.nn.relu, tf.nn.relu, tf.nn.selu.
hidden_dim : int
Dimensionality of the hidden layer.
max_iter : int
Maximum number of iterations allowed in training.
eta : float
Learning rate.
tol : float
Stopping criterion for the loss.
"""
def __init__(self,
embedding=None,
train_embedding=True,
max_length = MAX_LENGTH,
# model dimensions
**kwargs):
self.embedding = embedding
self.embed_dim = EMBED_SIZE
self.max_length = max_length
self.train_embedding = train_embedding
super().__init__(**kwargs)
# self.eta = self.config.lr
self.params += [
'embedding', 'embed_dim', 'max_length', 'train_embedding']
def define_embedding(self):
if type(self.embedding) == type(None):
self.embedding = np.random.uniform(size=[self.vocab_size, self.embed_dim],
low=-1.0, high=1.0)
def add_placeholders(self):
"""Generates placeholder variables to represent the input tensors
"""
inputs_batch, lens_batch, labels_batch, self.ids_batch = self.data_manager.batch_op
# inputs_batch, lens_batch, labels_batch, self.ids_batch = tf.map_fn(lambda x: x, self.data_manager.batch_op)
# something = self.data_manager.batch_op
batch_size = tf.shape(inputs_batch)[0]
self.inputs_placeholder = tf.reshape(inputs_batch,
shape=(batch_size, self.max_length, 2), name="inputs") # word ids and case ids
# word_ids, case_ids = tf.split(self.inputs_placeholder,
# num_or_size_splits=2, axis=2)
self.word_ids = self.inputs_placeholder[:, :, 0]
self.case_ids = self.inputs_placeholder[:, :, 1]
self.lens_placeholder = tf.reshape(lens_batch,
shape=(batch_size,), name="inputs_lengths")
self.outputs = tf.reshape(labels_batch,
shape=(batch_size, N_CLASSES), name="labels")
# dropout params
def add_wordvec_features(self):
"""Adds a trainable embedding layer.
Returns:
embeddings: tf.Tensor of shape (None, max_length, n_features*embed_dim)
"""
assert self.embedding.shape[-1] == self.embed_dim
all_embeddings = tf.get_variable('embeddings',
shape=self.embedding.shape,
initializer=tf.constant_initializer(self.embedding),
trainable=self.train_embedding
)
input_embeddings = tf.nn.embedding_lookup(
params=all_embeddings,
ids=self.word_ids
)
embeddings = tf.reshape(input_embeddings,
(-1, self.max_length, self.embed_dim)
)
return embeddings
def add_case_features(self):
return tf.one_hot(self.case_ids, N_CASES)
def get_features(self):
return tf.concat(
[self.add_wordvec_features(), self.add_case_features()],
axis=2
)
def add_prediction_op(self):
self.n_word_features = self.embed_dim + N_CASES
x = self.get_features()
# Take the average word vector
x_avg = tf.reduce_mean(x, axis=1)
W = tf.get_variable('W',
shape=(self.n_word_features, N_CLASSES),
initializer=tf.contrib.layers.xavier_initializer()
)
b = tf.get_variable('b', shape=(N_CLASSES))
preds = tf.matmul(x_avg, W) + b
return preds
def build_graph(self):
self.define_embedding()
self.add_placeholders()
self.model = self.add_prediction_op()
def train_dict(self):
return {self.data_manager.handle: self.data_manager.handle_train}
def dev_dict(self):
return {self.data_manager.handle: self.data_manager.handle_dev}
def test_dict(self):
return {self.data_manager.handle: self.data_manager.handle_test}
# override to use Adam
def get_optimizer(self):
return tf.train.AdamOptimizer(
self.eta).minimize(self.cost, global_step=self.global_step)
def predict_proba(self, init_dm=True, dataset='dev'):
"""Return probabilistic predictions.
Parameters
----------
X : np.array
Returns
-------
np.array of predictions, dimension m x n, where m is the length
of X and n is the number of classes
"""
if not self.sess:
logger.error("model unitnialized, not running batch.")
return
if init_dm:
self.sess.run(self.data_manager.initializer, feed_dict=self.data_manager.get_init_feed_dict(dataset))
return self.sess.run(
[self.probs, self.inputs_placeholder, self.lens_placeholder, self.outputs, self.ids_batch], feed_dict=self.test_dict())
def predict(self, init_dm=True, dataset='dev'):
"""Return classifier predictions, as the class with the
highest probability for each example, for a single batch
Returns
-------
list
"""
probs, inputs, lens, outputs, email_ids = self.predict_proba(init_dm, dataset)
return np.argmax(probs, axis=1), inputs, lens, np.argmax(outputs, axis=1), email_ids
def simple_example():
vocab = ['a', 'b', '$UNK']
train = [
[list('ab'), 'good'],
[list('aab'), 'good'],
[list('abb'), 'good'],
[list('aabb'), 'good'],
[list('ba'), 'bad'],
[list('baa'), 'bad'],
[list('bba'), 'bad'],
[list('bbaa'), 'bad']]
test = [
[list('aaab'), 'good'],
[list('baaa'), 'bad']]
mod = TfLinearClassifier(
vocab=vocab, max_iter=100, max_length=4)
X, y = zip(*train)
mod.fit(X, y)
X_test, _ = zip(*test)
print('\nPredictions:', mod.predict(X_test))
if __name__ == '__main__':
simple_example()