def test_get_node(self): fc1 = Linear(4, 2)() fc2 = Linear(4, 2)() fc1.element().set_name("fc1") cadd = CAddTable()([fc1, fc2]) output1 = ReLU()(cadd) model = Model([fc1, fc2], [output1]) res = model.node("fc1") assert res.element().name() == "fc1"
def test_create_node(self): import numpy as np fc1 = Linear(4, 2)() fc2 = Linear(4, 2)() cadd = CAddTable()([fc1, fc2]) output1 = ReLU()(cadd) model = Model([fc1, fc2], [output1]) fc1.element().set_weights([np.ones((4, 2)), np.ones((2,))]) fc2.element().set_weights([np.ones((4, 2)), np.ones((2,))]) output = model.forward([np.array([0.1, 0.2, -0.3, -0.4]), np.array([0.5, 0.4, -0.2, -0.1])]) assert_allclose(output, np.array([2.2, 2.2]))
def test_graph_backward(self): fc1 = Linear(4, 2)() fc2 = Linear(4, 2)() cadd = CAddTable()([fc1, fc2]) output1 = ReLU()(cadd) output2 = Threshold(10.0)(cadd) model = Model([fc1, fc2], [output1, output2]) fc1.element().set_weights([np.ones((4, 2)), np.ones((2,))]) fc2.element().set_weights([np.ones((4, 2)) * 2, np.ones((2,)) * 2]) output = model.forward([np.array([0.1, 0.2, -0.3, -0.4]), np.array([0.5, 0.4, -0.2, -0.1])]) gradInput = model.backward([np.array([0.1, 0.2, -0.3, -0.4]), np.array([0.5, 0.4, -0.2, -0.1])], [np.array([1.0, 2.0]), np.array([3.0, 4.0])]) assert_allclose(gradInput[0], np.array([3.0, 3.0, 3.0, 3.0])) assert_allclose(gradInput[1], np.array([6.0, 6.0, 6.0, 6.0]))