Chainer 的 Link 变为 "NoneType"

Chainer's Link becomes "NoneType"

我正在尝试使用 Chainer (v4.0.0b1) 构建具有多 GPU 的 LSTM 网络。 如以下代码。

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):
        ...

...

然而,LSTM link 变成了 "NoneType"。如以下调用错误。

TypeError: 'NoneType' object is not callble

觉得奇怪就显示了"self.lstm"。结果显示了'None'。例如"Link"的fc1显示如下

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

我发现 "self.lstm" 不能在 "self.lstm = L.LSTM(1000, 1000).to_gpu(1)" 中声明为 Link。但是不知道为什么不能申报

我使用Chainer's Docker作为执行环境

感谢您的回答。

简而言之,使用

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()

详情:

在chainer中,to_gpu() returns None几乎在所有情况下,所以你不能使用方法链。 (唯一的例外是 chainer.backends.cuda.to_gpu(),它是 returns GPU 化数组。)

相反,Link.to_gpu() 将其所有属性(变量和链接)发送到 GPU,并将 CPU 上对象的引用替换为 GPU 上的对象。

因此,您不必用 LSTM.to_gpu() 的返回值替换 self.lstm 属性。

这个错误是Chainer的一个包。它已被修复,正在等待检查。稍等片刻,就会提交。