有没有人知道如何使用TensorFlow进行多任务深度学习?也就是说,共享底层而不共享顶层。你能不能分享一些示例代码?
答案 0 :(得分:5)
使用TensorFlow后端的Keras可以轻松完成此操作。功能API是为这些用例设计的。看一下functional API guide.这是一个共享图层的LSTM示例,取自上面的指南:
# this layer can take as input a matrix
# and will return a vector of size 64
shared_lstm = LSTM(64)
# when we reuse the same layer instance
# multiple times, the weights of the layer
# are also being reused
# (it is effectively *the same* layer)
encoded_a = shared_lstm(tweet_a)
encoded_b = shared_lstm(tweet_b)
# we can then concatenate the two vectors:
merged_vector = merge([encoded_a, encoded_b], mode='concat', concat_axis=-1)
# and add a logistic regression on top
predictions = Dense(1, activation='sigmoid')(merged_vector)
# we define a trainable model linking the
# tweet inputs to the predictions
model = Model(input=[tweet_a, tweet_b], output=predictions)
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit([data_a, data_b], labels, nb_epoch=10)
当您训练具有多个输出的Keras模型时,您可以为每个输出定义一个损失函数,Keras将优化所有损失的总和,这非常有用。