harsh
harsh

Reputation: 575

How to convert from tensor to float

I am having a list of tensors I want to convert to floating points how can I do it. I have tried using .item but it is not working. I am getting ValueError: only one element tensors can be converted to Python scalars.

tensor([[12.1834,  4.9616,  7.7913],
        [ 8.9394,  8.5784,  9.3691],
        [ 9.4475,  8.9766,  9.8418],
        [11.8210,  6.0852,  8.2168],
        [ 8.2885,  6.2607,  9.8877]], grad_fn=<CloneBackward0>)

enter image description here

Upvotes: 7

Views: 52132

Answers (2)

Hossein Tahmasbi
Hossein Tahmasbi

Reputation: 55

This works:

results[1].detach().item()

Upvotes: 4

Borin --help
Borin --help

Reputation: 186

You just need to cast Tensor constant to numpy object,then can access by index.

result.numpy()[0]

Upvotes: 11

Related Questions