-
Notifications
You must be signed in to change notification settings - Fork 0
/
My_bi-lstm
83 lines (69 loc) · 4.15 KB
/
My_bi-lstm
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
import tensorflow as tf
import numpy as np
from tensorflow.contrib import rnn
class MyLSTM(object):
def BiRNN(self, x, dropout, scope, embedding_size, sequence_length):
n_input = embedding_size
n_steps = sequence_length
n_hidden = 100
n_layers = 1
# Prepare data shape to match `bidirectional_rnn` function requirements
# Current data input shape: (batch_size, n_steps, n_input) (?, seq_len, embedding_size)
# Required shape: 'n_steps' tensors list of shape (batch_size, n_input)
# Permuting batch_size and n_steps
x = tf.transpose(x, [1, 0, 2])
# Reshape to (n_steps*batch_size, n_input)
x = tf.reshape(x, [-1, n_input])
# Split to get a list of 'n_steps' tensors of shape (batch_size, n_input)
# x = tf.split(x, n_steps, 0)
x = tf.split(axis=0, num_or_size_splits=n_steps, value=x)
print(x)
with tf.name_scope("fw" + scope), tf.variable_scope("fw" + scope):
print(tf.get_variable_scope().name)
def lstm_fw_cell():
fw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)
return tf.contrib.rnn.DropoutWrapper(fw_cell, output_keep_prob=dropout)
lstm_fw_cell_m = tf.contrib.rnn.MultiRNNCell([lstm_fw_cell() for _ in range(n_layers)],
state_is_tuple=True)
# Backward direction cell
with tf.name_scope("bw"+scope),tf.variable_scope("bw"+scope):
print(tf.get_variable_scope().name)
def lstm_bw_cell():
bw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden, forget_bias=1.0, state_is_tuple=True)
return tf.contrib.rnn.DropoutWrapper(bw_cell, output_keep_prob=dropout)
lstm_bw_cell_m = tf.contrib.rnn.MultiRNNCell([lstm_bw_cell() for _ in range(n_layers)], state_is_tuple=True)
# Get lstm cell output
#try:
with tf.name_scope("bw"+scope), tf.variable_scope("bw"+scope):
outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell_m, lstm_bw_cell_m, x, dtype=tf.float32)
# except Exception: # Old TensorFlow version only returns outputs not states
# outputs = tf.nn.bidirectional_rnn(lstm_fw_cell_m, lstm_bw_cell_m, x,
# dtype=tf.float32)
return outputs
def __init__(
self, sequence_length, embedding_size, hidden_units, class_num, l2_reg_lambda, batch_size):
# Placeholders for input, output and dropout
self.input_x = tf.placeholder(tf.float32, [None, sequence_length, embedding_size], name="input_x")
self.input_y = tf.placeholder(tf.int64, [None], name="input_y")
self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")
# Keeping track of l2 regularization loss (optional)
l2_loss = tf.constant(0.0, name="l2_loss")
# Create a convolution + maxpool layer for each filter size
with tf.name_scope("output"):
self.out1 = self.BiRNN(self.input_x, self.dropout_keep_prob, "side1", embedding_size, sequence_length)
with tf.name_scope("mean_pooling_layer"):
# out_put=tf.reduce_sum(self.out1,0)/(tf.reduce_sum(self.mask_x,0)[:,None])
# 检查维度0还是1
out_put = tf.reduce_mean(self.out1, 0)
with tf.name_scope("Softmax_layer_and_output"):
softmax_w = tf.get_variable("softmax_w",[hidden_units,class_num],dtype=tf.float32)
softmax_b = tf.get_variable("softmax_b",[class_num],dtype=tf.float32)
self.logits = tf.matmul(out_put,softmax_w)+softmax_b
with tf.name_scope("loss"):
self.loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.logits+1e-10,labels=self.input_y)
self.cost = tf.reduce_mean(self.loss)
with tf.name_scope("accuracy"):
self.prediction = tf.argmax(self.logits,1,name='prediction')
correct_prediction = tf.equal(self.prediction,self.input_y)
self.correct_num=tf.reduce_sum(tf.cast(correct_prediction,tf.float32))
self.accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32),name="accuracy")