0

I am trying to convert image labels convert into tensor, but I got some error please help me to convert to tensor: Here My code:

features_train, features_test, targets_train, targets_test = train_test_split(X,Y,test_size=0.2,
                                                                              random_state=42)
X_train = torch.from_numpy(features_train)
X_test = torch.from_numpy(features_test)

Y_train =torch.from_numpy(targets_train).type(torch.IntTensor) 
Y_test = torch.from_numpy(targets_test).type(torch.IntTensor)
train = torch.utils.data.TensorDataset(X_train,Y_train)
test = torch.utils.data.TensorDataset(X_test,Y_test)


train_loader = torch.utils.data.DataLoader(train, batch_size = train_batch_size, shuffle = False)
test_loader = torch.utils.data.DataLoader(test, batch_size = test_batch_size, shuffle = False)

Here my error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-f1578581ff5c> in <module>()
      5 X_test = torch.from_numpy(features_test)
      6 
----> 7 Y_train =torch.from_numpy(targets_train).type(torch.IntTensor)
      8 Y_test = torch.from_numpy(targets_test).type(torch.IntTensor)
      9 train = torch.utils.data.TensorDataset(X_train,Y_train)

TypeError: expected np.ndarray (got Series)

Here my array values:

targets_train
478     1
5099    3
1203    2
5674    2
142     1
4836    2
4031    1
1553    3
4416    1
605     5
1194    3
4319    4
1498    5
1

1 Answer 1

1

Here is what I would do:

import torch
import numpy as np
n = np.arange(10)
print(n) #[0 1 2 3 4 5 6 7 8 9]
t1 = torch.Tensor(n)  # as torch.float32
print(t1) #tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
t2 = torch.from_numpy(n)  # as torch.int32
print(t2) #tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=torch.int32)
Sign up to request clarification or add additional context in comments.

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.