Windows下 Keras 报错 TypeError: lambda() got an unexpected keyword argument ‘name’
环境
- Windows10 x64
- Keras 0.3.3
- Theano 0.8.0
问题
这种情况貌似只发生在Windows系统下用Keras跑部分实例代码时,Ubuntu下没见过。
以Keras的示例代码mnist_irnn.py为例,直接运行时会报错如下:
Using Theano backend.
Using gpu device 0: GeForce GTX 860M (CNMeM is disabled, CuDNN not available)
('X_train shape:', (50000L, 784L, 1L))
(50000L, 'train samples')
(10000L, 'test samples')
Evaluate IRNN...
Traceback (most recent call last):
File "<ipython-input-1-7becf7f3cbe9>", line 1, in <module>
runfile('D:/File/code/python/testRNN/mnist_irnn.py', wdir='D:/File/code/python/testRNN')
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "D:/File/code/python/testRNN/mnist_irnn.py", line 67, in <module>
activation='relu', input_shape=X_train.shape[1:]))
File "C:\Anaconda2\lib\site-packages\keras\keras\layers\recurrent.py", line 257, in __init__
super(SimpleRNN, self).__init__(**kwargs)
File "C:\Anaconda2\lib\site-packages\keras\keras\layers\recurrent.py", line 132, in __init__
super(Recurrent, self).__init__(**kwargs)
File "C:\Anaconda2\lib\site-packages\keras\keras\layers\core.py", line 60, in __init__
self.set_input_shape((None,) + tuple(kwargs['input_shape']))
File "C:\Anaconda2\lib\site-packages\keras\keras\layers\core.py", line 223, in set_input_shape
self.build()
File "C:\Anaconda2\lib\site-packages\keras\keras\layers\recurrent.py", line 270, in build
name='{}_W'.format(self.name))
TypeError: <lambda>() got an unexpected keyword argument 'name'
解决方法
直接点击错误链接的话,可能会被带到如recurrent.py,core.py等其他代码上,但实际上正如错误最后一行所说,这主要是lambda表达式引起的错误,问题就在这个代码块:
model.add(SimpleRNN(output_dim=hidden_units,
init=lambda shape: normal(shape, scale=0.001),
inner_init=lambda shape: identity(shape, scale=1.0),
activation='relu', input_shape=X_train.shape[1:]))
这里需要把lambda表达式直接改写为Keras在initializations.py中预先写好的默认构造函数,如'uniform'
,'normal'
,'identity'
等,就像这样:
model.add(SimpleRNN(output_dim=output_dim,
init='normal',#lambda shape: normal(shape, scale=0.001),
inner_init='identity',#lambda shape: identity(shape, scale=1.0),
activation='relu', input_shape=X_train.shape[1:]))
这样就没问题了。
如果实在想修改初始化函数中的各个参数的话,呃,那就去本地的initializations.py源代码中改吧。
嗯嗯,我知道,这方法是有点挫,可我实在想不出其他招儿了,凑合过呗,还能离咋地…
BTW,还是不知道为啥这个错误只发生在Windows下,在Ubuntu上毛都不用改,还跑得飞快(有cuDNN法术加持就是吊,手动眼斜)。