Python打卡第36天

@浙大疏锦行

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 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() |

相关推荐
wuyk5559 小时前
Python办公自动化-第01讲-环境搭建与自动化认知
python·自动化·pyqt
高洁0110 小时前
数字孪生与计算机视觉:让机器看懂现场
人工智能·python·机器学习·transformer·迁移学习
ComPDFKit10 小时前
RAG 回答只显示 PDF 文件名?用 Python 保留引用页码
开发语言·python·pdf·rag
yz_aiks12 小时前
Termux 使用教程:把安卓手机变成随身 Linux(装 Python、SSH 远程、跑自动化脚本)
android·linux·python·智能手机·ssh·termux
2601_9679801114 小时前
Flask Web 应用
python·flask·web开发·环境变量·虚拟环境
aifannihao14 小时前
ß 大写变 SS:用 Python 算清 Ruby2Shell 的缓冲区账本
python·安全·标准库·缓冲区溢出·字符串编码
liwulin050614 小时前
【OLLAMA】调用SKILL的通用ReAct
python
郝学胜-神的一滴15 小时前
Effective Python 条款36:优雅拼装迭代器与生成器
开发语言·数据结构·人工智能·python·numpy·scikit-learn
Norna16 小时前
Git 给路径加了引号,这个文件就从 review 里消失了
python
wuyk55516 小时前
Python办公自动化-第04讲-批量新建Excel
开发语言·python·excel