PyBrain神经网络不收敛于简单的f(x)=x+1函数

2024-06-28 19:44:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用下面的PyBrain神经网络代码将1添加到给定的输入中。代码不知何故从不收敛。你知道吗

我尝试了不同的学习率和不同数量的隐藏层,神经元等,但没有任何运气。你知道吗

有什么帮助吗?你知道吗

import numpy, math
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure import SigmoidLayer, LinearLayer
from pybrain.tools.shortcuts import buildNetwork

net = buildNetwork(1,
                   100, # number of hidden units
                   1,
                   bias = True,
                   hiddenclass = SigmoidLayer,
                   outclass = LinearLayer
                   )

xvalues = numpy.arange(100)
yvalues = (xvalues) + 1

ds = SupervisedDataSet(1, 1)
for x, y in zip(xvalues, yvalues):
    ds.addSample((x,), (y,))

print(ds['input'])
print(ds['target'])

trainer = BackpropTrainer(net, ds, verbose=True)
trainer.trainUntilConvergence()

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
y=xvalues
y_lin = [ net.activate([x]) for x in xvalues ]
ax.plot(y, y_lin, 'bo', lw=4)
ax.plot(y, yvalues, 'r^', lw=2)
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.show()

查看预期值和预测值之间的差异。你知道吗

奇怪的是,如果我给10个训练值而不是100个,网络似乎会收敛。你知道吗

谢谢。你知道吗


Tags: 代码fromimportnumpynetdspltax