如何使用Keras合并或连接两个顺序模型?

摩纳克·迪

我有一个包含两个文本字段的数据集,在标记化之后,我制作了两个顺序模型,试图合并或合并,但是合并时遇到错误。

我已经建立了两个顺序模型,并且尝试不使用Keras Functional API来合并它们。

# define the model
model1 = Sequential()
model1.add(Embedding(vocabulary_size_1, embedding_size, input_length=MAXLEN))
model1.add(Flatten())
model1.add(Dense(op_units, activation='softmax'))

# define the model
model2 = Sequential()
model2.add(Embedding(vocabulary_size_2, embedding_size, input_length=MAXLEN))
model2.add(Flatten())
model2.add(Dense(op_units, activation='softmax'))

merged = concatenate(axis=1)
merged_model=merged([model1.output, model2.ouput])

TypeError                                 Traceback (most recent call last)
<ipython-input-76-79cf08fec6fc> in <module>
----> 1 merged = concatenate(axis=1)
      2 merged_model=merged([model1.output, model2.ouput])

TypeError: concatenate() missing 1 required positional argument: 'inputs'

我期待不使用Keras Functional API的方法

Ashwin Geet D'Sa

concatenate()功能要求您指定要串联的模型。

merged = concatenate([model1,model2],axis=1)但是,轴必须是axis = -1(您可以在yopur情况下使用任何合适的方法。)

您的代码可以通过以下功能方式进一步编写:

inputs = Input(shape=(vocabulary_size,embedding_size), dtype='float32')

model1=Embedding(vocabulary_size, embedding_size)(inputs)
model1=Flatten()(model1)
model1=Dense(op_units, activation='softmax')(model1)

model2=Embedding(vocabulary_size, embedding_size)(inputs)
model2=Flatten()(model2)
model2=Dense(op_units,activation='softmax')(model2)

merged = concatenate([model1,model2],axis=-1)

model=Model(inputs=inputs,outputs=merged)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章