PytorchZerotoAll学习笔记(五)--逻辑回归
逻辑回归:
本章内容主要讲述简单的逻辑回归:这个可以归纳为二分类的问题。
逻辑,非假即真。两种可能,我们可以联想一下在继电器控制的电信号(0 or 1)
举个栗子:比如说你花了好几个星期复习的考试(通过 or 失败)
哇,那个女孩子长得真好看,你同不同意?
一场NBA,湖人赢了火箭还是输给火箭?
这里:我们引入sigmoid函数,可以设定一个阈值来区分两类。


这样我们可以设定一个阈值:0.5. 超过0.5的值归为1这一类,其余的(>0)都归为零这一类
这里的代码跟上一篇博客的很像,如果你不熟悉的话,希望你可以回头看看。这里我说明一下,不同的点
import torch
from torch.autograd import Variable
import torch.nn.functional as F x_data = Variable(torch.Tensor([[1.0], [2.0], [3.0], [4.0]]))
y_data = Variable(torch.Tensor([[0.], [0.], [1.], [1.]])) class Model(torch.nn.Module): def __init__(self):
"""
In the constructor we instantiate nn.Linear module
"""
super(Model, self).__init__()
self.linear = torch.nn.Linear(1, 1) # One in and one out def forward(self, x):
"""
In the forward function we accept a Variable of input data and we must return
a Variable of output data.
"""
这里是不同的点,使用了function 模块的 sigmoid的函数
y_pred = F.sigmoid(self.linear(x))
return y_pred # our model
model = Model() # Construct our loss function and an Optimizer. The call to model.parameters()
# in the SGD constructor will contain the learnable parameters of the two
# nn.Linear modules which are members of the model.
criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01) # Training loop
for epoch in range(1000):
# Forward pass: Compute predicted y by passing x to the model
y_pred = model(x_data) # Compute and print loss
loss = criterion(y_pred, y_data)
print(epoch, loss.data[0]) # Zero gradients, perform a backward pass, and update the weights.
optimizer.zero_grad()
loss.backward()
optimizer.step() # After training
hour_var = Variable(torch.Tensor([[1.0]]))
print("predict 1 hour ", 1.0, model(hour_var).data[0][0] > 0.5)
hour_var = Variable(torch.Tensor([[7.0]]))
print("predict 7 hours", 7.0, model(hour_var).data[0][0] > 0.5)
PytorchZerotoAll学习笔记(五)--逻辑回归的更多相关文章
- tensorflow学习笔记五----------逻辑回归
在逻辑回归中使用mnist数据集.导入相应的包以及数据集. import numpy as np import tensorflow as tf import matplotlib.pyplot as ...
- Python学习笔记之逻辑回归
# -*- coding: utf-8 -*- """ Created on Wed Apr 22 17:39:19 2015 @author: 90Zeng " ...
- C#可扩展编程之MEF学习笔记(五):MEF高级进阶
好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较常用 ...
- (转)Qt Model/View 学习笔记 (五)——View 类
Qt Model/View 学习笔记 (五) View 类 概念 在model/view架构中,view从model中获得数据项然后显示给用户.数据显示的方式不必与model提供的表示方式相同,可以与 ...
- java之jvm学习笔记五(实践写自己的类装载器)
java之jvm学习笔记五(实践写自己的类装载器) 课程源码:http://download.csdn.net/detail/yfqnihao/4866501 前面第三和第四节我们一直在强调一句话,类 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(五) indigo computer vision
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...
- Typescript 学习笔记五:类
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- ES6学习笔记<五> Module的操作——import、export、as
import export 这两个家伙对应的就是es6自己的 module功能. 我们之前写的Javascript一直都没有模块化的体系,无法将一个庞大的js工程拆分成一个个功能相对独立但相互依赖的小 ...
- 机器学习实战(Machine Learning in Action)学习笔记————05.Logistic回归
机器学习实战(Machine Learning in Action)学习笔记————05.Logistic回归 关键字:Logistic回归.python.源码解析.测试作者:米仓山下时间:2018- ...
- muduo网络库学习笔记(五) 链接器Connector与监听器Acceptor
目录 muduo网络库学习笔记(五) 链接器Connector与监听器Acceptor Connector 系统函数connect 处理非阻塞connect的步骤: Connetor时序图 Accep ...
随机推荐
- 搭建springboot项目
1.搭建环境windows10+jdk1.8+eclipse4.8+maven 2.为了学习微服务架构学习搭建基础项目 3.分为两种搭建方式为maven项目和单独建立springboot项目(ecli ...
- Notes 20180310 : String第二讲_String的声明与创建
1 字符串的声明与创建 学习String的第一步就是创建(声明)字符串,我们在这里之所以分为创建和声明(其实是一个意思,都是创建字符串,但两者却有本质的区别)是因为String是一个很特殊的类,它的 ...
- selenium java maven 自动化测试(一) helloworld
本教程使用selenium-java,简单的完成了网页访问 网页内容获取,表单填写以及按钮点击. 1. 使用maven构建项目 在pom中添加如下依赖: <dependency> < ...
- linux下安装protobuf及cmake编译
一.protobuf 安装 protobuf版本:2.6.1 下载地址:https://github.com/google/protobuf/archive/v2.6.1.zip 解压之后进入目录 修 ...
- Hadoop启动dataNode失败,却没有任何报错
问题描述: centos7,伪分布模式下,启动datanode后,通过JPS查看发现没有相关进程,在日志文件里也没有任何提示.通过百度,网上一堆说什么vesion 的ID不一致,不能解决我的问题. 经 ...
- 【Linux】文件、目录权限及归属
访问权限: 可读(read):允许查看文件内容.显示目录列表 可写(write):允许修改文件内容,允许在目录中新建.移动.删除文件或子目录 可执行(execute):允许运行程序.切换目录 归属: ...
- centos7添加新网卡实现双IP双网关
问题背景: 业务需要,针对业务需要不同地域的机构访问,所以需要在同一台机器上配置不同IP并配置不同网关,实现不用机构可以访问同一台服务器办理业务. 系统环境: centos linux7 网络环境: ...
- centos7 远程连接mongodb时,27017端口连接不上的解决办法
一.问题描述:centos 7 上安装mongogdb,然后通过另外一台电脑用pymongo连接mongodb时,报错:连接拒绝 解决过程: 1.修改mongo.conf文件 命令:sudo vi ...
- 【Mac】安装 tesserocr 遇到的一些坑(‘cinttypes' file not found)
问题描述 tesserocr 是 Python 的一个光学字符识别库,它其实是对 tesseract 做的一层 Python API 封装,所以在安装这个库之前我已经用 Homebrew 成功安装好了 ...
- leetcode-746-Min Cost Climbing Stairs(动态规划)
题目描述: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...