23

How to convert a numpy array of dtype=object to torch Tensor?

array([
   array([0.5, 1.0, 2.0], dtype=float16),
   array([4.0, 6.0, 8.0], dtype=float16)
], dtype=object)
2
  • Can you show us a small example how you try to convert the numpy array to the torch tensor? So that we can run it by ourselves? Commented Apr 17, 2019 at 9:55
  • 1
    I'm not surprised that pytorch has problems creating a tensor from an object dtype array. That's an array of arrays - arrays which are stored elsewhere in memory. But it may work with data.tolist(), a list of arrays. Or join them into a 2d array with np.stack(data). This will only work where the component arrays have the same shape (as appears to be the case here). Commented Apr 17, 2019 at 15:28

3 Answers 3

25

It is difficult to answer properly since you do not show us how you try to do it. From your error message I can see that you try to convert a numpy array containing objects to a torch tensor. This does not work, you will need a numeric data type:

import torch
import numpy as np

# Your test array without 'dtype=object'
a = np.array([
    np.array([0.5, 1.0, 2.0], dtype=np.float16),
    np.array([4.0, 6.0, 8.0], dtype=np.float16),
])

b = torch.from_numpy(a)

print(a.dtype) # This should not be 'object'
print(b)

Output

float16
tensor([[0.5000, 1.0000, 2.0000],
        [4.0000, 6.0000, 8.0000]], dtype=torch.float16)
Sign up to request clarification or add additional context in comments.

2 Comments

Your a is a tuple. It shouldn't have a dtype.
No, it is not a tuple. I added some indentation so one can see it more clearly.
10

Just adding to what was written above:

First you should make sure your array dtype isn't a 'O' (Object).

You do that by: (credit)

a=np.vstack(a).astype(np.float)

Then you can use:

b = torch.from_numpy(a)

Comments

0

use the .astype() to transform the type of your data. i use this successfully. you can try

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.