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

Method _rnn_helper

test/python/test_api.py:784–846  ·  view source on GitHub ↗
(self, mode)

Source from the content-addressed store, hash-verified

782 self._rnn_helper(3)
783
784 def _rnn_helper(self, mode):
785 dev = gpu_dev
786
787 hidden_size = 7
788 seq_length = 5
789 batch_size = 6
790 feature_size = 3
791 directions = 2
792 num_layers = 2
793
794 x = tensor.Tensor(shape=(seq_length, batch_size, feature_size),
795 device=dev).gaussian(0, 1)
796 hx = tensor.Tensor(shape=(num_layers * directions, batch_size,
797 hidden_size),
798 device=dev).gaussian(0, 1)
799 cx = tensor.Tensor(shape=(num_layers * directions, batch_size,
800 hidden_size),
801 device=dev).gaussian(0, 1)
802
803 rnn_handle = singa_api.CudnnRNNHandle(x.data,
804 hidden_size,
805 mode,
806 num_layers=num_layers,
807 dropout=0.1,
808 bidirectional=1)
809
810 w = tensor.Tensor(shape=(rnn_handle.weights_size,),
811 device=dev).gaussian(0, 1)
812 # print("weights size is ", rnn_handle.weights_size)
813
814 (y, hy, cy) = singa_api.GpuRNNForwardTraining(x.data, hx.data, cx.data,
815 w.data, rnn_handle)
816 self.assertEqual(y.shape(),
817 (seq_length, batch_size, directions * hidden_size))
818 self.assertEqual(hy.shape(), hx.shape)
819 self.assertEqual(cy.shape(), cx.shape)
820
821 (y2, hy2,
822 cy2) = singa_api.GpuRNNForwardInference(x.data, hx.data, cx.data,
823 w.data, rnn_handle)
824 self.assertEqual(y2.shape(),
825 (seq_length, batch_size, directions * hidden_size))
826 self.assertEqual(hy2.shape(), hx.shape)
827 self.assertEqual(cy2.shape(), cx.shape)
828
829 dy = tensor.Tensor(shape=(seq_length, batch_size,
830 directions * hidden_size),
831 device=dev).gaussian(0, 1)
832 dhy = tensor.Tensor(shape=(num_layers * directions, batch_size,
833 hidden_size),
834 device=dev).gaussian(0, 1)
835 dcy = tensor.Tensor(shape=(num_layers * directions, batch_size,
836 hidden_size),
837 device=dev).gaussian(0, 1)
838
839 (dx, dhx, dcx) = singa_api.GpuRNNBackwardx(y, dy.data, dhy.data,
840 dcy.data, w.data, hx.data,
841 cx.data, rnn_handle)

Callers 4

test_rnn_reluMethod · 0.95
test_rnn_tanhMethod · 0.95
test_rnn_lstmMethod · 0.95
test_rnn_gruMethod · 0.95

Calls 4

TensorMethod · 0.80
CudnnRNNHandleMethod · 0.80
shapeMethod · 0.80
gaussianMethod · 0.45

Tested by

no test coverage detected