我在python中編寫了一個神經網絡對象,它具有成本函數並通過反向傳播確定其漸變。我看到一些優化功能here,但我不知道如何實現它們。我也很難找到任何示例代碼來學習。Scipy優化算法? (用於最小化神經網絡成本函數) - python
很明顯,我需要以某種方式告訴它我試圖改變什麼參數,我試圖最小化成本函數,然後通過backprop計算梯度。我怎麼說,說,fmin_cg是什麼?
獎勵問題:我可以在哪裏瞭解各種算法的使用差異?
===== OK,更新=====
這是我大氣壓:
def train(self, x, y_vals, iters = 400):
t0 = concatenate((self.hid_t.reshape(-1), self.out_t.reshape(-1)), 1)
self.forward_prop(x, t0)
c = lambda v: self.cost(x, y_vals, v)
g = lambda v: self.back_prop(y_vals, v)
t_best = fmin_cg(c, t0, g, disp=True, maxiter=iters)
self.hid_t = reshape(t_best[:,:(hid_n * (in_n+1))], (hid_n, in_n+1))
self.out_t = reshape(t_best[:,(hid_n * (in_n+1)):], (out_n, hid_n+1))
而且,這是它拋出的錯誤:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "netset.py", line 27, in <module>
net.train(x,y)
File "neuralnet.py", line 60, in train
t_best = fmin_cg(c, t0, g, disp=True, maxiter=iters)
File "/usr/local/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 952, in fmin_cg
res = _minimize_cg(f, x0, args, fprime, callback=callback, **opts)
File "/usr/local/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 1017, in _minimize_cg
deltak = numpy.dot(gfk, gfk)
ValueError: matrices are not aligned
... Halp!
哇,很好的迴應。謝謝。 – mavix 2012-07-07 22:45:18
這實際上是低成本的,爲什麼前向傳播應該執行兩次?在matlab的fmin_cg中,你只需要一個函數返回成本和漸變作爲元組。 – Curious 2013-07-02 16:32:53
好的提示。我將編輯答案。 – alfa 2013-07-02 17:35:55