【问题标题】:how to do multitask deep learning with tensorflow如何使用 tensorflow 进行多任务深度学习
【发布时间】:2016-06-16 13:44:42
【问题描述】:

有人知道如何使用 TensorFlow 进行多任务深度学习吗?也就是说,共享底层而不共享顶层。可以分享一些示例代码吗?

【问题讨论】:

标签: neural-network tensorflow deep-learning


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2017-12-09
    • 2017-05-20
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多