【python pytorch】Pytorch实现逻辑回归

2024-09-07 06:18

本文主要是介绍【python pytorch】Pytorch实现逻辑回归,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

pytorch 逻辑回归学习demo:

import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable# Hyper Parameters 
input_size = 784
num_classes = 10
num_epochs = 10
batch_size = 50
learning_rate = 0.001# MNIST Dataset (Images and Labels)
train_dataset = dsets.MNIST(root='./data', train=True, transform=transforms.ToTensor(),download=True)print(train_dataset)test_dataset = dsets.MNIST(root='./data', train=False, transform=transforms.ToTensor())# Dataset Loader (Input Pipline)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)# Model
class LogisticRegression(nn.Module):def __init__(self, input_size, num_classes):super(LogisticRegression, self).__init__()self.linear = nn.Linear(input_size, num_classes)def forward(self, x):out = self.linear(x)return outmodel = LogisticRegression(input_size, num_classes)# Loss and Optimizer
# Softmax is internally computed.
# Set parameters to be updated.
criterion = nn.CrossEntropyLoss()  
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)  # Training the Model
for epoch in range(num_epochs):for i, (images, labels) in enumerate(train_loader):images = Variable(images.view(-1, 28*28))labels = Variable(labels)# Forward + Backward + Optimizeoptimizer.zero_grad()outputs = model(images)loss = criterion(outputs, labels)loss.backward()optimizer.step()if (i+1) % 100 == 0:print ('Epoch: [%d/%d], Step: [%d/%d], Loss: %.4f' % (epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))# Test the Model
correct = 0
total = 0
for images, labels in test_loader:images = Variable(images.view(-1, 28*28))outputs = model(images)_, predicted = torch.max(outputs.data, 1)total += labels.size(0)correct += (predicted == labels).sum()print('Accuracy of the model on the 10000 test images: %d %%' % (100 * correct / total))# Save the Model
torch.save(model.state_dict(), 'model.pkl')

运行结果:

