machine-learning - 使用 theano 扫描实现 LSTM,比使用循环慢得多

标签 machine-learning theano pylearn lstm

我正在使用 Theano/Pylearn2 在我自己的网络中实现 LSTM 模型。然而,我发现 Theano 扫描比使用普通循环慢得多。我使用了 Theano 分析器

<% time> <sum %> <apply time> <time per call> <type> <#call> <#apply> <Class name>
  95.4%    95.4%      25.255s       4.31e-02s     Py     586       3   theano.scan_module.scan_op.Scan
   1.8%    97.2%       0.466s       4.72e-05s     C     9864      41   theano.sandbox.cuda.basic_ops.GpuElemwise
   0.8%    97.9%       0.199s       8.75e-05s     C     2276      10   theano.sandbox.cuda.basic_ops.GpuAlloc
   0.7%    98.7%       0.196s       1.14e-04s     C     1724       8   theano.sandbox.cuda.blas.GpuDot22
   0.3%    99.0%       0.087s       1.06e-04s     C      828       3   theano.sandbox.cuda.basic_ops.GpuIncSubtensor
   0.2%    99.2%       0.051s       1.66e-04s     Py     310       2   theano.sandbox.cuda.basic_ops.GpuAdvancedSubtensor1

和行动,

<% time> <sum %> <apply time> <time per call> <type> <#call> <#apply> <Op name>
  77.2%    77.2%      20.433s       7.40e-02s     Py     276        1   forall_inplace,gpu,grad_of_lstm__layers}
  18.2%    95.4%       4.822s       1.56e-02s     Py     310        2   forall_inplace,gpu,lstm__layers}

所以很多很多的时间都花在了 Scan 上(这有点符合预期,但我没想到它这么慢)。

我的代码主体是

        def fprop(self, state_below, state_prev = 0, cell_prev = 0):
            if state_prev == None:
              state_prev = self.state_prev;
            if cell_prev == None:
              cell_prev = self.cell_prev;
            i_gate = T.nnet.sigmoid(T.dot(state_below,self.Wi) +
                                                            T.dot(state_prev,self.Ui));
            f_gate = T.nnet.sigmoid(T.dot(state_below,self.Wf) +
                                                            T.dot(state_prev,self.Uf));
            C = T.tanh(T.dot(state_below, self.Wc) +
                               T.dot(state_prev, self.Uc));
            C = i_gate * C + f_gate  * cell_prev;
            o_gate = T.nnet.sigmoid(T.dot(state_below,self.Wo) +
                                                            T.dot(state_prev,self.Uo) +
                                                            T.dot(C, self.Vo));
            h_out = o_gate * T.tanh(C);
            return h_out, C

我将扫描结果写为:

[h,c,out], _ = theano.scan(fn=self.fprop_with_output,
               sequences=[X.T,Y[:,1:].T],
               outputs_info=[dict(initial=h_,taps=[-1]), dict(initial=c_,taps=[-1]),None],n_steps=X.shape[1]-1);

我注意到的一件事是 Theano 扫描的类型使用 Python 实现(?),这就是它慢得离谱的原因吗?或者我做错了什么?为什么 Scan 是 Theano python 实现而不是 C 实现。

(我说使用循环更快,但它在运行时更快,对于大型模型,我无法在合理的时间内编译使用循环的版本)。

最佳答案

不久前有人问过这个问题,但我也遇到了同样的问题。答案是 GPU 上的扫描速度很慢。

参见:https://github.com/Theano/Theano/issues/1168

关于machine-learning - 使用 theano 扫描实现 LSTM,比使用循环慢得多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29825687/

相关文章:

python - 使用 pylearn2 创建单层神经网络

git - 是否有必要从我们的 CI 管道提交 DVC 文件?

python - 增量拟合sklearn RandomForestClassifier

python - Keras模型: Input shape dimension error for RL agent

python - 带有keras的CNN,准确性没有提高

machine-learning - 神经网络 : gpu vs no-gpu

machine-learning - 无法在 pylearn2 上编译 cuda_convnet 错误

r - R 中的偏最小二乘分类

python - 使用 theano 的矩阵三重积

machine-learning - 用于时间序列或序列预测的 Pylearn2 示例