-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
221 lines (189 loc) · 8.69 KB
/
model.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
import sys
sys.path.append('submodule/')
import numpy as np
import tensorflow as tf
import common_attention as ca
import common_layers as cl
# Evaluation metric for multi-label situation
def accuracy4multilabel(labels, predictions):
numerator = tf.reduce_sum(tf.cast(tf.multiply(predictions, labels), tf.float32), axis = -1)
denominator = tf.cast(tf.reduce_sum(predictions + labels, axis = -1), tf.float32) - numerator + 0.000001
accuracy = tf.divide(numerator, denominator)
mean, op = tf.metrics.mean(accuracy)
return mean, op
def attn_net(features, labels, mode, params):
hidden_size = params['hidden_size']
voca_size = params['voca_size']
def residual_fn(x, y):
return cl.layer_norm(x + tf.nn.dropout(
y, 1.0 - params['residual_dropout'] if mode == tf.estimator.ModeKeys.TRAIN else 1))
def embed_op(inputs, params):
if params['embedding'] == None:
embedding = tf.get_variable('embedding', [params['voca_size'], params['hidden_size']], dtype = params['dtype'])
else:
glove = np.load(params['embedding'])
embedding = tf.Variable(glove, trainable = params['embedding_trainable'], name = 'embedding', dtype = tf.float32)
tf.summary.histogram(embedding.name + '/value', embedding)
return tf.nn.embedding_lookup(embedding, inputs)
def conv_op(embd_inp, params):
fltr = tf.get_variable(
'conv_fltr',
params['kernel'],
params['dtype'],
regularizer = tf.contrib.layers.l2_regularizer(1.0)
)
convout = tf.nn.conv1d(embd_inp, fltr, params['stride'], params['conv_pad'])
return convout
def multi_conv_op(embed_inp, params):
x = embed_inp
return convout
def ffn_op(x, params):
out = x
if params['ffn_size'] == None:
ffn_size = []
else:
ffn_size = params['ffn_size']
for unit_size in ffn_size[:-1]:
out = tf.layers.dense(
out,
unit_size,
activation = tf.tanh,
use_bias = True,
kernel_regularizer = tf.contrib.layers.l2_regularizer(1.0)
)
return tf.layers.dense(
out,
params['label_size'],
activation = None,
use_bias = True,
kernel_regularizer = tf.contrib.layers.l2_regularizer(1.0)
)
def transformer_ffn_layer(x, params):
"""Feed-forward layer in the transformer.
Args:
x: a Tensor of shape [batch_size, length, hparams.hidden_size]
hparams: hyperparmeters for model
Returns:
a Tensor of shape [batch_size, length, hparams.hidden_size]
"""
return cl.conv_hidden_relu(
x,
params['filter_size'],
params['hidden_size'],
dropout=params['relu_dropout'] if mode == tf.estimator.ModeKeys.TRAIN else 0)
inputs = features['x']
if params['lexicon_effect'] is not None:
lexicon = features['lexicon']
lexicon = tf.cast(lexicon, tf.float32)
# raw input to embedded input of shape [batch, length, hidden_size]
embd_inp = embed_op(inputs, params)
if params['hidden_size'] != embd_inp.get_shape().as_list()[-1]:
x = tf.layers.dense(
embd_inp,
params['hidden_size'],
activation = None,
use_bias = False,
kernel_regularizer = tf.contrib.layers.l2_regularizer(1.0)
)
else:
x = embd_inp
# attention bias computation
padding = ca.embedding_to_padding(x)
self_attention_bias = ca.attention_bias_ignore_padding(padding)
for layer in xrange(params['num_layers']):
x = residual_fn(
x,
ca.multihead_attention(
query_antecedent = x,
memory_antecedent = None,
bias = self_attention_bias,
total_key_depth = params['hidden_size'],
total_value_depth = params['value_depth'],
output_depth = params['hidden_size'],
num_heads = params['num_heads'],
dropout_rate = params['attn_dropout'] if mode == tf.estimator.ModeKeys.TRAIN else 0,
summaries = False,
image_shapes = None,
name = None
))
# fully connected network
# [batch, length, hparams.ffn_size]
x = residual_fn(x, transformer_ffn_layer(x, params))
logits = list()
for i in range(params['multi_label']):
with tf.variable_scope(None, default_name = 'class'):
# convolution
convout = conv_op(x, params)
channel_out = params['kernel'][-1]
# width_out = (feature_length - windows_size)/stride + 1
# (56 - 10) + 1 = 47
width_out = (x.get_shape().as_list()[1] - params['kernel'][0])/params['stride'] + 1
# pooling over time
convout = tf.reshape(convout, [-1, width_out, 1, channel_out])
pooling = tf.nn.max_pool(convout, [1, width_out, 1, 1], [1,1,1,1], 'VALID')
pooling = tf.reshape(pooling, [-1, channel_out])
if params['lexicon_effect'] == 'nrc1':
lexicon_partial = tf.stack([lexicon[:,i]], axis = -1)
integrate_lexicon = tf.concat([pooling, lexicon_partial], axis = -1)
ffn_out = ffn_op(integrate_lexicon, params)
else:
ffn_out = ffn_op(pooling, params)
logits.append(ffn_out)
# predictions, loss and eval_metric
if len(logits) == 1:
# single label classification
logits = logits[0]
softmax_out = tf.nn.softmax(logits)
predictions = tf.argmax(softmax_out, axis = -1)
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(
mode = mode,
predictions = {'sentiment' : predictions})
labels = tf.cast(labels, tf.int32)
labels = tf.one_hot(labels, params['label_size'])
loss_ce = tf.losses.softmax_cross_entropy(onehot_labels = labels, logits = logits)
eval_metric_ops = {
'accuracy' : tf.metrics.accuracy(tf.argmax(labels, -1), predictions = predictions),
'pearson_all' : tf.contrib.metrics.streaming_pearson_correlation(softmax_out, tf.cast(labels, tf.float32)),
'pearson_some' : tf.contrib.metrics.streaming_pearson_correlation(tf.cast(predictions, tf.float32), tf.cast(tf.argmax(labels, -1), tf.float32))
}
else:
# multi label classification
logits = tf.concat(logits, axis = -1)
if mode != tf.estimator.ModeKeys.TRAIN and type(params['lexicon_effect']) == float:
logits = logits + params['lexicon_effect'] * lexicon
#predictions = tf.cast(tf.round(tf.sigmoid(logits)), tf.int32)
prob = tf.sigmoid(logits)
prob = tf.Print(prob, [prob], 'This is prob')
predictions = tf.to_int32(prob>0.5)
# Provide an estimator spec for 'Modekeys.PREDICT'
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(
mode = mode,
predictions = {"probability" : prob, "sentiment" : predictions})
labels = tf.cast(labels, tf.int32)
eval_metric_ops = {
'accuracy' : accuracy4multilabel(labels, predictions)
}
loss_ce = tf.losses.sigmoid_cross_entropy(labels, logits)
# regularizaiton loss
loss_reg = sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
reg_const = params['regularization'] # Choose an appropriate one.
loss = loss_ce + reg_const * loss_reg
tf.summary.scalar('loss_ce', loss_ce)
tf.summary.scalar('loss_reg', loss_reg)
tf.summary.scalar('loss', loss)
#optimizer = tf.train.GradientDescentOptimizer(learning_rate=params["learning_rate"])
learning_rate = params['learning_rate']
learning_rate = tf.train.exponential_decay(learning_rate, tf.train.get_global_step(), 500, params['decay'], staircase = True)
optimizer = tf.train.AdamOptimizer(learning_rate)
grad_and_var = optimizer.compute_gradients(loss, tf.trainable_variables())
# add histogram summary for gradient
for grad, var in grad_and_var:
tf.summary.histogram(var.name + '/gradient', grad)
train_op = optimizer.apply_gradients(grad_and_var, global_step = tf.train.get_global_step())
return tf.estimator.EstimatorSpec(
mode=mode,
loss=loss,
train_op=train_op,
eval_metric_ops=eval_metric_ops)