简体   繁体   English

使用 Tensorflow 进行多任务深度学习

[英]Multitask deep learning with Tensorflow

Have someone tried doing multitask deep learning with TensorFlow?有人试过用 TensorFlow 进行多任务深度学习吗? That is, sharing the bottom layers while not sharing the top layers.也就是说,共享底层而不共享顶层。 An example with simple illustration would help a lot.一个带有简单插图的例子会有很大帮助。

There is an similar question here , the answer used keras.有一个类似的问题在这里,答案使用keras。

It's similar when just using tensorflow.仅使用 tensorflow 时也类似。 The idea is this: we can define multiple outputs of a network, and thus multiple loss functions (objectives).这个想法是这样的:我们可以定义网络的多个输出,从而定义多个损失函数(目标)。 We then tell optimizer to minimize a combined loss function, usually using a linear combination.然后我们告诉优化器最小化组合损失函数,通常使用线性组合。

A concept diagram概念图多任务深度学习图

This diagram is drawn according to this paper .该图是根据本文绘制的。

Let's say we are training a classifier that predict the digit in the image, with maximum 5 digits per image.假设我们正在训练一个分类器来预测图像中的数字,每张图像最多 5 个数字。 Here we defined 6 output layer: digit1 , digit2 , digit3 , digit4 , digit5 , length .在这里,我们定义6输出层: digit1digit2digit3digit4digit5length The digit layer should output 0~9 if there is such a digit, or X ( substitute it with an real number in practice ) if there isn't any digit in its position .digit层应输出0〜9,如果有这样的位,或X在实践中的实数代替它),如果没有在其位置的任何数字。 Same thing for length , it should output 0~5 if the image contains 0~5 digit, or X if it contains more than 5 digits. length相同,如果图像包含 0~5 位,则应输出 0~5,如果包含超过 5 位,则应输出X

Now to train it, we just add up all the cross entropy loss of each softmax function:现在要训练它,我们只需将每个 softmax 函数的所有交叉熵损失相加:

# Define loss and optimizer
lossLength = tf.log(tf.clip_by_value(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(length_logits, true_length)), 1e-37, 1e+37))
lossDigit1 = tf.log(tf.clip_by_value(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(digit1_logits, true_digit1)), 1e-37, 1e+37))
lossDigit2 = tf.log(tf.clip_by_value(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(digit2_logits, true_digit2)), 1e-37, 1e+37))
lossDigit3 = tf.log(tf.clip_by_value(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(digit3_logits, true_digit3)), 1e-37, 1e+37))
lossDigit4 = tf.log(tf.clip_by_value(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(digit4_logits, true_digit4)), 1e-37, 1e+37))
lossDigit5 = tf.log(tf.clip_by_value(tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(digit5_logits, true_digit5)), 1e-37, 1e+37))

cost = tf.add(
        tf.add(
        tf.add(
        tf.add(
        tf.add(cL,lossDigit1),
        lossDigit2),
        lossDigit3),
        lossDigit4),
        lossDigit5)

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM