线性模型是机器学习中最基本的模型之一,通过对输入特征进行线性组合来预测输出。本实验旨在展示使用随机梯度下降优化器训练线性模型的过程,并评估模型在鸢尾花数据集上的性能。
本系列实验使用了PyTorch深度学习框架,相关操作如下:
conda create -n DL python=3.7
conda activate DL
pip install torch==1.8.1+cu102 torchvision==0.9.1+cu102 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
conda install matplotlib
conda install scikit-learn
软件包 | 本实验版本 | 目前最新版 |
---|---|---|
matplotlib | 3.5.3 | 3.8.0 |
numpy | 1.21.6 | 1.26.0 |
python | 3.7.16 | |
scikit-learn | 0.22.1 | 1.3.0 |
torch | 1.8.1+cu102 | 2.0.1 |
torchaudio | 0.8.1 | 2.0.2 |
torchvision | 0.9.1+cu102 | 0.15.2 |
import torch
import torch.optim as optim
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn import metrics
optim
)metrics
)linear_model
该函数接受输入数据x
,使用随机生成的权重w
和偏置b
,计算输出值output
。这里的线性模型的形式为 output = x * w + b
。
def linear_model(x):
return torch.matmul(x, w) + b
loss_function
这里使用的是均方误差(MSE)作为损失函数,计算预测值与真实值之间的差的平方。
def loss_function(y_true, y_pred):
loss = (y_pred - y_true) ** 2
return loss
iris = load_iris()
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
x_train = torch.tensor(x_train, dtype=torch.float32)
y_train = torch.tensor(y_train, dtype=torch.float32).view(-1, 1)
x_test = torch.tensor(x_test, dtype=torch.float32)
y_test = torch.tensor(y_test, dtype=torch.float32).view(-1, 1)
w = torch.rand(1, 1, requires_grad=True)
b = torch.randn(1, requires_grad=True)
使用随机梯度下降(SGD)优化器进行模型训练,指定学习率和待优化的参数w, b。
optimizer = optim.SGD([w, b], lr=0.01) # 使用SGD优化器
num_epochs = 100
for epoch in range(num_epochs):
optimizer.zero_grad() # 梯度清零
prediction = linear_model(x_train, w, b)
loss = loss_function(y_train, prediction)
loss.mean().backward() # 计算梯度
optimizer.step() # 更新参数
if (epoch + 1) % 10 == 0:
print(f"Epoch {epoch+1}/{num_epochs}, Loss: {loss.mean().item()}")
x
进行预测,得到预测结果 prediction
。
loss_function
计算预测结果与真实标签之间的损失,得到损失张量 loss
。
loss.mean().backward()
计算损失的平均值,并根据计算得到的梯度进行反向传播。
optimizer.step()
更新权重和偏置,使用优化器进行梯度下降更新。
在测试集上进行预测,使用训练好的模型对测试集进行预测
with torch.no_grad():
test_prediction = linear_model(x_test, w, b)
test_prediction = torch.round(test_prediction) # 四舍五入为整数
test_prediction = test_prediction.detach().numpy()
metrics
模块计算分类准确度(accuracy)、精确度(precision)、召回率(recall)和F1得分(F1 score)。w
和 b
,以及在测试集上的评估指标。accuracy = metrics.accuracy_score(y_test, test_prediction)
precision = metrics.precision_score(y_test, test_prediction, average='macro')
recall = metrics.recall_score(y_test, test_prediction, average='macro')
f1 = metrics.f1_score(y_test, test_prediction, average='macro')
print("The optimized parameters are:")
print("w:", w.flatten().tolist())
print("b:", b.item())
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1 Score:", f1)
本实验使用随机梯度下降优化器训练线性模型,并在鸢尾花数据集上取得了较好的分类性能。实验结果表明,经过优化后的模型能够对鸢尾花进行准确的分类,并具有较高的精确度、召回率和F1得分。
import torch
import torch.optim as optim
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn import metrics
def linear_model(x, w, b):
return torch.matmul(x, w) + b
def loss_function(y_true, y_pred):
loss = (y_pred - y_true) ** 2
return loss
# 加载鸢尾花数据集并进行预处理
iris = load_iris()
x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
x_train = torch.tensor(x_train, dtype=torch.float32)
y_train = torch.tensor(y_train, dtype=torch.float32).view(-1, 1)
x_test = torch.tensor(x_test, dtype=torch.float32)
y_test = torch.tensor(y_test, dtype=torch.float32).view(-1, 1)
w = torch.rand(x_train.shape[1], 1, requires_grad=True)
b = torch.randn(1, requires_grad=True)
optimizer = optim.SGD([w, b], lr=0.01) # 使用SGD优化器
num_epochs = 100
for epoch in range(num_epochs):
optimizer.zero_grad() # 梯度清零
prediction = linear_model(x_train, w, b)
loss = loss_function(y_train, prediction)
loss.mean().backward() # 计算梯度
optimizer.step() # 更新参数
if (epoch + 1) % 10 == 0:
print(f"Epoch {epoch+1}/{num_epochs}, Loss: {loss.mean().item()}")
# 在测试集上进行预测
with torch.no_grad():
test_prediction = linear_model(x_test, w, b)
test_prediction = torch.round(test_prediction) # 四舍五入为整数
test_prediction = test_prediction.detach().numpy()
accuracy = metrics.accuracy_score(y_test, test_prediction)
precision = metrics.precision_score(y_test, test_prediction, average='macro')
recall = metrics.recall_score(y_test, test_prediction, average='macro')
f1 = metrics.f1_score(y_test, test_prediction, average='macro')
print("The optimized parameters are:")
print("w:", w.flatten().tolist())
print("b:", b.item())
print("Accuracy:", accuracy)
print("Precision:", precision)
print("Recall:", recall)
print("F1 Score:", f1)
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有