博客
关于我
pytorch入门第五课——逻辑斯蒂回归
阅读量:687 次
发布时间:2019-03-17

本文共 1290 字,大约阅读时间需要 4 分钟。

前言

本文记录了学习刘洪普老师的PyTorch入门课中逻辑斯蒂回归的内容。通过实践与理论学习,深入理解逻辑斯蒂回归的原理及其在分类任务中的应用。

以下是本文的目录结构:

目录

  • 1. 方法
  • 2. 关于激活函数
  • 3. Jupyter Notebook示例

一、方法

逻辑斯蒂回归并非传统意义上的回归算法,而是一种分类模型。它通过对分类概率进行建模,避免了对数据分布的假设,能够更准确地反映分类结果。

逻辑斯蒂函数的数学表达式为:

$$P(y=1|x) = \sigma(b + wx)$$

其中,$\sigma$ 是逻辑斯蒂激活函数,定义为:

$$\sigma(x) = \frac{1}{1 + e^{-x}}$$

逻辑斯蒂函数的主要特点包括:范围限制于(0,1),单调递增,并且具有饱和特性(随着输入值的远离零点,激活值趋近于1或0)。

二、Jupyter Notebook示例

以下是构建逻辑斯蒂回归模型的代码示例:

import torch
import torch.nn.functional as F
class LogisticRegressionModel(torch.nn.Module):
def __init__(self):
super(LogisticRegressionModel, self).__init__()
self.linear = torch.nn.Linear(1, 1)
\n def forward(self, x):
y_pred = F.sigmoid(self.linear(x))
return y_pred
model = LogisticRegressionModel()
criterion = torch.nn.BCELoss(size_average=False)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
\n
for epoch in range(1000):
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(f'Epoch {epoch}, Loss: {loss.item()}'."
optimizer.zero_grad()
loss.backward()
optimizer.step()

通过上述代码可以观察到以下训练效果:

Training Loss Trend:

0: 2.8701

1: 2.8529
2: 2.8366
3: 2.8209
4: 2.8061
5: 2.7920
6: 2.7786
7: 2.7658
8: 2.7536
9: 2.7419

随着训练次数的增加,损失值逐渐下降,表明模型在学习样本数据集。

三、总结

逻辑斯蒂回归是一种简单而有效的分类模型,通过对数据进行概率建模,避免了对分布假设的依赖。如果你有分类任务需要处理,不妨考虑使用逻辑斯蒂回归来解决问题。记住,在实践中,保持数据的平衡性和多样性是提升模型性能的关键。

转载地址:http://hmzhz.baihongyu.com/

你可能感兴趣的文章
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>