Keras顺序模型的多个输入 [英] Multiple inputs to Keras Sequential model

查看:205
本文介绍了Keras顺序模型的多个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并两个模型的输出,并使用keras顺序模型将它们作为第三模型的输入. 型号1:

I am trying to merge output from two models and give them as input to the third model using keras sequential model. Model1 :

inputs1 = Input(shape=(750,))
x = Dense(500, activation='relu')(inputs1)
x = Dense(100, activation='relu')(x)

Model1:

inputs2 = Input(shape=(750,))
y = Dense(500, activation='relu')(inputs2)
y = Dense(100, activation='relu')(y)

Model3:

merged = Concatenate([x, y])
final_model = Sequential()
final_model.add(merged)
final_model.add(Dense(100, activation='relu'))
final_model.add(Dense(3, activation='softmax'))

直到这里,我的理解是,来自两个模型的输出x和y被合并并作为输入提供给第三个模型.但是当我很喜欢这一切时,

Till here, my understanding is that, output from two models as x and y are merged and given as input to the third model. But when I fit this all like,

module3.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
module3.fit([in1, in2], np_res_array)

in1和in2是尺寸为10000 * 750的两个numpy ndarray,其中包含我的训练数据,而np_res_array是相应的目标.
由于列表"对象没有属性"shape",因此出现了错误据我们所知,这就是我们为模型提供多个输入的方式,但是这是什么错误呢?我该如何解决?

in1 and in2 are two numpy ndarray of dimention 10000*750 which contains my training data and np_res_array is the corresponding target.
This gives me error as 'list' object has no attribute 'shape' As far as know, this is how we give multiple inputs to a model, but what is this error? How do I resolve it?

推荐答案

您不能使用顺序API来执行此操作.这是由于两个原因:

You can't do this using Sequential API. That's because of two reasons:

    顾名思义,
  1. 顺序模型是一系列的层,其中每个层都直接连接到其上一层,因此它们不能具有分支(例如合并层,多个输入/输出层,跳过连接等) ).

  1. Sequential models, as their name suggests, are a sequence of layers where each layer is connected directly to its previous layer and therefore they cannot have branches (e.g. merge layers, multiple input/output layers, skip connections, etc.).

顺序API的add()方法接受Layer实例作为其参数,而不接受Tensor实例.在您的示例中,merged是张量(即连接层的输出).

The add() method of Sequential API accepts a Layer instance as its argument and not a Tensor instance. In your example merged is a Tensor (i.e. concatenation layer's output).

此外,使用Concatenate层的正确方法是这样的:

Further, the correct way of using Concatenate layer is like this:

merged = Concatenate()([x, y])

但是,您也可以使用concatenate(注意小写的"c"),它是等效的功能接口,如下所示:

However, you can also use concatenate (note the lowercase "c"), its equivalent functional interface, like this:

merged = concatenate([x, y])

最后,要构建第三个模型,您还需要使用功能性API .

Finally, to be able to construct that third model you also need to use the functional API.

这篇关于Keras顺序模型的多个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