使用 TensorDataset
將資料整理成一個資料集,是訓練 PyTorch 神經網路的第一步之一。TensorDataset 可將 NumPy 陣列轉成 PyTorch 可使用的格式,讓流程更簡化。
在這個練習中,你會用已預先載入的 animals 資料集建立一個 TensorDataset,並檢視它的結構。
本練習屬於課程
使用 PyTorch 的深度學習入門
練習說明
- 將
X和y轉成張量並建立一個TensorDataset。 - 取出並印出第一個樣本。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
import torch
from torch.utils.data import TensorDataset
X = animals.iloc[:, 1:-1].to_numpy()
y = animals.iloc[:, -1].to_numpy()
# Create a dataset
dataset = ____(____, ____)
# Print the first sample
input_sample, label_sample = ____
print('Input sample:', input_sample)
print('Label sample:', label_sample)