简体   繁体   English

神经网络 - 基本 Python

[英]Neural Network - Basic Python

I am using the following tutorial for developing a basic neural network that does feedforward and backdrop.我正在使用以下教程来开发执行前馈和背景的基本神经网络。 The link to the tutorial is here: Python Neural Network Tutorial教程链接在这里: Python 神经网络教程

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)

What im trying to do is change the data set and return 1 if the predicted number is even and 0 if the same is odd.我试图做的是更改数据集,如果预测数是偶数则返回 1,如果预测数是奇数则返回 0。 So I made the following changes:所以我做了以下更改:

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]]

What am I doing wrong?我究竟做错了什么?

Basically there are two problems here:这里基本上有两个问题:

  1. Your expression of sigmoid_derivative is wrong, it should be:您对 sigmoid_derivative 的表达是错误的,应该是:

    return sigmoid(x)*((1.0 - sigmoid(x)))返回 sigmoid(x)*((1.0 - sigmoid(x)))

  2. If you take a look at the sigmoid function plot or your network weights, you would find out that your network saturated due to your large input.如果您查看 sigmoid function plot 或您的网络权重,您会发现您的网络由于输入量大而饱和。 By doing something like X=X%5 you can get the training result you want, as the result of mine on your data:通过执行 X=X%5 之类的操作,您可以获得所需的训练结果,这是我对您的数据的结果:

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

sigmoid 图

Just add X = X/30 and train the network 10 times longer.只需添加X = X/30并将网络训练时间延长 10 倍。 This converged for me.这对我来说是收敛的。 You divide X by 30 to make every input in between 0 and 1. You train it longer because it is a more complex dataset.您将X除以 30 以使每个输入都在 0 和 1 之间。您训练它的时间更长,因为它是一个更复杂的数据集。

Your derivative is fine because when you use the derivative function, the input to it is already sigmoid(x) .您的导数很好,因为当您使用导数 function 时,它的输入已经是sigmoid(x) So x*(1-x) is sigmoid(x)*(1-sigmoid(x))所以x*(1-x)sigmoid(x)*(1-sigmoid(x))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM