Re: [问题] Neural Network中的delta-rule algorithm

楼主: yauhh (小y宝贝)   2014-06-17 03:34:39
※ 引述《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求权重向量时,用个循环,
这样就可以了.

Links booklink

Contact Us: admin [ a t ] ucptt.com