MCPcopy Create free account
hub / github.com/apache/singa / test_rnn_with_seq_lengths

Method test_rnn_with_seq_lengths

test/python/test_model.py:171–202  ·  view source on GitHub ↗
(self, dev=gpu_dev)

Source from the content-addressed store, hash-verified

169
170 @unittest.skipIf(not singa_api.USE_CUDA, 'CUDA is not enabled')
171 def test_rnn_with_seq_lengths(self, dev=gpu_dev):
172 bs = 2
173 seq_length = 3
174 hidden_size = 2
175 em_size = 2
176 x_np = np.array([[[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]],
177 [[0.3, 0.3], [0.4, 0.4], [0.0,
178 0.0]]]).astype(np.float32)
179 y_np = np.array([[0.4, 0.4], [0.5, 0.5]]).astype(np.float32)
180 seq_lengths_np = np.array([3, 2]).astype(np.int32)
181
182 x = tensor.from_numpy(x_np)
183 x.to_device(dev)
184 y = tensor.from_numpy(y_np)
185 y.to_device(dev)
186 seq_lengths = tensor.from_numpy(seq_lengths_np)
187
188 m = LSTMModel3(hidden_size)
189 m.compile([x, seq_lengths],
190 is_train=True,
191 use_graph=False,
192 sequential=False)
193 m.train()
194 for i in range(10):
195 out = m.forward(x, seq_lengths)
196 loss = autograd.mse_loss(out, y)
197 print("train l:", tensor.to_numpy(loss))
198 m.optimizer(loss)
199 m.eval()
200 out = m.forward(x, seq_lengths)
201 loss = autograd.mse_loss(out, y)
202 print(" eval l:", tensor.to_numpy(loss))
203
204 @unittest.skipIf(not singa_api.USE_CUDA, 'CUDA is not enabled')
205 def test_lstm_model(self, dev=gpu_dev):

Callers

nothing calls this directly

Calls 6

forwardMethod · 0.95
LSTMModel3Class · 0.85
to_deviceMethod · 0.45
compileMethod · 0.45
trainMethod · 0.45
evalMethod · 0.45

Tested by

no test coverage detected