知識點回歸:
- CPU性能的查看:看架構代際、核心數、線程數
- GPU性能的查看:看顯存、看級別、看架構代際
- GPU訓練的方法:數據和模型移動到GPU device上
- 類的call方法:為什么定義前向傳播時可以直接寫作self.fc1(x)
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import numpy as np# 仍然用4特征,3分類的鳶尾花數據集作為我們今天的數據集
# 加載鳶尾花數據集
iris = load_iris()
X = iris.data # 特征數據
y = iris.target # 標簽數據
# 劃分訓練集和測試集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)# # 打印下尺寸
# print(X_train.shape)
# print(y_train.shape)
# print(X_test.shape)
# print(y_test.shape)# 歸一化數據,神經網絡對于輸入數據的尺寸敏感,歸一化是最常見的處理方式
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test) #確保訓練集和測試集是相同的縮放# 將數據轉換為 PyTorch 張量,因為 PyTorch 使用張量進行訓練
# y_train和y_test是整數,所以需要轉化為long類型,如果是float32,會輸出1.0 0.0
X_train = torch.FloatTensor(X_train)
y_train = torch.LongTensor(y_train