ChainerでAutoEncoderを作成して、学習させようしたところ,
以下のようなエラーが発生しました。
Traceback (most recent call last):
File "MNIST_autoenc.py", line 30, in <module>
y = model(xs[batch:batch+128])
File "/usr/local/lib/python3.7/site-packages/chainer/link.py", line 293, in __call__
forward = self.forward # type: ignore
AttributeError: 'AutoEncoder' object has no attribute 'forward'
コードは以下の通りです。
修正箇所があればご教授お願いします。
import chainer
import chainer.links as L
import chainer.functions as F
import numpy as np
class AutoEncoder(chainer.Chain):
def __init__(self):
super(AutoEncoder, self).__init__(
l0 = L.Linear(784, 256),
l1 = L.Linear(256, 784)
)
def __call__(self, x):
h0 = F.relu(self.l0(x))
h1 = F.sigmoid(self.l1(h0))
return h5
train, test = chainer.datasets.get_mnist(ndim=1)
xs, ts = train._datasets
txs, tts = test._datasets
model = AutoEncoder()
model.to_cpu()
optimizer = chainer.optimizers.Adam()
optimizer.setup(model)
for _ in range(1000):
model.zerograds()
batch = np.random.randint(0, 60000-128)
y = model(xs[batch:batch+128])
acc = F.accuracy(y, rs[batch:batch+128])
loss = F.softmax_cross_entropy(y, t[batch:batch+128])
loss.backward()
optimizer.update()
if _ % 100 == 0:
print("epoch:%d --> accuracy:%.4f, loss:%.4f" % (_, acc.data, loss.data))
環境は
macbook pro 2015
python3 ver3.7.0
chainer ver6.0.0
numpy 1.17.0