-
Notifications
You must be signed in to change notification settings - Fork 12
/
model.py
66 lines (54 loc) · 2.67 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
# tensorflow-pos-tagger
# Copyright (C) 2017 Matthew Rahtz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import tensorflow as tf
class Tagger(object):
def __init__(self, vocab_size, embedding_size, n_past_words, n_pos_tags):
print("Initialising model...")
self.input_x = tf.placeholder(
tf.int32, [None, n_past_words + 1], name="input_x")
self.input_y = tf.placeholder(tf.int64, [None], name="input_y")
with tf.name_scope("embedding"):
# Initialise following recommendations from
# https://www.tensorflow.org/get_started/mnist/pros
self.embedding_matrix = tf.Variable(
tf.truncated_normal([vocab_size, embedding_size], stddev=0.1))
with tf.name_scope("model"):
self.word_matrix = \
tf.nn.embedding_lookup(self.embedding_matrix, self.input_x)
# stack the rows
# -1: "figure out the right size"
# (accounts for variable batch size)
new_shape = [-1, (n_past_words + 1) * embedding_size]
self.feature_vector = tf.reshape(self.word_matrix, new_shape)
# one hidden layer
feature_vector_size = int(self.feature_vector.shape[1])
h_size = 100
w1 = tf.Variable(
tf.truncated_normal([feature_vector_size, h_size], stddev=0.1))
self.h = tf.nn.relu(tf.matmul(self.feature_vector, w1))
self.w2 = tf.Variable(
tf.truncated_normal([h_size, n_pos_tags], stddev=0.1))
self.logits = tf.matmul(self.h, self.w2)
self.loss = tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=self.input_y, logits=self.logits))
with tf.name_scope("accuracy"):
# logits has shape [?, n_pos_tags]
self.predictions = tf.argmax(
self.logits, axis=1, name='predictions')
correct_prediction = tf.equal(self.predictions, self.input_y)
self.accuracy = tf.reduce_mean(
tf.cast(correct_prediction, tf.float32))