開始使用免費開始

使用 TensorDataset

將資料整理成一個資料集,是訓練 PyTorch 神經網路的第一步之一。TensorDataset 可將 NumPy 陣列轉成 PyTorch 可使用的格式,讓流程更簡化。

在這個練習中,你會用已預先載入的 animals 資料集建立一個 TensorDataset,並檢視它的結構。

本練習屬於課程

使用 PyTorch 的深度學習入門

檢視課程

練習說明

  • Xy 轉成張量並建立一個 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)
編輯並執行程式碼