Crazy behavior of masked_fill

I was writing a speech recognition code, but he got -inf for some values in the mel spectrum
I identified that it was caused by the extension part filled with 0 to align with the batch

So if you reset -inf to 0 with masked_fill as shown below, it should be restored.But it doesn’t work.Why is this? ?

model=VQVAE(hp).to("cuda")

inp=torch.randn(32,96000)
melns=torch.tensor([376,370,376,376,
                     280,270,300,260]).to("cuda")

inp[7][90000:]*=0

inp=inp.masked_fill(float('-inf') == inp,0.)

inpspec = spectrogram(inp).log2().to("cuda")
print(inpspec.size(),inpspec[7])

result

tensor([[ 9.2075,  7.7891,  5.3404,  ...,    -inf,    -inf,    -inf],
        [ 9.1560,  7.3915,  5.9799,  ...,    -inf,    -inf,    -inf],
        [ 9.0153,  5.9043,  6.7280,  ...,    -inf,    -inf,    -inf],
        ...,
        [ 9.5787,  9.9251, 10.2523,  ...,    -inf,    -inf,    -inf],
        [ 8.1497, 10.1253, 10.8566,  ...,    -inf,    -inf,    -inf],
        [ 9.8009, 10.7202, 11.2168,  ...,    -inf,    -inf,    -inf]],
       device='cuda:0')

Also, if you try float(‘-inf’) != inp,0., everything will be -inf

I don’t quite understand your code snippet since masked_fill won’t change anything.
You are initializing inp with values from a normal distribution in:

inp=torch.randn(32,96000)

which will not create -Inf values in it. Afterwards, you are zeroing out specific values:

inp[7][90000:]*=0

and are then trying to mask all -Inf in inp via:

inp=inp.masked_fill(float('-inf') == inp,0.)

while none were created at all.

spectrogram(inp) will create zeros where you’ve previously zeroed out the values:

out = spectrogram(inp)
out[7]

tensor([[376.9002, 215.6078,   3.0867,  ...,   0.0000,   0.0000,   0.0000],
        [293.0263, 160.2865, 128.0155,  ...,   0.0000,   0.0000,   0.0000],
        [273.2487, 144.9103, 331.6011,  ...,   0.0000,   0.0000,   0.0000],
        ...,
        [182.5855, 218.7867, 359.0259,  ...,   0.0000,   0.0000,   0.0000],
        [168.2010,  65.0044, 162.7816,  ...,   0.0000,   0.0000,   0.0000],
        [ 22.9736, 337.6405,   7.2782,  ...,   0.0000,   0.0000,   0.0000]])

and .log2() will then of course create -Infs which is also expected.

That’s it! ! appreciate
And I was making a very stupid mistake
sorry