实训项目使用 PyTorch 实现鸢尾花数据集
时间: 2025-06-23 13:24:38 浏览: 11
### 使用 PyTorch 实现鸢尾花数据集的实训项目
#### 数据准备
为了使用PyTorch来处理鸢尾花数据集,首先需要加载并预处理数据。可以利用`pandas`库方便地导入CSV文件,并通过`sklearn.preprocessing.StandardScaler`对特征进行标准化[^2]。
```python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
# 加载数据
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
column_names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']
data = pd.read_csv(url, names=column_names)
# 编码标签
label_encoder = LabelEncoder()
data['species'] = label_encoder.fit_transform(data['species'])
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(
data.drop('species', axis=1),
data['species'],
test_size=0.2,
random_state=42
)
# 特征缩放
scaler = StandardScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
```
#### 创建神经网络模型
定义一个多层感知器(MLP),这是一个简单的全连接前馈神经网络结构,在这里采用两层隐藏层的设计方案:
```python
import torch
import torch.nn as nn
import torch.optim as optim
class IrisNet(nn.Module):
def __init__(self):
super(IrisNet, self).__init__()
self.fc1 = nn.Linear(4, 16) # 输入维度为4,输出维度为16
self.fc2 = nn.Linear(16, 8) # 隐藏层之间的转换
self.fc3 = nn.Linear(8, 3) # 输出类别数为3
def forward(self, x):
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
model = IrisNet()
criterion = nn.CrossEntropyLoss() # 定义损失函数
optimizer = optim.Adam(model.parameters(), lr=0.01) # Adam优化算法
```
#### 训练过程
将上述准备工作完成后就可以进入正式的训练环节了。需要注意的是每次迭代都要清空梯度缓存以防止累积错误影响后续更新操作的结果准确性。
```python
num_epochs = 500
for epoch in range(num_epochs):
model.train()
inputs = torch.tensor(X_train_scaled, dtype=torch.float32)
labels = torch.tensor(y_train.values, dtype=torch.long)
optimizer.zero_grad() # 清除之前的梯度信息
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward() # 反向传播计算参数梯度
optimizer.step() # 更新权重参数
if (epoch+1) % 100 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
```
#### 测试评估
完成训练之后还需要验证所建立起来的分类器性能如何,通常可以通过准确率等指标来进行衡量。
```python
with torch.no_grad():
correct = 0
total = 0
for i in range(len(X_test)):
input_tensor = torch.tensor([X_test_scaled[i]], dtype=torch.float32)
output = model(input_tensor)
_, predicted = torch.max(output.data, 1)
if predicted.item() == y_test.iloc[i]:
correct += 1
total += 1
accuracy = correct / total * 100
print(f'Test Accuracy of the model on the test set is: {accuracy:.2f}%')
```
阅读全文
相关推荐











