BP神經網路 解決異或問題

2021-09-01 13:35:28 字數 3234 閱讀 6425

import numpy as np

import matplotlib.pyplot as plt

#輸入資料(4,3)

x = np.array([[1,0,0],

[1,0,1],

[1,1,0],

[1,1,1]])

#標籤y = np.array([[0,1,1,0]])

#權值初始化,1行3列,取值範圍-1到1

v = (np.random.random((3,4))-0.5)*2 #輸入層和隱藏層之間的權值(3,4)

w = (np.random.random((4,1))-0.5)*2 #隱藏層和輸出層之間的權值(4,1)

print(w)

print(v)

#學習率設定

lr = 0.11

#啟用函式sigmoid

def sigmoid(x):

return 1/(1+np.exp(-x))

#simoid求導

def dsigmoid(x):

return x*(1-x)

#權值更新

def update():

global x,y,w,v,lr

output_hidden = sigmoid(np.dot(x,v))#隱藏層的輸出(4,4)

output_output = sigmoid(np.dot(output_hidden,w))#輸出層的輸出(4,1)

#輸出層誤差訊號

output_output_delta = (y.t - output_output)*dsigmoid(output_output)

#隱藏層誤差訊號

output_hidden_delta = output_output_delta.dot(w.t)*dsigmoid(output_hidden)

#權值的增量

w_c = lr*output_hidden.t.dot(output_output_delta)#

v_c = lr*x.t.dot(output_hidden_delta)

w = w + w_c

v = v + v_c

for i in range(20000):

update()

if i%500==0:

output_hidden = sigmoid(np.dot(x,v))#隱藏層的輸出(4,4)

output_output = sigmoid(np.dot(output_hidden,w))#輸出層的輸出(4,1)

print('error',np.mean(np.abs(y.t-output_output)))

output_hidden = sigmoid(np.dot(x,v))#隱藏層的輸出(4,4)

output_output = sigmoid(np.dot(output_hidden,w))#輸出層的輸出(4,1)

print(output_output)

[[ 0.79697584]

[-0.899471 ]

[-0.03021633]

[-0.73609684]]

[[-0.83225906 0.49092062 -0.93064959 0.39906478]

[-0.4626103 0.40386656 0.90899369 -0.84416671]

[-0.12415614 -0.30768482 -0.80695638 -0.11680479]]

error 0.006722481867364967

error 0.006711968080461744

error 0.006701502339927143

error 0.006691084283599529

error 0.006680713553112746

error 0.0066703897938456425

error 0.006660112654871668

error 0.0066498817889102235

error 0.0066396968522776535

error 0.006629557504839739

error 0.006619463409965184

error 0.006609414234478989

error 0.006599409648617244

error 0.006589449325982851

error 0.006579532943500827

error 0.006569660181375644

error 0.006559830723048313

error 0.006550044255154733

error 0.006540300467484534

error 0.006530599052939871

error 0.006520939707496551

error 0.006511322130163323

error 0.006501746022944472

error 0.006492211090801057

error 0.0064827170416130505

error 0.006473263586143195

error 0.006463850438000195

error 0.006454477313602788

error 0.006445143932144832

error 0.006435850015560173

error 0.0064265952884888415

error 0.00641737947824287

error 0.006408202314773685

error 0.00639906353063903

error 0.006389962860970968

error 0.006380900043443988

error 0.006371874818244327

error 0.00636288692803846

error 0.006353936117943215

error 0.006345022135495877

[[0.00497924]

[0.99446599]

[0.99287581]

[0.0077072 ]]

線性神經網路 解決異或問題

日常三大包 import numpy as np import pandas as pd import matplotlib.pyplot as plt多想想upadte函式的原理,這裡是先隨機乙個權重w,然後w的置換與x相乘去,得到的是乙個列 w1x1 w2x2 w3x3的形式 然後其與y進行比較...

線性神經網路(異或)

v np.dot x,w np.dot x,w array矩陣相乘 4 3乘3 1等於4 1 w lr x.t.dot y v int x.shape 0 np.dot x,w array矩陣相乘 3 4乘4 1等於3 1 v np.dot x,w np.dot x,w array矩陣相乘 4 3乘...

基於python的BP神經網路及異或實現過程解析

bp神經網路是最簡單的神經網路模型了,三層能夠模擬非線性函式效果。難點 neural networks created on 2019.9.24 author vince import math import logging import numpy import random import mat...