如何在Keras中连接两个神经网络

如何在Keras中连接两个神经网络,keras,neural-network,deep-learning,artificial-intelligence,Keras,Neural Network,Deep Learning,Artificial Intelligence,我有一个关于Keras和深层神经网络的问题。我不知道这是否可能 结构: “神经网络1”获取输入“Input1”,并计算输出“output 1”。“神经网络2”的输入为“输出1”和“输入1” 你能告诉我这是否可行,我如何用Kers建立这样的神经网络吗? 谢谢使用图层和: 完整示例: from tensorflow.keras.layers import Concatenate, Dense, Input from tensorflow.keras.models import Model impo

我有一个关于Keras和深层神经网络的问题。我不知道这是否可能

结构: “神经网络1”获取输入“Input1”,并计算输出“output 1”。“神经网络2”的输入为“输出1”和“输入1”

你能告诉我这是否可行,我如何用Kers建立这样的神经网络吗? 谢谢

使用图层和:

完整示例:

from tensorflow.keras.layers import Concatenate, Dense, Input
from tensorflow.keras.models import Model

import numpy as np

x1 = np.random.normal(size=(1000, 10))
x2 = np.random.normal(size=(1000, 5))
y = (
    x1.mean(axis=-1, keepdims=True)
    + x2.mean(axis=-1, keepdims=True)
    + np.random.normal(scale=0.05, size=(1000, 1))
)

input_1 = Input(shape=(10,))
output_1 = Dense(1)(input_1)
input_2 = Input(shape=(5,))
concat_2 = Concatenate()([output_1, input_2])
output_2 = Dense(1)(concat_2)
model = Model(inputs=[input_1, input_2], outputs=[output_2])

model.compile(loss='mse', optimizer='sgd')
hist = model.fit([x1, x2], y, epochs=500)
from tensorflow.keras.layers import Concatenate, Dense, Input
from tensorflow.keras.models import Model

import numpy as np

x1 = np.random.normal(size=(1000, 10))
x2 = np.random.normal(size=(1000, 5))
y = (
    x1.mean(axis=-1, keepdims=True)
    + x2.mean(axis=-1, keepdims=True)
    + np.random.normal(scale=0.05, size=(1000, 1))
)

input_1 = Input(shape=(10,))
output_1 = Dense(1)(input_1)
input_2 = Input(shape=(5,))
concat_2 = Concatenate()([output_1, input_2])
output_2 = Dense(1)(concat_2)
model = Model(inputs=[input_1, input_2], outputs=[output_2])

model.compile(loss='mse', optimizer='sgd')
hist = model.fit([x1, x2], y, epochs=500)