Keras功能API多输入层

时间:2019-01-03 21:11:42

标签: python machine-learning keras

如何使用Keras Functional API定义多输入层?以下是我要构建的神经网络的示例。有三个输入节点。我希望每个节点都是不同长度的一维numpy数组。

这是我到目前为止所拥有的。基本上,我想定义一个具有多个输入张量的输入层。

from keras.layers import Input, Dense, Dropout, concatenate
from keras.models import Model

x1 = Input(shape =(10,))
x2 = Input(shape =(12,))
x3 = Input(shape =(15,))

input_layer = concatenate([x1,x2,x3])

hidden_layer = Dense(units=4, activation='relu')(input_layer)
prediction = Dense(1, activation='linear')(hidden_layer)

model = Model(inputs=input_layer,outputs=prediction)

model.summary()

Neural Network

代码给出错误。

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("x1_1:0", shape=(?, 10), dtype=float32) at layer "x1". The following previous layers were accessed without issue: []

稍后,当我拟合模型时,我将传入一列具有相应长度的numpy数组。

2 个答案:

答案 0 :(得分:2)

输入必须是您的Input()层:

model = Model(inputs=[x1, x2, x3],outputs=prediction)

答案 1 :(得分:2)

更改

model = Model(inputs=input_layer,outputs=prediction)

model = Model(inputs=[x1, x2, x3],outputs=prediction)