Skip to content

Commit 55ff922

Browse files
committed
q2 ok
1 parent 2ed84c0 commit 55ff922

11 files changed

+46471
-7
lines changed

data_utils/__init__.pyc

192 Bytes
Binary file not shown.

data_utils/ner.pyc

1.12 KB
Binary file not shown.

data_utils/utils.pyc

9.42 KB
Binary file not shown.

q2_NER.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ def add_placeholders(self):
9292
(Don't change the variable names)
9393
"""
9494
### YOUR CODE HERE
95-
raise NotImplementedError
95+
self.input_placeholder = tf.placeholder(tf.int32, shape=(None , self.config.window_size))
96+
self.labels_placeholder = tf.placeholder(tf.float32, shape=(None , self.config.label_size))
97+
self.dropout_placeholder = tf.placeholder(tf.float32)
9698
### END YOUR CODE
9799

98100
def create_feed_dict(self, input_batch, dropout, label_batch=None):
@@ -117,7 +119,11 @@ def create_feed_dict(self, input_batch, dropout, label_batch=None):
117119
feed_dict: The feed dictionary mapping from placeholders to values.
118120
"""
119121
### YOUR CODE HERE
120-
raise NotImplementedError
122+
assert input_batch is not None
123+
if label_batch is None:
124+
feed_dict = {self.input_placeholder:input_batch, self.dropout_placeholder:dropout}
125+
else:
126+
feed_dict = {self.input_placeholder:input_batch, self.labels_placeholder:label_batch, self.dropout_placeholder:dropout}
121127
### END YOUR CODE
122128
return feed_dict
123129

@@ -148,7 +154,8 @@ def add_embedding(self):
148154
# The embedding lookup is currently only implemented for the CPU
149155
with tf.device('/cpu:0'):
150156
### YOUR CODE HERE
151-
raise NotImplementedError
157+
embeddings = tf.Variable(tf.random_uniform([len(self.wv), self.config.embed_size], -1.0, 1.0))
158+
window = tf.reshape(tf.nn.embedding_lookup(embeddings, self.input_placeholder), [-1, self.config.window_size*self.config.embed_size])
152159
### END YOUR CODE
153160
return window
154161

@@ -180,7 +187,22 @@ def add_model(self, window):
180187
output: tf.Tensor of shape (batch_size, label_size)
181188
"""
182189
### YOUR CODE HERE
183-
raise NotImplementedError
190+
xavier_initializer = xavier_weight_init()
191+
192+
with tf.variable_scope("Layer"):
193+
W = tf.get_variable("W", initializer=xavier_initializer([self.config.window_size*self.config.embed_size, self.config.hidden_size]))
194+
b1 = tf.get_variable("b1", [self.config.hidden_size])
195+
h = tf.tanh(tf.matmul(window, W) + b1)
196+
197+
with tf.variable_scope("Softmax"):
198+
U = tf.get_variable("U", initializer=xavier_initializer([self.config.hidden_size, self.config.label_size]))
199+
b2 = tf.get_variable("b2", [self.config.label_size])
200+
output_bf_dropout = tf.matmul(h, U) + b2
201+
202+
regularization_loss = self.config.l2 * (tf.nn.l2_loss(W) + tf.nn.l2_loss(U))
203+
tf.add_to_collection("total_loss", regularization_loss)
204+
205+
output = tf.nn.dropout(output_bf_dropout, self.dropout_placeholder)
184206
### END YOUR CODE
185207
return output
186208

@@ -195,7 +217,8 @@ def add_loss_op(self, y):
195217
loss: A 0-d tensor (scalar)
196218
"""
197219
### YOUR CODE HERE
198-
raise NotImplementedError
220+
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, self.labels_placeholder))
221+
loss += tf.get_collection("total_loss")[-1]
199222
### END YOUR CODE
200223
return loss
201224

@@ -219,7 +242,7 @@ def add_training_op(self, loss):
219242
train_op: The Op for training.
220243
"""
221244
### YOUR CODE HERE
222-
raise NotImplementedError
245+
train_op = tf.train.AdamOptimizer(learning_rate=self.config.lr).minimize(loss)
223246
### END YOUR CODE
224247
return train_op
225248

q2_initialization.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ def _xavier_initializer(shape, **kwargs):
2424
out: tf.Tensor of specified shape sampled from Xavier distribution.
2525
"""
2626
### YOUR CODE HERE
27-
raise NotImplementedError
27+
if len(shape) == 1:
28+
epsilon = np.sqrt(6/(shape[0]+1))
29+
else:
30+
epsilon = np.sqrt(6/(shape[0]+shape[1]))
31+
out = tf.random_uniform(shape, minval=-epsilon, maxval=epsilon, dtype=tf.float32)
2832
### END YOUR CODE
2933
return out
3034
# Returns defined initializer function.

q2_initialization.pyc

2.72 KB
Binary file not shown.

0 commit comments

Comments
 (0)