Exemplo n.º 1
0
    def test_neural_net_gradient_regression_2(self):
        X = numpy.abs(numpy.random.randn(10, 2))
        w1 = numpy.array([-0.5, 0.8, -0.6])
        noise = numpy.random.randn(X.shape[0]) / 10
        noise[0] = 0
        noise[1] = 0.07
        X[1, 0] = 0.7
        X[1, 1] = -0.5
        y = w1[0] + X[:, 0] * w1[1] + X[:, 1] * w1[2] + noise

        for act in [
                'relu', 'sigmoid', 'identity', 'leakyrelu', 'sigmoid4', 'expit'
        ]:

            with self.subTest(act=act):
                neu = NeuralTreeNode(w1[1:], bias=w1[0], activation=act)
                loss1 = neu.loss(X, y)
                pred1 = neu.predict(X)
                if act == 'relu':
                    self.assertEqualArray(pred1[1:2], numpy.array([0.36]))
                    pred11 = neu.predict(X)
                    self.assertEqualArray(pred11[1:2], numpy.array([0.36]))

                net = NeuralTreeNet(X.shape[1], empty=True)
                net.append(neu, numpy.arange(0, 2))
                ide = NeuralTreeNode(numpy.array([1], dtype=X.dtype),
                                     bias=numpy.array([0], dtype=X.dtype),
                                     activation='identity')
                net.append(ide, numpy.arange(2, 3))
                pred2 = net.predict(X)
                loss2 = net.loss(X, y)

                self.assertEqualArray(pred1, pred2[:, -1])
                self.assertEqualArray(pred2[:, -2], pred2[:, -1])
                self.assertEqualArray(pred2[:, 2], pred2[:, 3])
                self.assertEqualArray(loss1, loss2)

                for p in range(0, 5):
                    grad1 = neu.gradient(X[p], y[p])
                    grad2 = net.gradient(X[p], y[p])
                    self.assertEqualArray(grad1, grad2[:3])
Exemplo n.º 2
0
    def test_neural_net_gradient_regression(self):
        X = numpy.abs(numpy.random.randn(10, 2))
        w1 = numpy.array([-0.5, 0.8, -0.6])
        noise = numpy.random.randn(X.shape[0]) / 10
        noise[0] = 0
        noise[1] = 0.07
        X[1, 0] = 0.7
        X[1, 1] = -0.5
        y = w1[0] + X[:, 0] * w1[1] + X[:, 1] * w1[2] + noise

        for act in ['identity', 'relu', 'leakyrelu',
                    'sigmoid', 'sigmoid4', 'expit']:
            with self.subTest(act=act):
                neu = NeuralTreeNode(w1[1:], bias=w1[0], activation=act)
                loss1 = neu.loss(X, y)
                grad1 = neu.gradient(X[0], y[0])

                net = NeuralTreeNet(X.shape[1], empty=True)
                net.append(neu, numpy.arange(0, 2))
                loss2 = net.loss(X, y)
                grad2 = net.gradient(X[0], y[0])
                self.assertEqualArray(loss1, loss2)
                self.assertEqualArray(grad1, grad2)