Вот простой пример, показывающий, как это сделать, ключ состоит в том, чтобы поместить модель нейронной сети в функцию
import keras
import numpy as np
import tensorflow as tf
from keras.layers import Input, Dense
tf.reset_default_graph()
# assume this is your model
def nn_model(input_x, n):
feature_maker = Dense(n, activation='relu')(input_x)
feature_maker = Dense(20, activation='relu')(feature_maker)
feature_maker = Dense(1, activation='linear')(feature_maker)
return feature_maker
input_layer = Input(shape=(3, ))
output_1 = nn_model(input_layer, 10)
output_2 = nn_model(input_layer, 20)
output_3 = nn_model(input_layer, 30)
model = keras.models.Model(inputs=input_layer, outputs=[output_1, output_2, output_3])
Вы можете построить эту модель по
from keras.utils.vis_utils import plot_model
plot_model(model, show_shapes=True)
* 1006. * Модель выглядит как

И эту модель можно обучить по
model.compile(loss='mse', optimizer='adam')
# example training set
x = np.linspace(1, 90, 270).reshape(90, 3)
y1 = np.random.rand(90)
y2 = np.random.rand(90) * 2
y3 = np.random.rand(90) * 3
model.fit(x, [y1, y2, y3], batch_size=32, epochs=10)