Restricted-Boltzmann-Machines-in-pytorch icon indicating copy to clipboard operation
Restricted-Boltzmann-Machines-in-pytorch copied to clipboard

numerical instability in free_energy

Open limbo-wg opened this issue 4 years ago • 2 comments

Hello~

Thank you very much for sharing the code, which really helps me a lot.

But I find some problem when I use the code in

RBM.ipynb

def free_energy(self,v):
	vbias_term = v.mv(self.v_bias)
	wx_b = F.linear(v,self.W,self.h_bias)
	hidden_term = wx_b.exp().add(1).log().sum(1)
	return (-hidden_term - vbias_term).mean()

I found that there may be numerical instability in free_energy. In hidden_term = wx_b.exp().add(1).log().sum(1), if wx_b contains elements that greater than or equal to 88, the result of wx_b.exp() is inf. And this may cause the result of loss to become NAN.

It could be fixed by making the following change:

hidden_term = wx_b.exp().clamp(max=87).add(1).log().sum(1)

Hope to hear from you ~

Thanks in advance! : )

limbo-wg avatar Nov 19 '20 13:11 limbo-wg

ping~ @odie2630463

Justobe avatar Jan 13 '21 04:01 Justobe

Hi , I am so sorry for replying late, I think it's numerical issue cause by log. Using clamp maybe a way to solve , but I will check if there have other implements and update it. (Old project to me haha)

Thanks you point out this issue!

odie2630463 avatar Jan 13 '21 05:01 odie2630463