设置
我目前正在尝试为我的LSTM实现关注层。我知道,在哪里可以找到可行的示例,这不是我的问题的重点。我最终得到了这段代码片段
from keras.layers import Input,LSTM,TimeDistributed,Activation,Multiply,Lambda,Dense
from keras.models import Model
import keras.backend as K
inp = Input(shape=(None, 10))
lstm = LSTM(units=50, return_sequences=True)(inp)
att = TimeDistributed(Dense(1,activation="tanh"))(lstm)
att = K.squeeze(att,axis=-1)
att = Activation("softmax")(att)
final = Multiply()([att,lstm])
final = Lambda(lambda x: K.sum(x,axis=1))(final)
out = Dense(units=28,activation="softmax")(final)
model = Model(inp,out)
错误
当我尝试运行此代码时,出现以下错误
File "...\model.py", line 56, in create_model
out = Dense(units=28, activation="softmax")(final)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\base_layer.py", line 431, in __call__
self.build(unpack_singleton(input_shapes))
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\processing\lib\site-packages\keras\layers\core.py", line 866, in build
constraint=self.kernel_constraint)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\processing\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\processing\lib\site-packages\keras\engine\base_layer.py", line 249, in add_weight
weight = K.variable(initializer(shape),
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\processing\lib\site-packages\keras\initializers.py", line 209, in __call__
scale /= max(1., float(fan_in + fan_out) / 2)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
当我调试__call__
的{{1}}时,可以看到VarianceScaling Initializer
作为自变量由shape
给出,这引起了问题。当我在(None,28)
层之前执行打印语句以检查out
层的输出形状时
final
符合预期。因此,print(final)
>>Tensor("lambda_1/Sum:0", shape=(?, 50), dtype=float32)
函数中的形状实际上应该是__call__
。
问题
当我创建一个更简单的示例时
(50,28)
我的形状相同,但是完全没有错误。那么,错误的输入/输出形状处理是keras bug之上的问题吗?还是我错过了什么?