Having difficulties printing the W in linear regression

I’m an beginner in deep learning

what I’m trying to do is print the epochs, W, b and cost on each steps.

I could print the epochs, b and cost but when I tried to print W, the value error occurs.

I have no idea why it happens.

this is the error what I got.

please someone explain me why this error occurs.
It works perfect without the W part.


ValueError Traceback (most recent call last)
in
25 optimizer.step()
26
—> 27 print(“epoch : {:4d}/{}, cost : {:.4f}, b : {:.3f}, W : {:.4f}” .format(epoch, nb_epoch, cost.item(), b.item(), W.item()))

ValueError: only one element tensors can be converted to Python scalars

what I assumed why the error occur is

I defined the W matrix like this

W = torch.zeros((3, 1), requires_grad = True)

so the python couldn’t understand it.

how can I print the W when its type is like that

You could directly print it without the item() call.
item() will return a Python variable. Since W is a multi-dimensional tensor, it cannot be converted to a Python value.