【问题标题】:Keras: Merge/Concatenate Layer: TypeError: __init__() got multiple values for argument 'axis'Keras:合并/连接层:TypeError:__init__() 为参数“轴”获得了多个值
【发布时间】:2018-04-29 09:43:30
【问题描述】:

我创建了以下网络。这个想法是结合leftright的输出,然后发送到一个LSTM模型。

EMBED_DIM = 4
look_back = 6
feature_num = 2
ENCODE_DIM = 676

left = Sequential()
left.add(Dense(EMBED_DIM,input_shape=(ENCODE_DIM,)))
left.add(RepeatVector(look_back))
left.add(Reshape((look_back,EMBED_DIM)))

right = Sequential()
right.add(Lambda(lambda x: x,input_shape=(look_back,feature_num)))


# create and fit the LSTM network
model = Sequential()
model.add(Concatenate([left, right], axis = 2,input_shape=(look_back, EMBED_DIM + feature_num) ))
model.add(LSTM(8, input_shape=(look_back,feature_num + EMBED_DIM)))

model.add(Dense(2))
model.compile(loss='mean_squared_error', optimizer='adam')

我正在尝试连接左右输出,然后将新张量发送到 LSTM 模型。

但是,我收到以下错误:


TypeError                                 Traceback (most recent call last)
<ipython-input-156-275f5597cdad> in <module>()

---> 37 model.add(Concatenate([left, right], axis = 2,input_shape=(look_back, EMBED_DIM + feature_num) ))
     38 model.add(LSTM(8, input_shape=(look_back,feature_num + EMBED_DIM)))
     39 

TypeError: __init__() got multiple values for argument 'axis'

知道我做错了什么吗?我可以添加Concatenate 层作为模型的第一层吗?谢谢!

【问题讨论】:

    标签: python lambda keras keras-layer keras-2


    【解决方案1】:

    顺序模型并不意味着有分支。使用功能 API 模型。

    让我们从左侧和右侧获取“张量”:

    leftOutput = left.output    
    rightOutput = right.output
    

    现在,Concatenate 是一个遵循所有其他层的相同逻辑的层。 (首先创建它,然后使用输入张量调用它):

    #first parentheses: create the layer / second parentheses: call the layer with inputs
    output = Concatenate(axis=2)([leftOutput,rightOutput])
    

    让我们将模型的其余部分也保留为函数式 API:

    output = LSTM(8)(output)
    output = Dense(2)(output)
    

    现在我们创建模型,告诉它输入和输出是什么:

    inputTensorLeft = left.input
    inputTensorRight = right.input    
    
    fullModel = Model([inputTensorLeft,inputTensorRight], output)
    

    请注意,您最终得到了三个模型,但其中一个包含另外两个。它们共享相同的权重。如果您正在训练共享路径,则训练一个将训练其他人。

    【讨论】:

    • 谢谢!但我希望在一次迭代中一起评估左、右和模型中的权重。即我希望最终输出中的误差可以反向传播到左右,并调整它们的权重。上述方法会这样做吗?
    • 是的。所有的权重都是共享的。训练完整的模型,在此代码中为fullModel
    • 非常感谢!真的很有帮助!
    • 请记住,在这种情况下fit 方法将接受两个输入:fullModel.fit([inputs1,inputs2], targets) -- 如果两个输入相同,没问题,但在这样的列表中传递两次。
    猜你喜欢
    • 2018-01-16
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2022-11-17
    • 2022-08-22
    • 2019-01-03
    相关资源
    最近更新 更多