如何从神经网络获得准确的预测

时间:2017-11-21 15:41:41

标签: python-2.7 numpy neural-network

我为3输入逻辑AND门的真值表创建了下面的神经网络,但是[1,1,0]的预期输出不正确。输出应该是0.但它预测为0.9意味着约1.因此输出不正确。所以我需要知道的是如何使输出预测更准确。请指导我。

import numpy as np

class NeuralNetwork():
    def __init__(self):

        self.X = np.array([[0, 0, 0],
                          [0, 0, 1],
                          [0, 1, 0],
                          [0, 1, 1],
                          [1, 0, 0],
                          [1, 0, 1],
                          [1, 1, 1]])

        self.y = np.array([[0],
                           [0],
                           [0],
                           [0],
                           [0],
                           [0],
                           [1]])

        np.random.seed(1)

        # randomly initialize our weights with mean 0
        self.syn0 = 2 * np.random.random((3, 4)) - 1
        self.syn1 = 2 * np.random.random((4, 1)) - 1

    def nonlin(self,x, deriv=False):
        if (deriv == True):
            return x * (1 - x)

        return 1 / (1 + np.exp(-x))

    def train(self,steps):
        for j in xrange(steps):

            # Feed forward through layers 0, 1, and 2
            l0 = self.X
            l1 = self.nonlin(np.dot(l0, self.syn0))
            l2 = self.nonlin(np.dot(l1, self.syn1))

            # how much did we miss the target value?
            l2_error = self.y - l2

            if (j % 10000) == 0:
                print "Error:" + str(np.mean(np.abs(l2_error)))

            # in what direction is the target value?
            # were we really sure? if so, don't change too much.
            l2_delta = l2_error * self.nonlin(l2, deriv=True)

            # how much did each l1 value contribute to the l2 error (according to the weights)?
            l1_error = l2_delta.dot(self.syn1.T)

            # in what direction is the target l1?
            # were we really sure? if so, don't change too much.
            l1_delta = l1_error * self.nonlin(l1, deriv=True)

            self.syn1 += l1.T.dot(l2_delta)
            self.syn0 += l0.T.dot(l1_delta)

        print("Output after training:")
        print(l2)

    def predict(self,newInput):
        # Multiply the input with weights and find its sigmoid activation for all layers
        layer0 = newInput
        print("predict -> layer 0 : "+str(layer0))
        layer1 = self.nonlin(np.dot(layer0, self.syn0))
        print("predict -> layer 1 : "+str(layer1))
        layer2 = self.nonlin(np.dot(layer1, self.syn1))
        print("predicted output is : "+str(layer2))




if __name__ == '__main__':
    ann=NeuralNetwork()
    ann.train(100000)
    ann.predict([1,1,0])

输出:

Error:0.48402933124
Error:0.00603525276229
Error:0.00407346660344
Error:0.00325224335386
Error:0.00277628698655
Error:0.00245737222701
Error:0.00222508289674
Error:0.00204641406194
Error:0.00190360175536
Error:0.00178613765229
Output after training:
[[  1.36893057e-04]
 [  5.80758383e-05]
 [  1.19857670e-03]
 [  1.85443483e-03]
 [  2.13949603e-03]
 [  2.19360982e-03]
 [  9.95769492e-01]]
predict -> layer 0 : [1, 1, 0]
predict -> layer 1 : [ 0.00998162  0.91479567  0.00690524  0.05241988]
predicted output is : [ 0.99515547]

2 个答案:

答案 0 :(得分:0)

实际上,它确实产生了正确的输出 - 模型不明确。您的输入数据符合A*B;第三个输入从不的值会影响给定的输出,所以你的模型无法知道它在案例110中应该是重要的。在纯信息理论方面,你不要&# 39; t有输入来强制你想要的结果。

答案 1 :(得分:0)

对于你在AND门中错过的每一个输入,似乎都会发生这种情况。例如,尝试使用[0, 1, 1]替换[1, 1, 0]输入,然后尝试预测[0, 1, 1]它预测接近1的最终值。我尝试了biaseslearning rate,但似乎没有任何效果。

像Prune提到的那样,可能是因为BackPropagation Network无法使用不完整的模型。

为了最大程度地训练您的网络并获得最佳权重,请提供所有可能的输入,即AND门的8个输入。然后,您始终可以获得正确的预测,因为您已经使用这些输入训练了网络,在这种情况下预测可能没有意义。可能是对小型数据集的预测效果不佳。

这只是我的猜测,因为我用于预测的几乎所有网络都拥有相当大的数据集。