簡體   English   中英

神經網絡 - 基本 Python

[英]Neural Network - Basic Python

我正在使用以下教程來開發執行前饋和背景的基本神經網絡。 教程鏈接在這里: 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)

我試圖做的是更改數據集,如果預測數是偶數則返回 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]]

我究竟做錯了什么?

這里基本上有兩個問題:

  1. 您對 sigmoid_derivative 的表達是錯誤的,應該是:

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

  2. 如果您查看 sigmoid function plot 或您的網絡權重,您會發現您的網絡由於輸入量大而飽和。 通過執行 X=X%5 之類的操作,您可以獲得所需的訓練結果,這是我對您的數據的結果:

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

sigmoid 圖

只需添加X = X/30並將網絡訓練時間延長 10 倍。 這對我來說是收斂的。 您將X除以 30 以使每個輸入都在 0 和 1 之間。您訓練它的時間更長,因為它是一個更復雜的數據集。

您的導數很好,因為當您使用導數 function 時,它的輸入已經是sigmoid(x) 所以x*(1-x)sigmoid(x)*(1-sigmoid(x))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM