Torch.autograd returns nonetype?

Beya_Bunas: <p>ctrl-f “torch.autograd.grad(”</p>
<p>this is terminal output of print statements:</p>
<p>tensor(1.3312, grad_fn=)<br>
tensor(1.7920, grad_fn=)<br>
(None,)<br>
Traceback (most recent call last):<br>
line 71, in <br>
est= unadjusted_langevin.sample(alpha)<br>
line 34, in sample<br>
loss_term = torch.Tensor(self.step_size*grad_f_at_x)<br>
TypeError: must be real number, not NoneType</p>
<p>Tried also removing allow_unused=True, then i get the error 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.</p>
<p>import torch<br>
from precondSGLD import pSGLD<br>
import copy</p>
<p>class UnadjustedLangevin(object):<br>
def <strong>init</strong>(self, x, func, step_size, iteration_limit):<br>
super(UnadjustedLangevin, self).<strong>init</strong>()</p>
<pre><code>    self.func = func
    self.step_size = step_size
    self.counter = 0.0
    self.x = x
    self.x.requires_grad = True
    self.iteration_limit = iteration_limit
    self.samples = []


#pull one sample x_k1 from initial sample x_k
def sample(self,alpha):
    #output = self.R.matmul(self.normal.sample()) + self.mu
    #output = 0

    #need to take -log of fx_output as that's f
    fx_output = torch.Tensor(self.func(self.x))

    fx_output.requires_grad = True
    print(torch.norm(self.x))
    print(torch.norm(fx_output))
    grad_f_at_x = torch.autograd.grad(torch.norm(self.x), torch.norm(fx_output), allow_unused=True)

    print(grad_f_at_x)

    loss_term = torch.Tensor(self.step_size*grad_f_at_x)
    z_k = torch.normal(torch.zeros_like(self.x), torch.eye(2))
    x_k1 = self.x - loss_term + torch.sqrt(2*self.step_size)*z_k


    self.samples.append(x_k1)

    self.counter += 1
    return self.samples
</code></pre>
<p>import torch<br>
from sampling_algs import *<br>
import numpy as np<br>
from scipy.linalg import logm<br>
import matplotlib.pyplot as plt<br>
import copy<br>
np.random.seed(14)<br>
torch.manual_seed(14)</p>
<p>#<span class="hashtag">#This</span> is the main script</p>
<p>torch.set_default_tensor_type(‘torch.FloatTensor’) <span class="hashtag">#cuda</span> not available<br>
<span class="hashtag">#torch</span>.set_default_tensor_type(‘torch.cuda.FloatTensor’) <span class="hashtag">#cuda</span> available</p>
<p>class GaussianDistribution(object):<br>
def <strong>init</strong>(self, mean, cov_matrix):<br>
super(GaussianDistribution, self).<strong>init</strong>()</p>
<pre><code>    self.mu = mean
    self.cov = cov_matrix
    self.precision = torch.inverse(cov_matrix)

    self.R = torch.cholesky(self.cov)
    self.normal = torch.distributions.normal.Normal(torch.zeros_like(mean), 
        torch.ones_like(mean))

def pdf_func1(self, alpha):
    #mixed Gaussian
    alpha = torch.Tensor(alpha)
    #print(torch.zeros_like(alpha))
    loss_val = (torch.normal(-torch.zeros_like(alpha), torch.eye(2)) + 
    torch.normal(torch.zeros_like(alpha), torch.eye(2)))
    return 0.5*loss_val
    #return 0.5*(((x - mean).T).matmul(self.precision)).matmul(x - self.mean)

def sample(self):
    #print(self.normal.sample())
    return self.normal.sample() + self.mu 
</code></pre>
<p><span class="hashtag">#MAIN</span><br>
if <strong>name</strong> == ‘<strong>main</strong>’:<br>
<span class="hashtag">#working</span> in n=2<br>
dim = 2</p>
<pre><code>#############################
#Log Concave Test Cases
#alpha=torch.Tensor([0.1,0.1])
#alpha=torch.Tensor([0.3,0.3])
alpha=torch.Tensor([0.5,0.5]) #log concave value of a mag&lt;1

#Non-Log Concave Test Cases
#alpha=torch.Tensor([1,1]) #non log concave value of a mag&gt;1 
#alpha=torch.Tensor([2,2])
#alpha=torch.Tensor([3,3])
##############################
gaussian_dist = GaussianDistribution(alpha, torch.eye(dim)) 
step_size = 1
p0 = torch.normal(torch.zeros_like(alpha), torch.eye(2))

##RUN ULA algorithm. should output an array of values to plot
unadjusted_langevin = UnadjustedLangevin(p0, gaussian_dist.pdf_func1, step_size, iteration_limit=int(1e4))

hist_samples = []
loss_log = []


#Collect 10,000 samples
for j in range(int(1e4)):
    est= unadjusted_langevin.sample(alpha)
    if j%3 == 0:
        hist_samples.append(est.cpu().numpy())

est_samples = np.array(hist_samples)[200:]
num_samples = est_samples.shape[0]


#TODO: change gaussian_dist here to the mixture Gaussian in problem 9
num_samples = 10000
true_samples = np.zeros([num_samples, 2])
for j in range(num_samples):
    true_samples[j, :] = gaussian_dist.sample().cpu().numpy()</code></pre>