Q:

AttributeError: 'torch.FloatTensor' object has no attribute 'item'

Here are the codes：
    from __future__ import print_function
    from itertools import count

    import torch 
    import torch.autograd
    import torch.nn.functional as F

    POLY_DEGREE = 4
    W_target = torch.randn(POLY_DEGREE, 1) * 5
    b_target = torch.randn(1) * 5

    def make_features(x):
        x = x.unsqueeze(1)
        return torch.cat([x ** i for i in range(1, POLY_DEGREE+1)], 1)

    def f(x):
        return x.mm(W_target) + b_target.item()

This resulted in the following error message:
AttributeError: 'torch.FloatTensor' object has no attribute 'item'

How can I solve this please?

A:

The function item() is new from PyTorch 0.4.0. When using earlier versions of PyTorch you will get this error. 
So you can upgrade your PyTorch version your to solve this. 
Edit:
I got through your example again. What do you want archive with item()?
In your case item() should just give you the (python) float value in the tensor.
Why do you want to use this? You can just leave out item().
So:
def f(x):
    return x.mm(W_target) + b_target

instead of:
def f(x):
    return x.mm(W_target) + b_target.item()

This should work for you, in PyTorch 0.4.0 there is not difference. It is also more efficient to leave out item().

