-
Notifications
You must be signed in to change notification settings - Fork 17
/
Lstm_Model.py
155 lines (123 loc) · 7.48 KB
/
Lstm_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
import tensorflow as tf
class TextLSTM(object):
def __init__(self,config):
self.num_steps=config.max_sentences_length
self.hidden_size=config.embedding_size
self.num_classes=config.num_labels
self.num_layers=config.num_layers
self.batch_size=config.batch_size
self.l2_rate=config.l2_rate
self.input_x=tf.placeholder(tf.float32,[None,self.num_steps,self.hidden_size],name="input_x")
self.input_y=tf.placeholder(tf.float32,[None,self.num_classes],name="input_y")
self.dropout_keep_prob=tf.placeholder(tf.float32,name="dropout_keep_prob")
with tf.variable_scope("Net",initializer=tf.orthogonal_initializer()):
def lstm_cell():
return tf.contrib.rnn.BasicLSTMCell(self.hidden_size,forget_bias=1.0,state_is_tuple=True)
attn_cell = lstm_cell
if self.dropout_keep_prob is not None:
def attn_cell():
return tf.contrib.rnn.DropoutWrapper(lstm_cell(),output_keep_prob=self.dropout_keep_prob)
self.cell_fw=tf.contrib.rnn.MultiRNNCell([attn_cell() for _ in range(config.num_layers)],
state_is_tuple=True)
self.cell_bw=tf.contrib.rnn.MultiRNNCell([attn_cell() for _ in range(config.num_layers)],
state_is_tuple=True)
if self.dropout_keep_prob is not None:
inputs=tf.nn.dropout(self.input_x,self.dropout_keep_prob)
else:
inputs=self.input_x
#shape: (batch_size, num_steps,hidden_size) => (num_steps,batch_size,hidden_size)
inputs= tf.transpose(inputs, [1,0,2])
outputs,state = tf.nn.bidirectional_dynamic_rnn(cell_fw=self.cell_fw,
cell_bw=self.cell_bw,
dtype="float32",
inputs=inputs,
swap_memory=True,
time_major=True)
outputs_fw,outputs_bw=outputs
output_fw=outputs_fw[-1]
output_bw=outputs_bw[-1]
finial_output=tf.concat([output_fw,output_bw],1)
with tf.name_scope("output"):
softmax_w=tf.get_variable("softmax_w",[self.hidden_size*2,self.num_classes],dtype=tf.float32)
softmax_b=tf.get_variable("softmax_b",[self.num_classes],dtype=tf.float32,initializer=tf.random_normal_initializer(stddev=0.01))
self.result=tf.matmul(finial_output,softmax_w)+softmax_b
self.final_state=state
self.predictions=tf.argmax(self.result,1,name="predictions")
tf.summary.histogram("softmax_w",softmax_w)
tf.summary.histogram("softmax_b",softmax_b)
self.softmax_result=tf.nn.softmax(self.result)
#计算损失
with tf.name_scope("loss"):
losses=tf.nn.softmax_cross_entropy_with_logits(logits=self.result, labels=self.input_y)
self.loss = tf.reduce_mean(losses)
tf.summary.scalar("loss",self.loss)
#计算正确率
with tf.name_scope("accuracy"):
correct_predictions=tf.equal(self.predictions, tf.argmax(self.input_y, 1))
self.accuracy=tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")
tf.summary.scalar("accuracy",self.accuracy)
with tf.name_scope("train_op"):
tvars = tf.trainable_variables()
self.l2_loss = 0.001 * tf.reduce_sum([tf.nn.l2_loss(v) for v in tvars]) # 0.001是lambda超参数
grads, _ = tf.clip_by_global_norm(tf.gradients(self.loss+self.l2_loss, tvars), config.max_grad_norm)
optimizer = tf.train.AdamOptimizer()
optimizer.apply_gradients(zip(grads, tvars))
self.train_op = optimizer.apply_gradients(zip(grads, tvars))
with tf.name_scope("summary"):
self.summary_op=tf.summary.merge_all()
'''
class TextLSTM(object):
def __init__(self,config):
self.num_steps=config.max_sentences_length
self.hidden_size=config.embedding_size
self.num_classes=config.num_labels
self.num_layers=config.num_layers
self.batch_size=config.batch_size
self.l2_rate=config.l2_rate
self.input_x=tf.placeholder(tf.float32,[None,self.num_steps,self.hidden_size],name="input_x")
self.input_y=tf.placeholder(tf.float32,[None,self.num_classes],name="input_y")
self.dropout_keep_prob=tf.placeholder(tf.float32,name="dropout_keep_prob")
with tf.variable_scope("Net",initializer=tf.orthogonal_initializer()):
def lstm_cell():
return tf.contrib.rnn.BasicLSTMCell(self.hidden_size,forget_bias=1.0,state_is_tuple=True)
attn_cell = lstm_cell
if self.dropout_keep_prob is not None:
def attn_cell():
return tf.contrib.rnn.DropoutWrapper(lstm_cell(),output_keep_prob=self.dropout_keep_prob)
self.cell=tf.contrib.rnn.MultiRNNCell([attn_cell() for _ in range(config.num_layers)],state_is_tuple=True)
if self.dropout_keep_prob is not None:
inputs=tf.nn.dropout(self.input_x,self.dropout_keep_prob)
else:
inputs=self.input_x
#shape: (batch_size, num_steps,hidden_size) => (num_steps,batch_size,hidden_size)
inputs= tf.transpose(inputs, [1,0,2])
outputs,state = tf.nn.dynamic_rnn(cell=self.cell,dtype="float32",inputs=inputs,swap_memory=True,time_major=True)
output=outputs[-1]
with tf.name_scope("output"):
softmax_w=tf.get_variable("softmax_w",[self.hidden_size,self.num_classes],dtype=tf.float32)
softmax_b=tf.get_variable("softmax_b",[self.num_classes],dtype=tf.float32,initializer=tf.random_normal_initializer(stddev=0.01))
self.result=tf.matmul(output,softmax_w)+softmax_b
self.final_state=state
self.predictions=tf.argmax(self.result,1,name="predictions")
tf.summary.histogram("softmax_w",softmax_w)
tf.summary.histogram("softmax_b",softmax_b)
self.softmax_result=tf.nn.softmax(self.result)
#计算损失
with tf.name_scope("loss"):
losses=tf.nn.softmax_cross_entropy_with_logits(logits=self.result, labels=self.input_y)
self.loss = tf.reduce_mean(losses)
tf.summary.scalar("loss",self.loss)
#计算正确率
with tf.name_scope("accuracy"):
correct_predictions=tf.equal(self.predictions, tf.argmax(self.input_y, 1))
self.accuracy=tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")
tf.summary.scalar("accuracy",self.accuracy)
with tf.name_scope("train_op"):
tvars = tf.trainable_variables()
grads, _ = tf.clip_by_global_norm(tf.gradients(self.loss, tvars), config.max_grad_norm)
optimizer = tf.train.AdamOptimizer()
optimizer.apply_gradients(zip(grads, tvars))
self.train_op = optimizer.apply_gradients(zip(grads, tvars))
with tf.name_scope("summary"):
self.summary_op=tf.summary.merge_all()
'''