[英]how to convert series numpy array into tensors using pytorch
我試圖將圖像標簽轉換為張量,但我得到一些錯誤請幫我轉換為張量:這里我的代碼:
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)
這是我的錯誤:
---------------------------------------------------------------------------
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)
這里是我的數組值:
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
這是我要做的:
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.