0

我正在尝试将 chainer 包用于我正在处理的大型项目。我已经阅读了他们网站上的教程,该教程给出了将其应用于 MNIST 数据集的示例,但它似乎无法轻松扩展到其他示例,而且根本没有足够的文档。

他们的示例代码如下:

class MLP(Chain):
    def __init__(self, n_units, n_out):
        super(MLP, self).__init__(
            # the size of the inputs to each layer will be inferred
            l1=L.Linear(None, n_units),  # n_in -> n_units
            l2=L.Linear(None, n_units),  # n_units -> n_units
            l3=L.Linear(None, n_out),    # n_units -> n_out
        )

    def __call__(self, x):
        h1 = F.relu(self.l1(x))
        h2 = F.relu(self.l2(h1))
        y = self.l3(h2)
        return y

train, test = datasets.get_mnist()
train_iter = iterators.SerialIterator(train, batch_size=5, shuffle=True)
test_iter = iterators.SerialIterator(test, batch_size=2, repeat=False, shuffle=False)
model = L.Classifier(MLP(100, 10))  # the input size, 784, is inferred
optimizer = optimizers.SGD()
optimizer.setup(model)
updater = training.StandardUpdater(train_iter, optimizer)
trainer = training.Trainer(updater, (4, 'epoch'), out='result')
trainer.extend(extensions.Evaluator(test_iter, model))
trainer.extend(extensions.LogReport())
trainer.extend(extensions.PrintReport(['epoch', 'main/accuracy', 'validation/main/accuracy']))
trainer.extend(extensions.ProgressBar())
trainer.run()  

有人可以指出如何将一条直线简单地拟合到 2D 中的几个数据点的方向吗?如果我能理解这样的简单拟合,我应该能够适当地缩放。

谢谢您的帮助!

4

1 回答 1

0

我在这里粘贴了简单的回归模型。

您可以将原始训练数据和测试数据用作元组。火车=(数据,标签)

这里,data.shape =(数据数,数据维度数)

并且,label.shape =(数据数,)

它们的数据类型都应该是 numpy.float32。

import chainer
from chainer.functions import *
from chainer.links import *
from chainer.optimizers import *

from chainer import training
from chainer.training import extensions
from chainer import reporter
from chainer import datasets
import numpy


class MyNet(chainer.Chain):

    def __init__(self):
        super(MyNet, self).__init__(
            l0=Linear(None, 30, nobias=True),
            l1=Linear(None, 1, nobias=True),
        )

    def __call__(self, x, t):
        l0 = self.l0(x)
        f0 = relu(l0)
        l1 = self.l1(f0)
        f1 = flatten(l1)
        self.loss = mean_squared_error(f1, t)
        reporter.report({'loss': self.loss}, self)
        return self.loss


def get_optimizer():
    return Adam()


def training_main():
    model = MyNet()

    optimizer = get_optimizer()
    optimizer.setup(model)

    train, test = datasets.get_mnist(label_dtype=numpy.float32)
    train_iter = chainer.iterators.SerialIterator(train, 50)
    test_iter = chainer.iterators.SerialIterator(test, 50,
                                                 repeat=False,
                                                 shuffle=False)

    updater = training.StandardUpdater(train_iter, optimizer)
    trainer = training.Trainer(updater, (10, 'epoch'))
    trainer.extend(extensions.ProgressBar())
    trainer.extend(extensions.Evaluator(test_iter, model))
    trainer.extend(
        extensions.PlotReport(['main/loss', 'validation/main/loss'],
                              'epoch'))
    trainer.run()


if __name__ == '__main__':
    training_main()
于 2017-03-25T01:39:12.497 回答