Skip to content

Instantly share code, notes, and snippets.

@Ab1992ao
Last active June 2, 2021 22:12
Show Gist options
  • Save Ab1992ao/649ef58b0d57617eefa2550f32987405 to your computer and use it in GitHub Desktop.
Save Ab1992ao/649ef58b0d57617eefa2550f32987405 to your computer and use it in GitHub Desktop.
agg loss for multitask pipe
def softmax_loss(vectors):
anc, pos, neg = vectors
c = 0.5
anc = c * anc
pos = c * pos
neg = c * neg
pos_sim = tf.reduce_sum((anc * pos), axis=-1, keepdims=True)
neg_mul = tf.matmul(anc, neg, transpose_b=True)
neg_sim = tf.log(tf.reduce_sum(tf.exp(neg_mul), axis=-1, keepdims=True))
loss = tf.nn.relu(neg_sim - pos_sim)
return loss
pos_loss = tf.keras.losses.categorical_crossentropy(tgs_lbl_inp, seq_cls)
tx_loss = tf.keras.losses.binary_crossentropy(inp_tx_tag, tx_out)
triplet_loss = tf.keras.layers.Lambda(loss, name='loss')([anc_enc, pos_enc, neg_enc])
mean_op = tf.keras.layers.Lambda(lambda x: tf.reduce_mean(x))
avg_loss = cls_w[0]* mean_op(loss) + cls_w[1]* mean_op(pos_loss) + cls_w[2]*tx_loss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment