1

我是 Pylearn 的新手——所以这个问题可能非常基础。我想将 L2 正则化添加到 MLP 的 SGD 训练器中,但我不知道该怎么做。这是我的代码(失败):

from pylearn2.datasets import DenseDesignMatrix
from pylearn2.models import mlp
from pylearn2.training_algorithms import sgd
from pylearn2.termination_criteria import EpochCounter
import theano
from pylearn2.costs import mlp as mlp_costs

c = mlp_costs.WeightDecay({'hidden':0.1, 'output':0.1})
# skipping the data reading part
ds = XOR()
hidden_layer = mlp.Sigmoid(layer_name='hidden', dim=2, irange=.1, init_bias=1.)
output_layer = mlp.Softmax(2, 'output', irange=.1)
trainer = sgd.SGD(learning_rate=.05, cost=c, batch_size=10, termination_criterion=EpochCounter(400))
layers = [hidden_layer, output_layer]
ann = mlp.MLP(layers, nvis=2)
trainer.setup(ann, ds)

注意:我已经跳过了数据读取部分——假设它不会影响问题。这就是我运行 mlp 的方式:

while True:
    trainer.train(dataset=ds)
    ann.monitor.report_epoch()
    ann.monitor()
    if not trainer.continue_learning(ann):
        break

这是我看到的错误:

Traceback (most recent call last):
  File "pylearn_test.py", line 52, in <module>
    run_ann()
  File "pylearn_test.py", line 38, in run_ann
    trainer.train(dataset=ds)
  File "/root/pylearn2/pylearn2/training_algorithms/sgd.py", line 442, in train
    "data_specs: %s" % str(data_specs))
NotImplementedError: Unable to train with SGD, because the cost does not actually use data from the data set. data_specs: (NullSpace, '')

我不确定如何将 L2 成本包含在正则化中?有人可以帮忙吗?

4

0 回答 0