有人知道如何使用TensorFlow进行多任务深度学习吗?即,共享底层而不共享顶层。您能否分享一些示例代码?

最佳答案

具有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会优化所有损失的总和,这非常有用。

08-25 06:22