(self, mode="vanilla", dev=gpu_dev)
| 456 | self.gradients_check(valinna_rnn_forward, param, auto_grad, dev=dev) |
| 457 | |
| 458 | def _gradient_check_cudnn_rnn(self, mode="vanilla", dev=gpu_dev): |
| 459 | seq = 10 |
| 460 | bs = 2 |
| 461 | fea = 10 |
| 462 | hid = 10 |
| 463 | x = np.random.random((seq, bs, fea)).astype(np.float32) |
| 464 | tx = tensor.Tensor(device=dev, data=x) |
| 465 | y = np.random.random((seq, bs, hid)).astype(np.float32) |
| 466 | y = np.reshape(y, (-1, hid)) |
| 467 | ty = tensor.Tensor(device=dev, data=y) |
| 468 | rnn = layer.CudnnRNN(hid, rnn_mode=mode, return_sequences=True) |
| 469 | |
| 470 | def vanilla_rnn_forward(): |
| 471 | out = rnn(tx) |
| 472 | out = autograd.reshape(out, (-1, hid)) |
| 473 | loss = autograd.softmax_cross_entropy(out, ty) |
| 474 | return loss |
| 475 | |
| 476 | loss = vanilla_rnn_forward() |
| 477 | auto_grads = autograd.gradients(loss) |
| 478 | |
| 479 | params = rnn.get_params() |
| 480 | for key, param in params.items(): |
| 481 | auto_grad = tensor.to_numpy(auto_grads[id(param)]) |
| 482 | self.gradients_check(vanilla_rnn_forward, param, auto_grad, dev=dev) |
| 483 | |
| 484 | @unittest.skipIf(not singa_wrap.USE_CUDA, 'CUDA is not enabled') |
| 485 | def test_gradient_check_cudnn_rnn_vanilla(self): |
no test coverage detected