Epoch: [1/10], Step: [100/1200], Loss: 2.2397
Epoch: [1/10], Step: [200/1200], Loss: 2.1378
Epoch: [1/10], Step: [300/1200], Loss: 2.0500
Epoch: [1/10], Step: [400/1200], Loss: 1.9401
Epoch: [1/10], Step: [500/1200], Loss: 1.9175
Epoch: [1/10], Step: [600/1200], Loss: 1.8203
Epoch: [1/10], Step: [700/1200], Loss: 1.7322
Epoch: [1/10], Step: [800/1200], Loss: 1.6910
Epoch: [1/10], Step: [900/1200], Loss: 1.6678
Epoch: [1/10], Step: [1000/1200], Loss: 1.5577
Epoch: [1/10], Step: [1100/1200], Loss: 1.5113
Epoch: [1/10], Step: [1200/1200], Loss: 1.5671
Epoch: [2/10], Step: [100/1200], Loss: 1.4560
Epoch: [2/10], Step: [200/1200], Loss: 1.3170
Epoch: [2/10], Step: [300/1200], Loss: 1.3822
Epoch: [2/10], Step: [400/1200], Loss: 1.2793
Epoch: [2/10], Step: [500/1200], Loss: 1.4281
Epoch: [2/10], Step: [600/1200], Loss: 1.2763
Epoch: [2/10], Step: [700/1200], Loss: 1.1570
Epoch: [2/10], Step: [800/1200], Loss: 1.1050
Epoch: [2/10], Step: [900/1200], Loss: 1.1151
Epoch: [2/10], Step: [1000/1200], Loss: 1.0385
Epoch: [2/10], Step: [1100/1200], Loss: 1.0978
Epoch: [2/10], Step: [1200/1200], Loss: 1.0007
Epoch: [3/10], Step: [100/1200], Loss: 1.1849
Epoch: [3/10], Step: [200/1200], Loss: 1.0002
Epoch: [3/10], Step: [300/1200], Loss: 1.0198
Epoch: [3/10], Step: [400/1200], Loss: 0.9248
Epoch: [3/10], Step: [500/1200], Loss: 0.8974
Epoch: [3/10], Step: [600/1200], Loss: 1.1095
Epoch: [3/10], Step: [700/1200], Loss: 1.0900
Epoch: [3/10], Step: [800/1200], Loss: 1.0178
Epoch: [3/10], Step: [900/1200], Loss: 0.9809
Epoch: [3/10], Step: [1000/1200], Loss: 0.9831
Epoch: [3/10], Step: [1100/1200], Loss: 0.8701
Epoch: [3/10], Step: [1200/1200], Loss: 0.9855
Epoch: [4/10], Step: [100/1200], Loss: 0.9081
Epoch: [4/10], Step: [200/1200], Loss: 0.8791
Epoch: [4/10], Step: [300/1200], Loss: 0.7540
Epoch: [4/10], Step: [400/1200], Loss: 0.9443
Epoch: [4/10], Step: [500/1200], Loss: 0.9346
Epoch: [4/10], Step: [600/1200], Loss: 0.8974
Epoch: [4/10], Step: [700/1200], Loss: 0.8897
Epoch: [4/10], Step: [800/1200], Loss: 0.7797
Epoch: [4/10], Step: [900/1200], Loss: 0.8608
Epoch: [4/10], Step: [1000/1200], Loss: 0.9216
Epoch: [4/10], Step: [1100/1200], Loss: 0.8676
Epoch: [4/10], Step: [1200/1200], Loss: 0.9251
Epoch: [5/10], Step: [100/1200], Loss: 0.7640
Epoch: [5/10], Step: [200/1200], Loss: 0.6955
Epoch: [5/10], Step: [300/1200], Loss: 0.8431
Epoch: [5/10], Step: [400/1200], Loss: 0.8489
Epoch: [5/10], Step: [500/1200], Loss: 0.7191
Epoch: [5/10], Step: [600/1200], Loss: 0.6671
Epoch: [5/10], Step: [700/1200], Loss: 0.6980
Epoch: [5/10], Step: [800/1200], Loss: 0.6837
Epoch: [5/10], Step: [900/1200], Loss: 0.9087
Epoch: [5/10], Step: [1000/1200], Loss: 0.7784
Epoch: [5/10], Step: [1100/1200], Loss: 0.7890
Epoch: [5/10], Step: [1200/1200], Loss: 1.0480
Epoch: [6/10], Step: [100/1200], Loss: 0.5834
Epoch: [6/10], Step: [200/1200], Loss: 0.8300
Epoch: [6/10], Step: [300/1200], Loss: 0.8316
Epoch: [6/10], Step: [400/1200], Loss: 0.7249
Epoch: [6/10], Step: [500/1200], Loss: 0.6184
Epoch: [6/10], Step: [600/1200], Loss: 0.7505
Epoch: [6/10], Step: [700/1200], Loss: 0.6599
Epoch: [6/10], Step: [800/1200], Loss: 0.7170
Epoch: [6/10], Step: [900/1200], Loss: 0.6857
Epoch: [6/10], Step: [1000/1200], Loss: 0.6543
Epoch: [6/10], Step: [1100/1200], Loss: 0.5679
Epoch: [6/10], Step: [1200/1200], Loss: 0.8261
Epoch: [7/10], Step: [100/1200], Loss: 0.7144
Epoch: [7/10], Step: [200/1200], Loss: 0.7573
Epoch: [7/10], Step: [300/1200], Loss: 0.7254
Epoch: [7/10], Step: [400/1200], Loss: 0.5918
Epoch: [7/10], Step: [500/1200], Loss: 0.6959
Epoch: [7/10], Step: [600/1200], Loss: 0.7058
Epoch: [7/10], Step: [700/1200], Loss: 0.7382
Epoch: [7/10], Step: [800/1200], Loss: 0.7282
Epoch: [7/10], Step: [900/1200], Loss: 0.6750
Epoch: [7/10], Step: [1000/1200], Loss: 0.6019
Epoch: [7/10], Step: [1100/1200], Loss: 0.6615
Epoch: [7/10], Step: [1200/1200], Loss: 0.5851
Epoch: [8/10], Step: [100/1200], Loss: 0.6492
Epoch: [8/10], Step: [200/1200], Loss: 0.5439
Epoch: [8/10], Step: [300/1200], Loss: 0.6613
Epoch: [8/10], Step: [400/1200], Loss: 0.6486
Epoch: [8/10], Step: [500/1200], Loss: 0.8281
Epoch: [8/10], Step: [600/1200], Loss: 0.6263
Epoch: [8/10], Step: [700/1200], Loss: 0.6541
Epoch: [8/10], Step: [800/1200], Loss: 0.5080
Epoch: [8/10], Step: [900/1200], Loss: 0.7020
Epoch: [8/10], Step: [1000/1200], Loss: 0.6421
Epoch: [8/10], Step: [1100/1200], Loss: 0.6207
Epoch: [8/10], Step: [1200/1200], Loss: 0.9254
Epoch: [9/10], Step: [100/1200], Loss: 0.7428
Epoch: [9/10], Step: [200/1200], Loss: 0.6815
Epoch: [9/10], Step: [300/1200], Loss: 0.6418
Epoch: [9/10], Step: [400/1200], Loss: 0.7096
Epoch: [9/10], Step: [500/1200], Loss: 0.6846
Epoch: [9/10], Step: [600/1200], Loss: 0.5124
Epoch: [9/10], Step: [700/1200], Loss: 0.6300
Epoch: [9/10], Step: [800/1200], Loss: 0.6340
Epoch: [9/10], Step: [900/1200], Loss: 0.5593
Epoch: [9/10], Step: [1000/1200], Loss: 0.5706
Epoch: [9/10], Step: [1100/1200], Loss: 0.6258
Epoch: [9/10], Step: [1200/1200], Loss: 0.7627
Epoch: [10/10], Step: [100/1200], Loss: 0.5254
Epoch: [10/10], Step: [200/1200], Loss: 0.5318
Epoch: [10/10], Step: [300/1200], Loss: 0.5448
Epoch: [10/10], Step: [400/1200], Loss: 0.5634
Epoch: [10/10], Step: [500/1200], Loss: 0.6398
Epoch: [10/10], Step: [600/1200], Loss: 0.7158
Epoch: [10/10], Step: [700/1200], Loss: 0.6169
Epoch: [10/10], Step: [800/1200], Loss: 0.5641
Epoch: [10/10], Step: [900/1200], Loss: 0.5698
Epoch: [10/10], Step: [1000/1200], Loss: 0.5612
Epoch: [10/10], Step: [1100/1200], Loss: 0.5126
Epoch: [10/10], Step: [1200/1200], Loss: 0.6746
Accuracy of the model on the 10000 test images: 87 %Process finished with exit code 0

