Function with torch.mm showing error while using torch.optim
I am kind of a newbie with PyTorch. Please forgive me if the question is childish. I am trying to minimize a function using PyTorch's optim. The function includes matrix multiplication. The details are given below.
First I have a tensor:
Xv.requires_grad_()
XT.requires_grad_()

My Objective Function:
def errorFun(x):
    ax = x[0]
    ay = x[1]
    x0 = x[2]
    y0 = x[3]

    A = torch.tensor([[ax, 0., x0], [0., ay, y0], [0., 0., 1.]], dtype=torch.float64)
    B = torch.tensor([[b11, b12, b13], [b21, b22, b23], [b31, b32, b33]], dtype=torch.float64)
    H = torch.mm(A, B)
    Ps = torch.mm(H, X)
    px = Ps[0,:]
    py = Ps[1,:]
    PX = torch.stack([px, py], dim=0)
    PX.requires_grad_()
    return mseloss(PX, XT)

I am minimizing it:
for ii in range(n_optim_steps):
    optimizer.zero_grad()
    loss = errorFun(params)

    #print('Step # {}, loss: {}'.format(ii, loss.item()))
    loss.backward()
    # Access gradient if necessary
    grad = params.grad.data
    optimizer.step()

But I am getting this error:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-54-84b874448a25&gt; in &lt;module&gt;()
     77     loss.backward()
     78     # Access gradient if necessary
---&gt; 79     grad = params.grad.data
     80     optimizer.step()
     81 

AttributeError: 'NoneType' object has no attribute 'data'

Thanks in advance.

I am not sure I understand your task. But it seems you are not using pytorch the way it was designed to be used.
There are 5 things you have to have:

Data for training;
A task you are interested in per forming;
A parameterized model (eg a neural network);
A cost function to be minimized;
An optimizer;

Consider the simple example:

Data: vectors containing random numbers;
Task: sum the numbers of the vector;
Model: Linear regressor (i.e: 1 layer neural net)
Cost function: Mean Squared Error
Optimizer: Stochastic Gradient Descent

The implementation:
import torch
import torch.nn as nn
from torch.optim import SGD

input_size = 5
model = nn.Linear(input_size, 1)
opt = SGD(model.parameters(), lr=0.01)
loss_func = nn.MSELoss()


for _ range(100):
    data = torch.rand(batch_size, input_size)
    target = data.sum(dim=1)
    
    opt.zero_grad()
    pred = model(data)
    loss = loss_func(pred, target)
    loss.backward()
    opt.step()

