Re: [問題] Neural Network中的delta-rule algorithm

看板Python作者 (喲)時間11年前 (2014/06/17 03:34), 11年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《sariel0322 (sariel)》之銘言: : 想請問一下 : 我現在有一個表: : input1 input2 output : -1 1 1 : 0 0 1 : 1 -1 1 : 1 0 0 : 0 1 0 : 希望能用delta-learning algorithm 以 iteration的方式 : 來得到final vector of weight factor : 如果用python來寫的話,大概要怎麼寫? : 在網路上搜尋了很久,找到的幾乎都是用matlab來做 根據http://en.wikipedia.org/wiki/Delta_rule 說delta rule 是求一個權重變量的函數. 應該要先把使用的環境認清楚,看有多少個東西,包括 神經元,輸入,權重,輸出等,需要用到多少個函數,包括 delta rule 和activation function等等. 式子說 delta w_ji = alpha * (t_j - y_j) * g'(h_j) * x_i 右邊的 t_j, y_j, h_j, x_i, alpha, g' 是這個函數的輸入, 左式是輸出. 所以,可以先寫個大概是這樣: def dweight(t, y, h, x, a, g): return a * (t-y) * apply(g,[h]) * x 但其實這樣已差不多了. g'應該是從原有的 activation function g 推演來, 現在先把g'當作一個現成的函數來使用. 然後,要使用delta rule,要先看你神經元的輸入/輸出表有多少東西. 像你問的表,是指一個神經元,二個輸入,將執行情況寫出來可能是 這個map: neuron = { 'x_1': x_1, 'x_2': x_2, 'w_1': w_1, 'w_2': w_2, 'y': y, 'g': g, \ 'g,': g1 } 因此,求 delta weight 就是這樣: h = neuron['x_1'] * neuron['w_1'] + neuron['x_2'] * neuron['w_2'] neuron['w_1'] = neuron['w_1'] + dweight(t, neuron['y'], h, neuron['x_1'], \ a, getattr(neuron,'g,')) neuron['w_2'] = neuron['w_2'] + dweight(t, neuron['y'], h, neuron['x_2'], \ a, getattr(neuron,'g,')) 或者寫成: neuron['w_1'], neuron['w_2'] = map( \ lambda (x,w): w + dweight(t, neuron['y'], h, x, a, getattr(neuron,'g,')), \ [(neuron['x_1'], neuron['w_1']), (neuron['x_2'], neuron['w_2'])]) 這樣,權重就改了. 當然在改權重之前,要先確認 neuron 輸入和輸出是不是都 符合那一張預期輸入/輸出表,然後再來計算權重. 再多說一點, 在確認輸入/輸出表的時候,用python的map, 在iteration求權重向量時,用個迴圈, 這樣就可以了. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.167.134.118 ※ 文章網址: http://www.ptt.cc/bbs/Python/M.1402947284.A.42B.html ※ 編輯: yauhh (118.167.134.118), 06/17/2014 03:41:45
文章代碼(AID): #1JdqRKGh (Python)
文章代碼(AID): #1JdqRKGh (Python)