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

相关推荐
计算机编程指导师1 小时前
【计算机毕设】基于Hadoop+Spark的美国互联网银行贷款数据可视化分析系统源码 毕业设计 选题推荐 毕设选题 数据分析 机器学习
大数据·hadoop·python·数据分析·spark·毕业设计·贷款
ZDN_is_beauty1 小时前
深度学习2-卷积神经网络
人工智能·深度学习·cnn
Starry-sky(jing)1 小时前
onnxruntime 安装后 GPU 没加速?3 条命令确认它跑在 CPU 还是 GPU
python·gpu·cuda·onnxruntime·推理加速
步行cgn2 小时前
Spring AOP 实战:安全日志
python·安全·spring
诺伦2 小时前
OpenAI Agents API 公测拆解:托管编排+持久会话+自有沙箱搭一条自动增长流水线 | RiseClaw玄策
python·openai·ai agent·营销自动化·agent编排
坊钰2 小时前
【LangGraph框架入门级】2. 图核心概念:State、Node、Edge 与 Reducers
python·ai编程·langgraph
wewowilling2 小时前
AI大模型开发--01Python基础(无废话)
开发语言·python·函数
小猴子爱上树2 小时前
跨境电商AI图片翻译工具:批量图片翻译、视频字幕翻译与智能抠图一站式解决
大数据·人工智能·python·音视频
5G微创业2 小时前
去除抖音视频水印的Python实现
python·微信小程序·去水印