nf-soft-mining icon indicating copy to clipboard operation
nf-soft-mining copied to clipboard

About log operator in LMC equation

Open weixiang-zhang opened this issue 1 year ago • 4 comments

Hi,

Thanks for your contribution. I am wondering that why there is not log operator in the codebase? Here is code in lmc.py:

net_grad.mul_(self.a).add_(self.noise, alpha=self.b)
self.prev_samples.add_(net_grad)

But the equation(10) in paper is: image

weixiang-zhang avatar Sep 16 '24 11:09 weixiang-zhang

Hello @zwxdxcm, Thank you for your interest in this work. Great question. The gradient of log Q is equal to the gradient of Q divided by Q. I perform this division in line 280 of "examples/train_ngp_nerf_prop.py".

shakibakh avatar Sep 23 '24 23:09 shakibakh

Thank you! but I still have a question... in line 280, the code is:

net_grad = data['points_2d'].grad.detach()
loss_per_pix = loss_per_pix.detach()
net_grad = net_grad / ((grad_scaler._scale * (correction * loss_per_pix).unsqueeze(1))+ torch.finfo(net_grad.dtype).eps)

here we ignore scale * eps. correction = 1/[Q(x)]^\alpha. netgrad is the gradiant of total loss. How does it equal to grad(Q(x)) / Q(x)? It seems like grad(L)/[1/[Q(x)]^\alpha * L]. Is there anything i ignored? Thank you again !

weixiang-zhang avatar Sep 28 '24 03:09 weixiang-zhang

Here is my understanding:

  1. approximate correction(x) = Q(x) thus → ▽log(Q(x)) = ▽Q(x)/Q(x) = ▽correction(x)/correction(x) I dont know why to multiply loss_per_pix again given that there is loss_per_pix.mul_(correction) in line 267

weixiang-zhang avatar Sep 28 '24 09:09 weixiang-zhang

Hello, I have the same question about the code in line 280 of "examples/train_ngp_nerf_prop.py".

According to the previous code, the correction and loss_per_pix are respectively: $$correction = \frac{1}{sg(Q(\mathbf{x}))^{\alpha}}$$ $$loss\_per\_pix=\frac{Q(\mathbf{x})^{2}}{sg(Q(\mathbf{x}))^{\alpha}}$$

Then the net_grad should be:

$$\begin{align} {netgrad} &= \frac{\partial loss\_per\_pix}{\partial\mathbf{x}} = \frac{2Q(\mathbf{x})}{sg(Q(\mathbf{x}))^{\alpha}} \nabla Q(\mathbf{x}) \ \end{align}$$

$$\begin{align}\nabla\log Q(\mathbf{x}) &= \frac{1}{Q(\mathbf{x})} \nabla Q(\mathbf{x}) \ &=\frac{1}{Q(\mathbf{x})} \frac{netgrad \cdot sg(Q(\mathbf{x}))^{\alpha}}{2Q(\mathbf{x})} \ &=\frac{netgrad}{2\cdot loss\_per\_pix}\end{align}$$

I don't know why net_grad in the code is need to divide by correction again.

net_grad = net_grad / ((grad_scaler._scale * (correction * loss_per_pix).unsqueeze(1))+ torch.finfo(net_grad.dtype).eps)

Maybe my understanding about the partial of loss_per_pix is wrong... Could u give me some advice about that? Thank u very much.

qiyun71 avatar Nov 26 '24 06:11 qiyun71

Thank you for catching this mismatch. There was an incorrect correction term in the denominator, which I’ve now fixed. This must have happened when I was cleaning up the code.

shakibakh avatar Dec 02 '25 04:12 shakibakh