0

I'm trying to construct an LSTM network with multi GPUs using Chainer (v4.0.0b1). As in the following code.

import numpy as np
import chainer
from chainer import optimizers, Chain, training, iterators, serializers, cuda, Variable
import chainer.functions as F
import chainer.links as L

...

class Network(Chain):
    def __init__(self):
        super(Network, self).__init__()
        with self.init_scope():
            ...
            self.fc1  = L.Liner(3000, 1000).to_gpu(1)
            self.lstm = L.LSTM(1000, 1000).to_gpu(1)
            self.fc2  = L.Liner(1000, 3000).to_gpu(1)
            ...

    def __call__(self, x, t):
        ...

...

However, the LSTM link becomes "NoneType". As in the following error in call.

TypeError: 'NoneType' object is not callble

I thought it was strange so I displayed "self.lstm". As a result, 'None' was displayed. For example, fc1 which is "Link" is displayed as follows.

<chainer.links.connection.linear.Linear object at hogehoge>

I found out that "self.lstm" could not be declared as Link in "self.lstm = L.LSTM(1000, 1000).to_gpu(1)". However, I don't know why I can't declare it.

I use Chainer's Docker as the execution environment.

Thank you for answering.

2 Answers2

0

In short, use

class Network(Chain):
    def __init__(self):
        super(Network, self).__init__()
        with self.init_scope():
            ...
            self.fc1  = L.Liner(3000, 1000)
            self.lstm = L.LSTM(1000, 1000)
            self.fc2  = L.Liner(1000, 3000)
            ...

    def __call__(self, x, t):
        ...

model = Network()
model.to_gpu()

Detail:

In chainer, to_gpu() returns None in almost all cases, so you must not use a method chain. (The only one exception is chainer.backends.cuda.to_gpu(), which returns the GPU-nized array.)

Instead, Link.to_gpu() send all its attributes (Variables and Links) to the GPU and replace the reference from the object on CPU to that on the GPU.

Therefore, you do not have to substitute the returned value of LSTM.to_gpu() for the self.lstm attribute.

Yuki Hashimoto
  • 1,013
  • 7
  • 19
0

This error is a bag of Chainer. It has been fixed and is waiting for checks. After a while, it will be committed.