def test_001_single_layer_tanh(self): network = nn.FeedForward(self._NFEATURES, self._SIZE, has_bias=True, fun=activation.tanh) nn.init_(network.linear.weight, self.W) nn.init_(network.linear.bias, self.b) with torch.no_grad(): y = network(torch.tensor(self.x)).numpy() np.testing.assert_almost_equal(y, np.tanh(self.res), decimal=5)
def test_004_reverse(self): network1 = nn.FeedForward(self._NFEATURES, self._SIZE, has_bias=True) nn.init_(network1.linear.weight, self.W) nn.init_(network1.linear.bias, self.b) network2 = nn.Reverse(network1) with torch.no_grad(): res1 = network1(torch.tensor(self.x)).numpy() res2 = network2(torch.tensor(self.x)).numpy() np.testing.assert_almost_equal(res1, res2, decimal=5)
def test_002_parallel_layers(self): l1 = nn.FeedForward(self._NFEATURES, self._SIZE, has_bias=True) nn.init_(l1.linear.weight, self.W) nn.init_(l1.linear.bias, self.b) l2 = nn.FeedForward(self._NFEATURES, self._SIZE, has_bias=True) nn.init_(l2.linear.weight, self.W) nn.init_(l2.linear.bias, self.b) network = nn.Parallel([l1, l2]) with torch.no_grad(): res = network(torch.tensor(self.x)).numpy() np.testing.assert_almost_equal(res[:, :, :self._SIZE], res[:, :, self._SIZE:])
def test_005_poormans_birnn(self): layer1 = nn.FeedForward(self._NFEATURES, self._SIZE, has_bias=True) nn.init_(layer1.linear.weight, self.W) nn.init_(layer1.linear.bias, self.b) layer2 = nn.FeedForward(self._NFEATURES, self._SIZE, has_bias=True) nn.init_(layer2.linear.weight, self.W) nn.init_(layer2.linear.bias, self.b) network = nn.birnn(layer1, layer2) with torch.no_grad(): res = network(torch.tensor(self.x)).numpy() np.testing.assert_almost_equal(res[:, :, :self._SIZE], res[:, :, self._SIZE:], decimal=5)
def test_003_simple_serial(self): W2 = np.random.normal(size=(self._SIZE, self._SIZE)).astype(taiyaki_dtype) res = self.res.dot(W2.transpose()) l1 = nn.FeedForward(self._NFEATURES, self._SIZE, has_bias=True, fun=activation.linear) nn.init_(l1.linear.weight, self.W) nn.init_(l1.linear.bias, self.b) l2 = nn.FeedForward(self._SIZE, self._SIZE, fun=activation.linear, has_bias=False) nn.init_(l2.linear.weight, W2) network = nn.Serial([l1, l2]) with torch.no_grad(): y = network(torch.tensor(self.x)).numpy() np.testing.assert_almost_equal(y, res, decimal=4)