在keras中,我想训练一组共享某些图层的模型.它们具有以下形式:
x ---> f(x)---> g_1(f(x))
x ---> f(x)---> g_2(f(x))
...
x ---> f(x)---> g_n(f(x))
这里f(x)是一些非平凡的共享层.g_1到g_n有其特定参数.
在每个训练阶段,数据x被馈送到n个网络中的一个,例如,第i个.然后通过基于梯度的优化器最小化/减小g_i(f(x))上的损失.我怎样才能定义和训练这样的模型?
提前致谢!
您可以使用功能模型轻松完成此操作.
一个小例子..你可以建立它:
import numpy as np from keras.models import Model from keras.layers import Dense, Input X = np.empty(shape=(1000,100)) Y1 = np.empty(shape=(1000)) Y2 = np.empty(shape=(1000,2)) Y3 = np.empty(shape=(1000,3)) inp = Input(shape=(100,)) dense_f1 = Dense(50) dense_f2 = Dense(20) f = dense_f2(dense_f1(inp)) dense_g1 = Dense(1) g1 = dense_g1(f) dense_g2 = Dense(2) g2 = dense_g2(f) dense_g3 = Dense(3) g3 = dense_g3(f) model = Model([inp], [g1, g2, g3]) model.compile(loss=['mse', 'binary_crossentropy', 'categorical_crossentropy'], optimizer='rmsprop') model.summary() model.fit([X], [Y1, Y2, Y3], nb_epoch=10)
编辑:
根据您的意见,您可以随时根据您的培训需求制作不同的模型并自行编写培训循环.您可以在model.summary()
所有模型中看到共享初始图层.这是示例的扩展
model1 = Model(inp, g1) model1.compile(loss=['mse'], optimizer='rmsprop') model2 = Model(inp, g2) model2.compile(loss=['binary_crossentropy'], optimizer='rmsprop') model3 = Model(inp, g3) model3.compile(loss=['categorical_crossentropy'], optimizer='rmsprop') model1.summary() model2.summary() model3.summary() batch_size = 10 nb_epoch=10 n_batches = X.shape[0]/batch_size for iepoch in range(nb_epoch): for ibatch in range(n_batches): x_batch = X[ibatch*batch_size:(ibatch+1)*batch_size] if ibatch%3==0: y_batch = Y1[ibatch*batch_size:(ibatch+1)*batch_size] model1.train_on_batch(x_batch, y_batch) elif ibatch%3==1: y_batch = Y2[ibatch*batch_size:(ibatch+1)*batch_size] model2.train_on_batch(x_batch, y_batch) else: y_batch = Y3[ibatch*batch_size:(ibatch+1)*batch_size] model3.train_on_batch(x_batch, y_batch)