def test_sum_2D(self): data = np.random.randn(2, 3) parameter = Parameter(shape=(2, 3), init_weight=data) out = autograd.sum(parameter, axis=0) model = Model(input=parameter, output=out) result = model.forward(data) np.testing.assert_almost_equal(result, data.sum(axis=0), decimal=5)
def test_slice_2D(self): data = np.random.randn(2, 3) parameter = Parameter(shape=(2, 3), init_weight=data) out = parameter.slice(0, 0, 2) model = Model(input=parameter, output=out) result = model.forward(data) np.testing.assert_almost_equal(result, data[0:2], decimal=5)
def compare_binary_op(self, kk_func, z_layer, shape): x = klayers.Input(shape=shape[0][1:]) y = klayers.Input(shape=shape[1][1:]) batch = shape[0][0] kkresult = kk_func(x, y) x_value = np.random.uniform(0, 1, shape[0]) y_value = np.random.uniform(0, 1, shape[1]) k_grad_y_pred = KK.get_session().run(KK.gradients(kkresult, [x, y]), feed_dict={ x: x_value, y: y_value }) k_output = KK.get_session().run(kkresult, feed_dict={ x: x_value, y: y_value }) inputs = [Input(s) for s in remove_batch(shape)] model = Model(inputs, z_layer(inputs)) z_output = model.forward([x_value, y_value]) grad_output = np.array(z_output) grad_output.fill(1.0) z_grad_y_pred = model.backward(x_value, grad_output) self.assert_allclose(z_output, k_output) [ self.assert_allclose(z, k) for (z, k) in zip(z_grad_y_pred, k_grad_y_pred) ]
def test_ExpandDim(self): inputdata = np.array([2, 1, 6]) input = Parameter(shape=(3, ), init_weight=inputdata) expand = ExpandDim(dim=0)(input) model = Model(input, expand) assert model.get_output_shape() == (1, 3) desired = inputdata.reshape(1, 3) outputdata = model.forward(inputdata) np.testing.assert_almost_equal(outputdata, desired, decimal=4)
def test_unsqueeze_1D(self): data = np.random.randn(4, ) parameter = Parameter(shape=(4, ), init_weight=data) out = autograd.expand_dims(parameter, axis=0) model = Model(input=parameter, output=out) result = model.forward(data) np.testing.assert_almost_equal(result, np.expand_dims(data, axis=0), decimal=5)
def compare_binary_op(self, kk_func, z_layer, shape, rtol=1e-5, atol=1e-5): x = klayers.Input(shape=shape[0][1:]) y = klayers.Input(shape=shape[1][1:]) batch = shape[0][0] kkresult = kk_func(x, y) x_value = np.random.uniform(0, 1, shape[0]) y_value = np.random.uniform(0, 1, shape[1]) k_grads = KK.get_session().run(KK.gradients(kkresult, [x, y]), feed_dict={ x: x_value, y: y_value }) k_output = KK.get_session().run(kkresult, feed_dict={ x: x_value, y: y_value }) inputs = [Input(s) for s in remove_batch(shape)] model = Model(inputs, z_layer(inputs)) z_output = model.forward([x_value, y_value]) grad_output = np.array(z_output) grad_output.fill(1.0) z_grads = model.backward([x_value, y_value], grad_output) # Check if the model can be forward/backward multiple times or not z_output2 = model.forward([x_value, y_value]) z_grads2 = model.backward([x_value, y_value], grad_output) self.assert_allclose(z_output, z_output2, rtol, atol) [ self.assert_allclose(z, k, rtol, atol) for (z, k) in zip(z_grads, z_grads2) ] self.assert_allclose(z_output, k_output, rtol, atol) [ self.assert_allclose(z, k, rtol, atol) for (z, k) in zip(z_grads, k_grads) ]
def test_expose_node(self): image_shape = [3, 16, 16] input_shape = [2] + image_shape input = Input(shape=input_shape, name="input1") def l1(x): x1 = x.index_select(1, 0) # input is [B, 2, 3, 16, 16] x2 = x.index_select(1, 0) return abs(x1 - x2) output = Lambda(function=l1)(input) model = Model(input, output) mock_data = np.random.uniform(0, 1, [10] + input_shape) out_data = model.forward(mock_data) assert out_data.shape == (10, 3, 16, 16)