Python 神经网络总是输出接近0.5的值

Python 神经网络总是输出接近0.5的值,python,machine-learning,neural-network,Python,Machine Learning,Neural Network,这是我的代码,它不断给出接近0.5的输出。为什么会发生这种情况?我该如何解决?通常,输出约为0.45至0.55。请给我具体的地方,它可以修改,以提供所需的输出。顺便说一句,这段代码的所有功劳都归Tariq Rashd的“制作你自己的神经网络”。我正试图将他的神经网络类实现到我创建的玩具数据集中。只要在那里进行更多的迭代EPOCHS=50,它就会工作 但是当我说更多的迭代时,我不是说55或60,我是说10000,我认为你的神经网络预测的是输入数据的平均值,因为你输出的是0或1。如果数据中没有实际的

这是我的代码,它不断给出接近0.5的输出。为什么会发生这种情况?我该如何解决?通常,输出约为0.45至0.55。请给我具体的地方,它可以修改,以提供所需的输出。顺便说一句,这段代码的所有功劳都归Tariq Rashd的“制作你自己的神经网络”。我正试图将他的神经网络类实现到我创建的玩具数据集中。

只要在那里进行更多的迭代
EPOCHS=50
,它就会工作
但是当我说更多的迭代时,我不是说55或60,我是说10000,我认为你的神经网络预测的是输入数据的平均值,因为你输出的是0或1。如果数据中没有实际的模式,那么它只会简单地预测平均值,因为这是它能给出的最佳答案。我不同意数据没有任何模式这一事实。我画出了点,如果目标为1,点为红色,如果目标为0,点为蓝色,MatplotLib对于3,1.5,答案为1,但是对于3,1答案为0。对于这样一个虚拟网络,这是一个相当混乱的模式。此外,您的输出误差实际上从未减少,因此预测将在平均值附近。
import numpy
import scipy.special
import random

data = [[3, 1.5, 1], [2, 1, 0], [4, 1.5, 1], [3, 1, 0], [3.5, 0.5, 1], [2, 0.5, 0], [5.5, 1, 1], [1, 1, 0]]

# neural network class definition
class neuralNetwork:


    # initialise the neural network
    def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
        # set number of nodes in each input, hidden, output layer
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes

        # link weight matrices, wih and who
        # weights inside the arrays are w_i_j, where link is from node i to node j in the next layer
        # w11 w21
        # w12 w22 etc 
        self.wih = numpy.random.normal(0.0, pow(self.inodes, -0.5), (self.hnodes, self.inodes))
        self.who = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.onodes, self.hnodes))

        # learning rate
        self.lr = learningrate

        # activation function is the sigmoid function
        self.activation_function = lambda x: scipy.special.expit(x)


    # train the neural network
    def train(self, inputs_list, targets_list):
        # convert inputs list to 2d array
        inputs = numpy.array(inputs_list, ndmin=2).T
        targets = numpy.array(targets_list, ndmin=2).T

        # calculate signals into hidden layer
        hidden_inputs = numpy.dot(self.wih, inputs)
        # calculate the signals emerging from hidden layer
        hidden_outputs = self.activation_function(hidden_inputs)

        # calculate signals into final output layer
        final_inputs = numpy.dot(self.who, hidden_outputs)
        # calculate the signals emerging from final output layer
        final_outputs = self.activation_function(final_inputs)

        # output layer error is the (target - actual)
        output_errors = targets - final_outputs
        # hidden layer error is the output_errors, split by weights, recombined at hidden nodes
        hidden_errors = numpy.dot(self.who.T, output_errors) 

        # update the weights for the links between the hidden and output layers
        self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)), numpy.transpose(hidden_outputs))

        # update the weights for the links between the input and hidden layers
        self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), numpy.transpose(inputs))


    # query the neural network
    def query(self, inputs_list):
        # convert inputs list to 2d array
        inputs = numpy.array(inputs_list, ndmin=2).T

        # calculate signals into hidden layer
        hidden_inputs = numpy.dot(self.wih, inputs)
        # calculate the signals emerging from hidden layer
        hidden_outputs = self.activation_function(hidden_inputs)

        # calculate signals into final output layer
        final_inputs = numpy.dot(self.who, hidden_outputs)
        # calculate the signals emerging from final output layer
        final_outputs = self.activation_function(final_inputs)

        return final_outputs

net = neuralNetwork(2, 1, 1, 0.1)

EPOCHS = 50
for epoch in range(EPOCHS):
    random.shuffle(data)
    for point in data:
        training_data = [point[0], point[1]]
        target = point[2]
        net.train(training_data, target)

net.query([3, 1.5])