
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 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 from sklearn.preprocessing ``import MinMaxScaler if torch.backends.mps.is_available(): ``device ``= torch.device(``"mps"``) ``print``(``"✅ MPS 可用,使用M5 GPU加速"``) else``: ``device ``= torch.device(``"cpu"``) ``print``(``"⚠️ MPS不可用,使用CPU"``) 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``) scaler ``= MinMaxScaler() X_train ``= scaler.fit_transform(X_train) X_test ``= scaler.transform(X_test) X_train ``= torch.FloatTensor(X_train).to(device) y_train ``= torch.LongTensor(y_train).to(device) X_test ``= torch.FloatTensor(X_test).to(device) y_test ``= torch.LongTensor(y_test).to(device) class SimpleMLP(nn.Module): ``def __init__(``self``): ``super``(SimpleMLP, ``self``).__init__() ``self``.fc1 ``= nn.Linear(``4``, ``16``) ``self``.relu ``= nn.ReLU() ``self``.fc2 ``= nn.Linear(``16``, ``3``) ``def forward(``self``, x): ``out ``= self``.fc1(x) ``out ``= self``.relu(out) ``out ``= self``.fc2(out) ``return out model ``= SimpleMLP().to(device) criterion ``= nn.CrossEntropyLoss() optimizer ``= optim.Adam(model.parameters(), lr``=``0.01``) epochs ``= 5000 loss_list ``= [] for epoch ``in range``(epochs): ``model.train() ``optimizer.zero_grad() ``outputs ``= model(X_train) ``loss ``= criterion(outputs, y_train) ``loss.backward() ``optimizer.step() ``loss_list.append(loss.item()) ``if (epoch``+``1``) ``% 500 =``= 0``: ``print``(f``'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}'``) import matplotlib.pyplot as plt plt.plot(loss_list) plt.plot(``range``(``0``, epochs, ``500``), [loss_list[i] ``for i ``in range``(``0``, epochs, ``500``)], ``'ro'``) plt.title(``'Training Loss'``) plt.xlabel(``'Epochs'``) plt.ylabel(``'Loss'``) plt.show() |