def masked_softmax(tensor, mask, expand=2, axis=1):
"""Masked soft-max using Lambda and merge-multiplication.
Args:
tensor: tensor containing scores
mask: mask for tensor where 1 - means values at this position and 0 - means void, padded, etc..
expand: axis along which to repeat mask
axis: axis along which to compute soft-max
Returns:
masked soft-max values
"""
mask = tf.expand_dims(mask, axis=expand)
exponentiate = Lambda(lambda x: K.exp(x - K.max(x, axis=axis, keepdims=True)))(tensor)
masked = tf.multiply(exponentiate, mask)
div = tf.expand_dims(tf.reduce_sum(masked, axis=axis), axis=axis)
predicted = tf.divide(masked, div)
return predicted
评论列表
文章目录