自己的第乙個神經網路

2021-09-23 17:16:56 字數 1817 閱讀 3305

最近一直在研究神經網路,於是週末空閒之餘通過一篇文章的啟發製作了乙個3層神經網路,用來計算加法(兩個輸入,3個隱含,1個輸出)。訓練5000000次後效果還是不錯的,幾乎可以計算所有和小於10的加法了。

(**)

import numpy as np

import random

def sigmoid(x):

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

w =

h =

o = 0

s = 0.6

for i in range(9):

for i in range(3):

def train():

for i in range(5000000):

if i%2 == 0:

x1 = random.randint(0,5)/10

x2 = random.randint(0,5)/10

y = x1 + x2

else:

x1 = random.uniform(0, 5) / 10

x2 = random.uniform(0, 5) / 10

y = x1 + x2

h[0] = sigmoid(x1*w[0]+x2*w[3])

h[1] = sigmoid(x1*w[1]+x2*w[4])

h[2] = sigmoid(x1*w[2]+x2*w[5])

o = sigmoid(h[0]*w[6]+h[1]*w[7]+h[2]*w[8])

err = pow((o-y),2)

c1 = -(o-y)*o*(1-o)

c2 = (c1*w[6])*(h[0])*(1-h[0])

c3 = (c1*w[7])*(h[1])*(1-h[1])

c4 = (c1*w[8])*(h[2])*(1-h[2])

w[0] = w[0]+(x1*(c2)*s)

w[1] = w[1]+(x1*(c3)*s)

w[2] = w[2]+(x1*(c4)*s)

w[3] = w[3]+(x2*(c2)*s)

w[4] = w[4]+(x2*(c3)*s)

w[5] = w[5]+(x2*(c4)*s)

w[6] = w[6]+(h[0]*(c1)*s)

w[7] = w[7]+(h[1]*(c1)*s)

w[8] = w[8]+(h[2]*(c1)*s)

print('epoch:'+str(i)+' x1:'+str(x1*10)+' x2:'+str(x2*10)+' y:'+str(y*10)+' output:'+str(o*10)+' error:'+str(err)+' 差:'+str(abs(o-y)))

while true:

x1 = float(input('x1: '))/10

x2 = float(input('x2: '))/10

y = x1 + x2

h[0] = sigmoid(x1 * w[0] + x2 * w[3])

h[1] = sigmoid(x1 * w[1] + x2 * w[4])

h[2] = sigmoid(x1 * w[2] + x2 * w[5])

o = sigmoid(h[0] * w[6] + h[1] * w[7] + h[2] * w[8])

print('output: '+str(o*10)+' y: '+str(y*10)+'\n')

train()

不過這個模型有2個缺陷:無法儲存訓練結果,以及純手工進行計算操作(沒辦法,我能力有限呀...)。下個週末我會來改進的。

構建第乙個神經網路

莫煩python 使用軟體anaconda3 import tensorflow as tf import numpy as np 匯入模組以後,構建乙個新增神經網路層的函式 add layer 其中需要設定的神經網路層的變數為輸入輸出和激勵函式,同時需要告知函式輸入輸出的大小 size def a...

建造第乙個神經網路

importtensorflowastf defadd layer inputs,in size,out size,activation function none 他有四個引數 輸入值,輸入的大小,輸出的大小,激勵函式 此處設定為none weights tf.variable tf.random...

第乙個神經網路的訓練

什麼是遷移學習 為了對遷移學習產生乙個直觀的認識,不妨拿老師與學生之間的關係做模擬。一位老師通常在ta所教授的領域有著多年豐富的經驗,在這些積累的基礎上,老師們能夠在課堂上教授給學生們該領域最簡明扼要的內容。這個過程可以看做是老手與新手之間的 資訊轉移 這個過程在神經網路中也適用。我們知道,神經網路...