def test_forward_pass(self): t = 2 # time b = 1 # batches n = 5 # input size m = 3 # output size l = pw.create_layer("LstmLayer", n, m) X = pw.Matrix(t, b, n) Y = pw.Matrix(t, b, m) wm = pw.Matrix(1, 1, l.get_parameter_size()) W = l.create_param_view(wm) im = pw.Matrix(t, b, l.get_fwd_state_size(t, b)) I = l.create_fwd_state(im, t, b) l.forward(W, I, X, Y, False)
def r_forward_pass(self, input_buffer, v_buffer): # determine dimensions and set buffer managers accordingly t, b, f = input_buffer.shape assert f == self.layers.values()[0].out_size self.set_buffer_manager_dimensions(t, b) # inject the v value if isinstance(v_buffer, pw.Matrix): self.v_manager.initialize_buffer(v_buffer) else: self.v_manager.initialize_buffer(pw.Matrix(v_buffer)) # inject the input buffer self.in_buffer[:] = input_buffer # set r input to 0 self.r_in_out_manager.get_source_view("InputLayer").as_array()[:] = 0.0 # execute all the intermediate layers for n, l in self.layers.items()[1:]: param = self.param_manager.get_source_view(n) v = self.v_manager.get_source_view(n) fwd_state = self.fwd_state_manager.get_source_view(n) r_fwd_state = self.r_fwd_state_manager.get_source_view(n) out = self.in_out_manager.get_source_view(n) r_in = self.r_in_out_manager.get_sink_view(n) r_out = self.r_in_out_manager.get_source_view(n) input_view = self.in_out_manager.get_sink_view(n) l.Rpass(param, v, fwd_state, r_fwd_state, input_view, out, r_in, r_out) # read the output buffer return self.r_out_buffer
def test_create_output_view_with_single_sample(self): l = pw.create_layer("LstmLayer", 3, 7) im = pw.Matrix(l.out_size) I = l.create_output_view(im) self.assertIsNotNone(I) self.assertEqual(len(I), 7) self.assertEqual(I.shape(), (1, 1, 7))
def test_create_input_view_with_single_sample(self): l = pw.create_layer("ForwardLayer", 3, 7) im = pw.Matrix(l.in_size) I = l.create_input_view(im) self.assertIsNotNone(I) self.assertEqual(len(I), 3) self.assertEqual(I.shape(), (1, 1, 3))
def test_create_output_view_with_1d_buffer(self): l = pw.create_layer("LstmLayer", 3, 7) t = 9 b = 5 im = pw.Matrix(l.get_output_buffer_size(t, b)) I = l.create_output_view(im, t, b) self.assertIsNotNone(I) self.assertEqual(len(I), 7 * t * b) self.assertEqual(I.shape(), (t, b, 7))
def test_create_input_view_with_3d_buffer(self): l = pw.create_layer("LstmLayer", 3, 7) t = 9 b = 5 im = pw.Matrix(t, b, l.in_size) I = l.create_input_view(im, t, b) self.assertIsNotNone(I) self.assertEqual(len(I), 3 * t * b) self.assertEqual(I.shape(), (t, b, 3))
def test_create_output_view_with_3d_buffer(self): l = pw.create_layer("ForwardLayer", 3, 7) t = 9 b = 5 im = pw.Matrix(t, b, l.out_size) I = l.create_output_view(im, t, b) self.assertIsNotNone(I) self.assertEqual(len(I), 7 * t * b) self.assertEqual(I.shape(), (t, b, 7))
def test_create_fwd_state(self): l = pw.create_layer("LstmLayer", 3, 7) im = pw.Matrix(l.get_fwd_state_size()) I = l.create_fwd_state(im) self.assertIsNotNone(I)
def test_create_param_view(self): l = pw.create_layer("LstmLayer", 3, 7) wm = pw.Matrix(l.get_parameter_size()) W = l.create_param_view(wm) self.assertIsNotNone(W)