コード例 #1
0
    def test_tanh(self) -> None:
        np.random.seed(0)
        t1 = Tensor(np.ones(shape=(3, 3, 3)) * 1_000, requires_grad=True)
        t2 = tanh(t1)

        assert t2.shape == (3, 3, 3)
        np.testing.assert_allclose(t2.data, 1)
コード例 #2
0
    def test_tanh_grad(self) -> None:
        np.random.seed(0)
        t1 = Tensor(np.ones(shape=(3, 3, 3)) * 1_000, requires_grad=True)
        t2 = tanh(t1)
        t2.backward(1)

        np.testing.assert_allclose(t1.grad, 0)
コード例 #3
0
    def test_tanh_no_grad(self) -> None:
        t1 = Tensor(np.zeros(shape=(3, 3, 3)), requires_grad=False)
        t2 = tanh(t1)

        assert t2.shape == (3, 3, 3)
        assert not t2.requires_grad
コード例 #4
0
 def predict(self, x: Tensor) -> Tensor:
     x = x @ self.layer1 + self.bias1
     x = tanh(x)
     x = x @ self.layer2 + self.bias2
     return x