机器学习算法将所有输出设置为零

时间:2019-10-05 20:03:56

标签: python machine-learning

我正在尝试制作我的第一个机器学习算法,但是它不起作用。目前,我正在尝试进行“与”门。当我训练我的神经网络时,所有输出都为零。这是我的代码 (函数feed_forward和property_backwards在名为Neuro_network的类中):

def feed_forward(self, mInput):
    mInput = np.reshape(mInput, self.layer[0][0].shape)
    self.layer[0][0] = np.asmatrix(mInput)
    self.layer[1][0] = np.asmatrix(mInput)
    for i in range (1, len(self.layer[0])):
        self.layer[0][i] = self.weight[i-1] * self.layer[1][i-1] + self.bias[i-1]
        self.layer[1][i] = self.sigmoid(self.layer[0][i])
    return self.layer


def propagate_backwards(self, inputs, desired_outputs, learning_rate):
    #set deltaWeights and deltaBiases to zero
    for i in range (0, len(self.deltaWeight)):
        self.deltaWeight[i] -= self.deltaWeight[i]
        self.deltaBias[i] -= self.deltaBias[i]

    for i in range (0, len(inputs)):
        self.layer = self.feed_forward(inputs[i])
        self.deltaLayer[-1] = np.multiply((self.layer[1][-1] - np.asmatrix(desired_outputs[i])), self.derivativeSigmoid(self.layer[0][-1]))
        for j in range (len(self.layer[0])-2, -1, -1):
            self.deltaLayer[j] = np.multiply((self.weight[j].transpose() * self.deltaLayer[j+1]), self.derivativeSigmoid(self.layer[0][j]))
            self.deltaWeight[j] += (self.deltaLayer[j+1] * self.layer[1][j].transpose()) / len(inputs)
            self.deltaBias[j] += self.deltaLayer[j+1] / len(inputs)
    for i in range (0, len(self.weight)):
        self.weight[i] -= self.deltaWeight[i] * learning_rate
        self.bias[i] -= self.deltaBias[i] * learning_rate





training_data = [[], [], [], []]
desired_output = [[], [], [], []]

training_data[0] = [1, 1]
training_data[1] = [1, 0]
training_data[2] = [0, 1]
training_data[3] = [0, 0]

desired_output[0] = [1]
desired_output[1] = [0]
desired_output[2] = [0]
desired_output[3] = [0]

#create neural network with 2 neurons in the first layer, 2 neurons in the second layer and 1 layer in the third layer
nn = neural_network([2, 2, 1], '/Users/anton/Documents/machine_learning/MNIST/neural_network3.txt') 

for i in range (0, 4): #print outputs and desired_outputs before training
    print(str(nn.feed_forward(training_data[i])[1][-1]) + " " + str(desired_output[i]))
print("")

for i in range(0, 20): #train 20 times
    nn.propagate_backwards(training_data, desired_output, 1)
    for i in range (0, 4):
        print(str(nn.feed_forward(training_data[i])[1][-1]) + " " + str(desired_output[i])) #print outputs and desired_outputs during training
    print("")

我的代码有什么问题? 我希望我的代码清楚... 预先感谢!

0 个答案:

没有答案