我在 python 中编写了一个神经网络对象,它具有成本函数并通过反向传播确定其梯度。我在这里看到一堆优化功能,但我不知道如何实现它们。我也很难找到任何示例代码来学习。
显然,我需要以某种方式告诉它我要更改哪些参数,我要最小化的成本函数,然后是反向传播计算的梯度。我怎么知道 fmin_cg 是什么?
额外问题:我在哪里可以了解各种算法的使用差异?
=====好的,更新=====
这就是我的自动取款机:
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
……住手!