负输入时预测为0.5

时间:2019-06-28 13:56:18

标签: pandas numpy machine-learning

我对机器学习非常陌生,我使用在网上找到的代码创建了一个简单的神经网络,并尝试使用不同的输入来观察会发生什么。使用以下输入(续200个训练示例),网络可以准确预测输出:

in1 in2 in3 in4 in5 in6 out
 0   0   0   0   0   0   0
 1   1   1   1   1   1   1 
 0   0   0   0   0   0   0
 1   1   1   1   1   1   1  

但是当我将0更改为-1时,它会预测应该非常接近1的是1,但是它会预测0.5应该是0。这是怎么回事?

我的代码如下:

import numpy as np
import pandas as pd

def sigmoid(x):
    return 1.0/(1+ np.exp(-x))  

def sigmoid_derivative(x):
    return sigmoid(x) * (1.0 - sigmoid(x))

class NeuralNetwork:
    def __init__(self, x, y):
        self.input      = x
        self.weights1   = np.random.rand(self.input.shape[1],6) 
        self.weights2   = np.random.rand(6,1)                 
        self.y          = y
        self.output     = np.zeros(self.y.shape)

    def feedforward(self):
        self.layer1 = sigmoid(np.dot(self.input, self.weights1))
        self.output = sigmoid(np.dot(self.layer1, self.weights2))

    def backprop(self):
        # application of the chain rule to find derivative of the loss function with respect to weights2 and weights1
        d_weights2 = np.dot(self.layer1.T, (2*(self.y - self.output) * sigmoid_derivative(self.output)))
        d_weights1 = np.dot(self.input.T,  (np.dot(2*(self.y - self.output) * sigmoid_derivative(self.output), self.weights2.T) * sigmoid_derivative(self.layer1)))

        # update the weights with the derivative (slope) of the loss function
        self.weights1 += d_weights1
        self.weights2 += d_weights2


if __name__ == "__main__":
    df = pd.read_csv("/home/drew/Desktop/d.csv")
    X = (np.array(df[['pipsminus1','pips',
                      'TopMinus1','top','BottomMinus1','bottom']]))
    y = np.array(df[['win15']])
    nn = NeuralNetwork(X,y)

    for i in range(10000):
       nn.feedforward()
       nn.backprop()

    newDf = pd.DataFrame(nn.output)
    newDf.to_csv("/home/drew/Desktop/results.csv")

0 个答案:

没有答案