使用以下代码:
tf = open('defl_07h.csv','r')
for line in tf.readlines():
data = [float(x) for x in line.strip().split(';') if x != '']
indata = tuple(data[:1])
outdata = tuple(data[1:])
ds.addSample(indata,outdata)
net = buildNetwork(ds.indim,20,ds.outdim,recurrent=True)
t = BackpropTrainer(net,learningrate=0.01,momentum=0.5,verbose=True)
t.trainOnDataset(ds,10)
t.testOnData(verbose=True)
获得如下相同的输出:
out:[3.479] 正确的:[11.86] 错误:35.12389858 out:[3.479] 正确的:[12.1] 错误:37.16423359 out:[3.479] 正确的:[12.28] 错误:38.73228485
然后创建网络结构:
Module: in
-connection to hidden0
- parameters [-1.9647867 -0.41898579 -0.24047698 0.6445537 0.06084947 -3.17343892
0.25454776 -0.45578641 0.70865416 -0.40517853 -0.22026247 -0.13106284
-0.71012557 -0.61140289 -0.00752148 -0.61770292 -0.50631486 0.95803659
-1.07403163 -0.87359713]
Recurrent connections
Module: bias
-connection to out
- parameters [ 0.55130311]
-connection to hidden0
- parameters [-0.31297409 -0.2182261 -0.70730661 -1.65964456 -0.18366456 0.52280203
-0.03388935 0.61288256 2.49908814 0.53909862 -0.56139066 0.06752532
-0.71713239 -1.4951833 0.84217369 0.16025118 0.01176442 -0.59444178
0.85652564 1.60607469]
Recurrent connections
Module: hidden0
-connection to out
- parameters [ 1.00559033 -0.02308752 -2.51970163 0.39714524 0.33257302 -0.6626978
-0.53004298 -1.0141971 -0.95530079 -0.66953093 -0.00438377 -1.1945728
0.99363152 -1.17032002 0.03620047 -0.21081934 0.2550164 -1.65894533
0.20820361 -1.38895542]
Recurrent connections
Module: out
Recurrent connections
哪里可能是错误?
答案 0 :(得分:6)
我发现您的代码示例存在一些问题,导致您的网络无法学习任何有意义的内容。以下是您的代码存在的一些问题:
BackpropTrainer
通常需要训练超过10个纪元。尝试100或1000.(或尝试我个人喜欢的RPromMinusTrainer
。)这两个问题都导致你的网络只是学会吐出一个值。
我改变了你的代码以包括规范化和RPropMinusTrainer
具有相当多的迭代次数(RProp-需要更少的时期):
from pybrain.supervised.trainers import *
from pybrain.tools.shortcuts import buildNetwork
from pybrain.datasets import SupervisedDataSet, SequentialDataSet
from pybrain.structure import RecurrentNetwork
from pybrain.structure import TanhLayer
import numpy as np
ds = SupervisedDataSet(1, 1)
tf = open('defl_07h.csv','r')
for line in tf.readlines():
data = [float(x) for x in line.strip().split(';') if x != '']
indata = tuple(data[:1])
outdata = tuple(data[1:])
ds.addSample(indata,outdata)
i = np.array([d[0] for d in ds])
i /= np.max(np.abs(i),axis=0)
o = np.array([d[1] for d in ds])
o /= np.max(np.abs(o),axis=0)
nds = SupervisedDataSet(1, 1)
for ix in range(len(ds)):
nds.addSample( i[ix], o[ix])
# train with normalized
net = buildNetwork(nds.indim,5,nds.outdim,recurrent=True)
t = RPropMinusTrainer(net,verbose=True)
t.trainOnDataset(nds,30)
t.testOnData(nds, verbose=True)
我得到以下输出:
out: [ 0.234]
correct: [ 0.168]
error: 0.00217289
out: [ 0.209]
correct: [ 0.168]
error: 0.00083736
...
out: [ 0.986]
correct: [ 0.914]
error: 0.00258013
out: [ 1.006]
correct: [ 0.916]
error: 0.00405199
out: [ 0.985]
correct: [ 0.924]
error: 0.00187248
不完美,但是预测会随着实际的标准化值而增加,就像原始(预标准化)数据一样。
PS:我知道这是一个老问题,但我在过去之前已经遇到过这个问题,并认为这可能对某些人有所帮助。因此,如果您在NN战壕中无处可去,请记住问自己:我的神经网是否期望归一化数据?我训练得足够吗?