神经网络-基本Python

时间:2020-05-23 17:07:49

标签: python numpy machine-learning neural-network

我正在使用以下教程来开发执行前馈和背景操作的基本神经网络。教程的链接在这里:Python Neural Network Tutorial

import numpy as np

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

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

class NeuralNetwork:
    def __init__(self, x, y):
        self.input      = x
        self.weights1   = np.random.rand(self.input.shape[1],4) 
        self.weights2   = np.random.rand(4,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__":
    X = np.array([[0,0,1],
                  [0,1,1],
                  [1,0,1],
                  [1,1,1]])
    y = np.array([[0],[1],[1],[0]])
    nn = NeuralNetwork(X,y)

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

    print(nn.output)

我想做的是更改数据集,如果预测数字为偶数,则返回1,如果奇数为奇数,则返回0。因此,我进行了以下更改:

if __name__ == "__main__":
    X = np.array([[2,4,6,8,10],
                  [1,3,5,7,9],
                  [11,13,15,17,19],
                  [22,24,26,28,30]])
    y = np.array([[1],[0],[0],[1]])
    nn = NeuralNetwork(X,y)

The output I get is :
[[0.50000001]
 [0.50000002]
 [0.50000001]
 [0.50000001]]

我在做什么错了?

2 个答案:

答案 0 :(得分:1)

这里基本上有两个问题:

  1. 您的sigmoid_derivative表达式错误,应该是:

    返回Sigmoid(x)*(((1.0-Sigmoid(x)))

  2. 如果查看S型函数图或网络权重,则会发现由于输入量大,网络已饱和。通过执行类似X = X%5的操作,您可以获得所需的训练结果,这是我的数据结果:

    [[9.99626174e-01] [3.55126310e-04] [3.55126310e-04] [9.99626174e-01]]

sigmoid plot

答案 1 :(得分:0)

只需添加X = X/30并训练网络10倍以上。这对我来说是收敛的。将X除以30,以使每个输入都介于0和1之间。由于它是一个更复杂的数据集,因此训练时间更长。

您的导数很好,因为使用导数功能时,其输入已经为sigmoid(x)。因此x*(1-x) sigmoid(x)*(1-sigmoid(x))