如何在PyBrain中使用函数normalize()?

时间:2016-03-13 20:41:41

标签: python neural-network normalization pybrain

我需要在训练前规范化我的数据。在pybrain.rl.environments.task中有一个函数normalize()。但我没有尝试,不工作,只有错误。无法调用训练数据的功能。

from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure import TanhLayer
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import RPropMinusTrainer
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure.networks import Network
from pybrain.rl.environments.task import Task
import numpy as np

ds = SupervisedDataSet(3, 1)

ds.addSample( (76.7, 13.8, 103.0), 770)
ds.addSample( (70.9, 13.0, 92.0), 650)
ds.addSample( (65.6, 15.9, 104.3), 713)
ds.addSample( (59.3, 14.8, 88.0), 593)
ds.addSample( (50.0, 13.0, 65.2), 443)
ds.addSample( (44.9, 17.6, 79.0), 547)
ds.addSample( (44.3, 18.4, 78.6), 553)
ds.addSample( (44.4, 18.4, 81.8), 576)

#create object for training data
test = Task(ds)

#set the normalization limits from 0 to 1
test.setScaling([(0, 1)], None)

#function call(problem here, I tried a lot of options for a function call, but none worked)
test.normalize((0, 1))

net = buildNetwork(ds.indim, 3, ds.outdim, bias = True, hiddenclass=TanhLayer)

trainer = BackpropTrainer(net, dataset=ds, verbose=False, learningrate = 0.01, momentum = 0.99)

trainer.trainOnDataset(ds,100)
trainer.testOnData(verbose=False)

我不明白为什么以及如何传递函数的规范化以使其有效。

1 个答案:

答案 0 :(得分:0)

我知道这已经太晚了,但我发现here是我找到的。看起来setScaling在参数sensor_limits中要标准化的维度中接收min-max的元组:

def setScaling(self, sensor_limits, actor_limits):
    self.sensor_limits = sensor_limits
    self.actor_limits = actor_limits

另一方面,normalize()接收参数传感器中的实际尺寸,并使用sensor_limits中指定的元组(min-max)进行计算:

for l, s in zip(self.sensor_limits, sensors):
    if not l:
        result.append(s)
    else:
        result.append((s - l[0]) / (l[1] - l[0]) * 2 - 1.0)