def test_numerical_gradients_check_for_vallina_rnn(self): inputs, target, h0 = prepare_inputs_targets_for_rnn_test() rnn = autograd.RNN(3, 2) def valinna_rnn_forward(): hs, _ = rnn(inputs, h0) loss = autograd.softmax_cross_entropy(hs[0], target[0]) for i in range(1, len(hs)): l = autograd.softmax_cross_entropy(hs[i], target[i]) loss = autograd.add(loss, l) #grads = autograd.gradients(loss) return loss loss1 = valinna_rnn_forward() auto_grads = autograd.gradients(loss1) for param in rnn.params: auto_grad = tensor.to_numpy(auto_grads[param]) self.gradients_check(valinna_rnn_forward, param, auto_grad)
def test_numerical_gradients_check_for_lstm(self): inputs, target, h0 = prepare_inputs_targets_for_rnn_test() c_0 = np.zeros((2, 2)).astype(np.float32) c0 = tensor.Tensor(device=gpu_dev, data=c_0) rnn = autograd.LSTM(3, 2) def lstm_forward(): hs, _, _ = rnn(inputs, (h0, c0)) loss = autograd.softmax_cross_entropy(hs[0], target[0]) for i in range(1, len(hs)): l = autograd.softmax_cross_entropy(hs[i], target[i]) loss = autograd.add(loss, l) return loss loss1 = lstm_forward() auto_grads = autograd.gradients(loss1) for param in rnn.params: auto_grad = tensor.to_numpy(auto_grads[param]) self.gradients_check(lstm_forward, param, auto_grad)