[英]how to convert series numpy array into tensors using pytorch
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
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.