提交 5dcaf543 编写于 作者: xujinanne's avatar xujinanne

change networks in lstm, modify parameters

上级 59132ca3
...@@ -147,10 +147,8 @@ def train(): ...@@ -147,10 +147,8 @@ def train():
total_cost, total_acc, total_num_seqs = [], [], [] total_cost, total_acc, total_num_seqs = [], [], []
last_hidden = None last_hidden = None
last_cell = None last_cell = None
init_hidden_data = np.zeros( init_hidden_data = np.zeros((1, args.batch_size, 128), dtype='float32')
(1, args.batch_size, 128 * 4), dtype='float32') init_cell_data = np.zeros((1, args.batch_size, 128), dtype='float32')
init_cell_data = np.zeros(
(1, args.batch_size, 128 * 4), dtype='float32')
for eop in range(args.epoch): for eop in range(args.epoch):
time_begin = time.time() time_begin = time.time()
for batch_id, data in enumerate(train_data_generator()): for batch_id, data in enumerate(train_data_generator()):
......
...@@ -246,7 +246,7 @@ class LSTM(fluid.dygraph.Layer): ...@@ -246,7 +246,7 @@ class LSTM(fluid.dygraph.Layer):
self.hid_dim = 128 self.hid_dim = 128
self.fc_hid_dim = 96 self.fc_hid_dim = 96
self.class_dim = 2 self.class_dim = 2
self.lstm_num_steps = 1 self.lstm_num_steps = 60
self.lstm_num_layers = 1 self.lstm_num_layers = 1
self.batch_size = batch_size self.batch_size = batch_size
self.seq_len = seq_len self.seq_len = seq_len
...@@ -256,16 +256,14 @@ class LSTM(fluid.dygraph.Layer): ...@@ -256,16 +256,14 @@ class LSTM(fluid.dygraph.Layer):
dtype='float32', dtype='float32',
param_attr=fluid.ParamAttr(learning_rate=30), param_attr=fluid.ParamAttr(learning_rate=30),
is_sparse=False) is_sparse=False)
self._fc1 = FC(self.full_name(), self._fc1 = FC(self.full_name(), size=self.hid_dim, num_flatten_dims=2)
size=self.hid_dim * 4,
num_flatten_dims=2)
self._fc2 = FC(self.full_name(), size=self.fc_hid_dim, act="tanh") self._fc2 = FC(self.full_name(), size=self.fc_hid_dim, act="tanh")
self._fc_prediction = FC(self.full_name(), self._fc_prediction = FC(self.full_name(),
size=self.class_dim, size=self.class_dim,
act="softmax") act="softmax")
self.simple_lstm_rnn = SimpleLSTMRNN( self.simple_lstm_rnn = SimpleLSTMRNN(
self.full_name(), self.full_name(),
self.hid_dim * 4, self.hid_dim,
num_steps=self.lstm_num_steps, num_steps=self.lstm_num_steps,
num_layers=self.lstm_num_layers, num_layers=self.lstm_num_layers,
init_scale=0.1, init_scale=0.1,
...@@ -277,18 +275,16 @@ class LSTM(fluid.dygraph.Layer): ...@@ -277,18 +275,16 @@ class LSTM(fluid.dygraph.Layer):
mask_emb = fluid.layers.expand( mask_emb = fluid.layers.expand(
to_variable(o_np_mask), [1, self.hid_dim]) to_variable(o_np_mask), [1, self.hid_dim])
emb = emb * mask_emb emb = emb * mask_emb
emb = fluid.layers.reshape( emb = fluid.layers.reshape(emb, shape=[-1, self.seq_len, self.hid_dim])
emb, shape=[-1, 1, self.seq_len, self.hid_dim])
emb = fluid.layers.reduce_max(emb, dim=1)
fc_1 = self._fc1(emb) fc_1 = self._fc1(emb)
init_h = fluid.layers.reshape( init_h = fluid.layers.reshape(
init_hidden, shape=[self.lstm_num_layers, -1, self.hid_dim * 4]) init_hidden, shape=[self.lstm_num_layers, -1, self.hid_dim])
init_c = fluid.layers.reshape( init_c = fluid.layers.reshape(
init_cell, shape=[self.lstm_num_layers, -1, self.hid_dim * 4]) init_cell, shape=[self.lstm_num_layers, -1, self.hid_dim])
real_res, last_hidden, last_cell = self.simple_lstm_rnn(fc_1, init_h, real_res, last_hidden, last_cell = self.simple_lstm_rnn(fc_1, init_h,
init_c) init_c)
last_hidden = fluid.layers.reshape( last_hidden = fluid.layers.reshape(
last_hidden, shape=[-1, self.hid_dim * 4]) last_hidden, shape=[-1, self.hid_dim])
tanh_1 = fluid.layers.tanh(last_hidden) tanh_1 = fluid.layers.tanh(last_hidden)
fc_2 = self._fc2(tanh_1) fc_2 = self._fc2(tanh_1)
prediction = self._fc_prediction(fc_2) prediction = self._fc_prediction(fc_2)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册