这篇关于【python pytorch】Pytorch实现逻辑回归的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1144288

相关文章

python管理工具之conda安装部署及使用详解

《python管理工具之conda安装部署及使用详解》这篇文章详细介绍了如何安装和使用conda来管理Python环境,它涵盖了从安装部署、镜像源配置到具体的conda使用方法,包括创建、激活、安装包... 目录pytpshheraerUhon管理工具:conda部署+使用一、安装部署1、 下载2、 安装3

Python进阶之Excel基本操作介绍

《Python进阶之Excel基本操作介绍》在现实中,很多工作都需要与数据打交道,Excel作为常用的数据处理工具,一直备受人们的青睐,本文主要为大家介绍了一些Python中Excel的基本操作,希望... 目录概述写入使用 xlwt使用 XlsxWriter读取修改概述在现实中,很多工作都需要与数据打交

使用Python实现在Word中添加或删除超链接

《使用Python实现在Word中添加或删除超链接》在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能,本文将为大家介绍一下Python如何实现在Word中添加或... 在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能。通过添加超

windos server2022里的DFS配置的实现

《windosserver2022里的DFS配置的实现》DFS是WindowsServer操作系统提供的一种功能,用于在多台服务器上集中管理共享文件夹和文件的分布式存储解决方案,本文就来介绍一下wi... 目录什么是DFS?优势:应用场景:DFS配置步骤什么是DFS?DFS指的是分布式文件系统(Distr

NFS实现多服务器文件的共享的方法步骤

《NFS实现多服务器文件的共享的方法步骤》NFS允许网络中的计算机之间共享资源,客户端可以透明地读写远端NFS服务器上的文件,本文就来介绍一下NFS实现多服务器文件的共享的方法步骤,感兴趣的可以了解一... 目录一、简介二、部署1、准备1、服务端和客户端:安装nfs-utils2、服务端:创建共享目录3、服

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

利用Python编写一个简单的聊天机器人

《利用Python编写一个简单的聊天机器人》这篇文章主要为大家详细介绍了如何利用Python编写一个简单的聊天机器人,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 使用 python 编写一个简单的聊天机器人可以从最基础的逻辑开始,然后逐步加入更复杂的功能。这里我们将先实现一个简单的

基于Python开发电脑定时关机工具

《基于Python开发电脑定时关机工具》这篇文章主要为大家详细介绍了如何基于Python开发一个电脑定时关机工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 简介2. 运行效果3. 相关源码1. 简介这个程序就像一个“忠实的管家”,帮你按时关掉电脑,而且全程不需要你多做

C#使用yield关键字实现提升迭代性能与效率

《C#使用yield关键字实现提升迭代性能与效率》yield关键字在C#中简化了数据迭代的方式,实现了按需生成数据,自动维护迭代状态,本文主要来聊聊如何使用yield关键字实现提升迭代性能与效率,感兴... 目录前言传统迭代和yield迭代方式对比yield延迟加载按需获取数据yield break显式示迭

Python实现高效地读写大型文件

《Python实现高效地读写大型文件》Python如何读写的是大型文件,有没有什么方法来提高效率呢,这篇文章就来和大家聊聊如何在Python中高效地读写大型文件,需要的可以了解下... 目录一、逐行读取大型文件二、分块读取大型文件三、使用 mmap 模块进行内存映射文件操作(适用于大文件)四、使用 pand