requires_grad = False seems not working in my case
I received a Cannot insert a Tensor that requires grad as a constant. Consider making it a parameter or input, or detaching the gradient error with tensor W.
W has the size of (10,10) and grad_fn=&lt;DivBackward0&gt;. The error happens at the second line
def muy(self, x):
    V = torch.tensor(self.W - self.lambda_ * torch.eye(self.ENCODING_DIM), requires_grad=False)
    return -0.5 * V.inverse().mm(self.b + self.lambda_ * x[:, None])

Other vars, values taken at the time of the error

self.lambda_: 1.0
self.ENCODING_DIM: 10
self.b: torch.Size([10, 1]), requires_grad=True
x: torch.Size([3, 1, 10]), grad_fn=&lt;MulBackward0&gt;

How could I set the result of muy as just an ingredient of the leaf node, so grad through V is required?
I tried this monstrosity, to no avail
def muy(self, x):
    V_inv = np.linalg.inv(self.V.detach().numpy())
    x_numpy = x[:, None].detach().numpy()
    temp= -0.5 * np.matmul(V_inv, self.b.detach().numpy() + self.lambda_ * x_numpy)
    return temp

Why I cared about this JIT:
I wanted to use tensorboard to visualize my model, if I understand the error messages right, the visualzing models use Tracer
EDIT
This still gives the same error, W or W.detach()
with torch.no_grad():
    V = self.W - self.lambda_ * torch.eye(self.ENCODING_DIM)
    return -0.5 * V.inverse().mm(self.b + self.lambda_ * x[:, None])


V = torch.tensor(self.W - self.lambda_ * torch.eye(self.ENCODING_DIM), requires_grad=False)

What you are trying to do here doesn't make much sense. torch.tensor(value) can only be created if the value is scalar (e.g. Python's 5), while you are trying to fit torch.Tensor there.
What you should do is simply this:
V = self.W - self.lambda_ * torch.eye(self.ENCODING_DIM)

If you want to detach self.W for some reason you can do this:
V = self.W.detach() - self.lambda_ * torch.eye(self.ENCODING_DIM)

(this will make a copy of self.W with requires_grad set to False).
You could also use torch.no_grad() context manager so this operation will not be recorded on the graph which will have the same effect on the graph (but only in this case, not in general and you won't make copy of self.W so it is advised to do that):
with torch.no_grad():
    V = self.W - self.lambda_ * torch.eye(self.ENCODING_DIM)

Code to reproduce
Can't reproduce this exact issue based on your code description, see below:
import torch

lambda_ = 1.0
W = torch.randn(10, 10, requires_grad=True)

ENCODING_DIM = 10
b = torch.randn(10, 1, requires_grad=True)
x = torch.randn(3, 1, 10, requires_grad=True)


with torch.no_grad():
    V = W - lambda_ * torch.eye(ENCODING_DIM)
    result = -0.5 * V.inverse().mm(b + lambda_ * x[:, None])

print(result)

This code gives the following (different!) error:

Traceback (most recent call last):   File &quot;foo.py&quot;, line 13, in

result = -0.5 * V.inverse().mm(b + lambda_ * x[:, None]) RuntimeError: matrices expected, got 2D, 4D tensors at
/pytorch/aten/src/TH/generic/THTensorMath.cpp:36

