3

I want to set INF value to matrix by mask matrix, just like pytorch code:

scores.data.masked_fill_(y_mask.data, -float('inf'))

I try to use tf.map_fn to implement that, but the performance is too slow. So does tensorflow have any efficient function to implement that?

Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
Jack.Liu
  • 61
  • 6
  • I don't see any direct method but you can simply do it if you have the mask indices which you want to fill in using a specific value. you can see this example - https://gist.github.com/jihunchoi/f1434a77df9db1bb337417854b398df1. – Wasi Ahmad Nov 23 '17 at 18:09

4 Answers4

3

I have used a math calculate method to instead. It's valid and much faster.

def mask_fill_inf(matrix, mask):
    negmask = 1 - mask
    num = 3.4 * math.pow(10, 38)
    return (matrix * mask) + (-((negmask * num + num) - num))

Do anyone have the better method?

Jack.Liu
  • 61
  • 6
0

I am inspired by the above answer.

masked_fill ==>

def mask_fill_inf(matrix, mask):
    num = 3.4 * math.pow(10, 38)
    return (matrix + (-(((mask * num) + num) - num)))
skytree
  • 1,060
  • 2
  • 13
  • 38
0

Maybe you can use:

tf.where(mask, default_value, data)

reference:https://github.com/tensorflow/tensorflow/issues/41617

Hao Hu
  • 1
0

May be this one

def masked_fill(tensor, mask, value):
    return tf.where(mask, tf.fill(tf.shape(tensor), value), tensor)