april-tools / cirkit

a python framework to build, learn and reason about probabilistic circuits and tensor networks
https://cirkit-docs.readthedocs.io/en/latest/
GNU General Public License v3.0
80 stars 4 forks source link

Using autograd.grad to obtain the gradients of output w.r.t. inputs #289

Closed isurulucky closed 1 month ago

isurulucky commented 1 month ago

Hi,

I was trying to use torch.autograd.grad to obtain the gradients of the log likelihoods w.r.t. to the input data in the learning-a-circuit notebook, right after the forward pass in the training loop:

dll_dbatch = torch.autograd.grad(mean_log_likelihoods, batch, create_graph=True, grad_outputs=torch.ones_like(mean_log_likelihoods))[0]

This gives me the RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior error, which indicate the actual input to the compiled torch module could be a different tensor from what we use in the forward pass at:

log_likelihoods = cct(batch)

Can this be due to how the cirkit backend works with pytorch? Is there a way to work around this?

Thank you.

isurulucky commented 1 month ago

This was due to a mistake from my end, where I had used Categorical distributions for the input nodes. I hacked around to use Gaussian distributions for input nodes, and did not get this issue thereafter. In the image_data template method, its not possible to use Gaussians as inputs atm. Will open a separate issue to discuss the possibility of allowing Gaussians to be used in image_data method.