Chainer CNN- TypeError:forward()缺少1个必需的位置参数:'x'

时间:2018-12-14 07:43:32

标签: python-3.x image-processing deep-learning chainer

我正在尝试在Chainer上运行分类器,但由于以下错误而失败。

我不知道该错误,因为我确认迭代器实际上已将批次发送给培训师。

神经网络模型有问题吗?还是将数据输入模型的方法不正确?

Input.py

from chainer.datasets import split_dataset_random
from chainer.iterators import SerialIterator
from chainer.optimizers import Adam
from chainer.training import Trainer
from chainer.training.updaters import StandardUpdater
from chainer import functions as F, links as L
from chainer import Sequential

import numpy as np

batch_size = 3

X_train = np.ones((9957, 60, 80, 3), dtype=np.float32)
X_train, _ = split_dataset_random(X_train, 8000, seed=0)
train_iter = SerialIterator(X_train, batch_size)

model = Sequential(
    L.Convolution2D(None, 64, 3, 2),
    F.relu,
    L.Convolution2D(64, 32, 3, 2),
    F.relu,
    L.Linear(None, 16),
    F.dropout,
    L.Linear(16, 4)
)

model_loss = L.Classifier(model)
optimizer = Adam()
optimizer.setup(model_loss)
updater = StandardUpdater(train_iter, optimizer)
trainer = Trainer(updater, (25, 'epoch'))

trainer.run()

Stacktrace.py

Exception in main training loop: forward() missing 1 required positional argument: 'x'
Traceback (most recent call last):
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/training/trainer.py", line 315, in run
    update()
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/training/updaters/standard_updater.py", line 165, in update
    self.update_core()
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/training/updaters/standard_updater.py", line 181, in update_core
    optimizer.update(loss_func, in_arrays)
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/optimizer.py", line 680, in update
    loss = lossfun(*args, **kwds)
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/link.py", line 242, in __call__
    out = forward(*args, **kwargs)
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/links/model/classifier.py", line 143, in forward
    self.y = self.predictor(*args, **kwargs)
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/link.py", line 242, in __call__
    out = forward(*args, **kwargs)
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/sequential.py", line 210, in forward
    x = layer(*x)
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/link.py", line 242, in __call__
    out = forward(*args, **kwargs)
Will finalize trainer extensions and updater before reraising the exception.
Traceback (most recent call last):
  File "/home/user/deploy/aaa.py", line 33, in <module>
    trainer.run()
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/training/trainer.py", line 348, in run
    six.reraise(*exc_info)
  File "/home/user/miniconda3/lib/python3.7/site-packages/six.py", line 693, in reraise
    raise value
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/training/trainer.py", line 315, in run
    update()
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/training/updaters/standard_updater.py", line 165, in update
    self.update_core()
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/training/updaters/standard_updater.py", line 181, in update_core
    optimizer.update(loss_func, in_arrays)
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/optimizer.py", line 680, in update
    loss = lossfun(*args, **kwds)
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/link.py", line 242, in __call__
    out = forward(*args, **kwargs)
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/links/model/classifier.py", line 143, in forward
    self.y = self.predictor(*args, **kwargs)
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/link.py", line 242, in __call__
    out = forward(*args, **kwargs)
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/sequential.py", line 210, in forward
    x = layer(*x)
  File "/home/user/miniconda3/lib/python3.7/site-packages/chainer/link.py", line 242, in __call__
    out = forward(*args, **kwargs)
TypeError: forward() missing 1 required positional argument: 'x'

神经网络模型或将数据输入模型的方式是否存在问题?如果您需要查看完整的代码,请告诉我

1 个答案:

答案 0 :(得分:0)

您要做的就是为模型提供ndarrayint的元组,因为这是L.Classifier的规范。

  

神经网络模型有问题吗?还是将数据输入模型的方法不正确?

因此,绝对的答案是“将数据输入模型的方式是错误的”。

在下面的代码中,我定义了一个继承DatasetMixin的类,以填充ndarrayint的元组。 (这是Chainer运行的常规方式)

应注意,L.Convolution2D的输入自变量必须是ndarray,其形状为(批,通道,宽度,高度)。所以我将数组转置到数据集中。

Solution.py

from chainer.datasets import split_dataset_random
from chainer.iterators import SerialIterator
from chainer.optimizers import Adam
from chainer.training import Trainer
from chainer.training.updaters import StandardUpdater
from chainer import functions as F, links as L
from chainer import Sequential

from chainer.dataset import DatasetMixin

import numpy as np


class MyDataset(DatasetMixin):
    def __init__(self, X, labels):
        super(MyDataset, self).__init__()
        self.X_ = X
        self.labels_ = labels
        self.size_ = X.shape[0]

    def __len__(self):
        return self.size_

    def get_example(self, i):
        return np.transpose(self.X_[i, ...], (2, 0, 1)), self.labels_[i]


batch_size = 3

X_train = np.ones((9957, 60, 80, 3), dtype=np.float32)
label_train = np.random.randint(0, 4, (9957,), dtype=np.int32)
dataset = MyDataset(X_train, label_train)
dataset_train, _ = split_dataset_random(dataset, 8000, seed=0)
train_iter = SerialIterator(dataset_train, batch_size)

model = Sequential(
    L.Convolution2D(None, 64, 3, 2),
    F.relu,
    L.Convolution2D(64, 32, 3, 2),
    F.relu,
    L.Linear(None, 16),
    F.dropout,
    L.Linear(16, 4)
)

model_loss = L.Classifier(model)
optimizer = Adam()
optimizer.setup(model_loss)
updater = StandardUpdater(train_iter, optimizer)
trainer = Trainer(updater, (25, 'epoch'))

trainer.run()