Theano AttributeError: 'module' object has no attribute 'relu'

Theano AttributeError: 'module' object has no attribute 'relu'

我想使用 theano.tensor.nnet.relu,但我一直收到此错误:

AttributeError: 'module' object has no attribute 'relu' 

我已经按照 theano's documentation 中描述的 sudo pip install --upgrade theano 命令更新了 theano,我也尝试过 sudo pip install --upgrade --no-deps theano。都没有用,我仍然得到同样的错误。

我正在尝试 theano -v 来确认我已经安装了最新版本,但随后出现以下错误:-bash: theano: command not found

所以我的两个问题是:

theano.tensor.nnet模块在最新版本中仅支持relu。为了使用它,您需要从 github 安装最新版本或等到下一个版本。

或者,您可以这样实现:

def relu(x):
    return T.maximum(x, 0.)

这可能不合适,但它会为您提供结果和梯度。

  1. 查看theano版本可以运行下面的代码:

    import theano
    print theano.__version__
    
  2. 您应该按照 here 中的说明获取最新版本

其实relu函数写起来很简单,你可以创建自己的relu函数,喜欢eickenberg的回答,或者喜欢theano.tensor.nnet风格:

    def relu(x):   
       return 0.5 * (x + abs(x))

relu 适用于 theano >= 0.7.1。我的猜测是 pip 链接到 theano==0.7.

你可以通过pip freeze查看theano版本:

pip freeze | grep Theano

所以你必须安装最新的 theano 并将 pip 指向 theano git repo :

pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git

另请注意,relu 是函数,不是模块,因此要访问它,您必须使用以下导入之一:

from theano.tensor.nnet import relu # access `relu` as is ..
import theano.tensor.nnet as theano_nnet #access `relu` as `theano_nnet.relu`