首页 > 解决方案 > 使用估计作为特征的多任务学习

问题描述

我有具有 3 个分类头的多任务网络[A, B, C]。我想使用 headA的输出作为第一个密集层的输入B and C

是否应该为反向传播做一些特别的事情,因为我认为从不B and C应该流回的梯度A,因为它已经被计算过并且应该作为常数处理。

有没有人有这样的代码示例?

标签: tensorflowkerasneural-network

解决方案


你可以试试:

A_layer = tf.keras.layers.Dense(5)(x)
A_head= tf.keras.layers.Dense(5)(A_layer)
A_logic = tf.keras.layers.Dense(1)(A_head)
A_loss = tf.losses.sigmoid_cross_entropy(A_y,A_logic)

B_layer = tf.keras.layers.Dense(5)(tf.stop_gradient(A_logic))
B_head= tf.keras.layers.Dense(5)(B_layer)
B_logic = tf.keras.layers.Dense(1)(B_head)
B_loss = tf.losses.sigmoid_cross_entropy(B_y,B_logic)

C_layer = tf.keras.layers.Dense(5)(tf.stop_gradient(A_logic))
C_head= tf.keras.layers.Dense(5)(C_layer)
C_logic = tf.keras.layers.Dense(1)(C_head)
C_loss = tf.losses.sigmoid_cross_entropy(C_y,C_logic)

total_loss = A_loss + B_loss + C_loss

train_op = tf.train.AdamOptimizer().minimize(total_loss)

推荐阅读