2

I have a list of tensors like the following one:

[tensor(0.9757), tensor(0.9987), tensor(0.9990), tensor(0.9994), tensor(0.9994)]

How can I change the type of the entire list and obtain something like:

[0.9757, 0.9987, 0.9990, 0.9994, 0.9994]

Thanks for the help!

1
  • Hi and welcome to SO. It is important for the community that you also demonstrate that you are working to solve your issue. The best way to do that in my opinion is to include the text based version of the source code you have so far, even if it is not working quite right. If you want a nudge getting started check out map(float, <your list>)
    – JonSG
    Commented Mar 3, 2022 at 18:40

1 Answer 1

3

You can use .item() and a list comprehension, assuming that every element is a one-element tensor:

result = [tensor.item() for tensor in data]
print(type(result[0]))
print(result)

This prints the desired result, albeit with some unavoidable precision error:

<class 'float'>
[0.9757000207901001, 0.9987000226974487, 0.9990000128746033, 0.9994000196456909, 0.9994000196456909]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.