当前位置:网站首页>pytorch-06.逻辑斯蒂回归
pytorch-06.逻辑斯蒂回归
2022-08-10 05:32:00 【生信研究猿】
#cross-entropy
#BCELoss 二元交叉熵
import torch
import numpy as np
import matplotlib.pyplot as plt
x_data = torch.Tensor([[1.0],[2.0],[3.0]])
y_data = torch.Tensor([[0],[0],[1]])
class LogisticRegressionModel(torch.nn.Module):
def __init__(self):
super(LogisticRegressionModel,self).__init__()
self.linear = torch.nn.Linear(1,1)
def forward(self,x):
y_pred = torch.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)
for epoch in range (1000):
y_pred = model(x_data)
loss = criterion(y_pred,y_data)
print('epoch=', epoch, " loss=", loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Output weight and bias
print('w=',model.linear.weight.item())
print('b=',model.linear.bias.item())
#--------------------------------------------------------
# Test Model
x_test = torch.Tensor([[4.0]])
y_test = model(x_test)
print('y_pred=',y_test.data.item())
x = np.linspace(0,10,200) #0到10采了200个点
x_t = torch.Tensor(x).view((200,1)) #变成200行1列的矩阵
y_t = model(x_t)
y = y_t.data.numpy()
plt.plot(x,y)
plt.plot([0,10],[0.5,0.5],c='r') #画了一条红线
plt.xlabel('Hours')
plt.ylabel('Probability of Pass')
plt.grid()
plt.show()
边栏推荐
- Chain Reading|The latest and most complete digital collection sales calendar-07.29
- Index Notes【】【】
- Collection工具类
- Four characteristics of ACID
- 小程序wx.request简单Promise封装
- Bifrost 同步数据库实现微服务跨库数据同步
- 常用类 BigDecimal
- The submenu of the el-cascader cascade selector is double-clicked to display the selected content
- The Principle of Union Search and API Design
- Consensus calculation and incentive mechanism
猜你喜欢
分享一款恋爱星座男女配对微信小程序源码
Chain Reading Recommendation: From Tiles to Generative NFTs
.Net Core导入千万级数据至Mysql
Chained Picks: Starbucks looks at digital collectibles and better engages customers
操作表 函数的使用
链表API设计
Operation table Function usage
微信小程序--模板与设置WXML
LeetCode 剑指offer 21.调整数组顺序使奇数位于偶数前面(简单)
链读 | 最新最全的数字藏品发售日历-07.28
随机推荐
树结构——2-3树图解
力扣——情侣牵手
微信小程序-小程序的宿主环境
网络安全之防火墙
el-cascader级联选择器的子菜单双击两次才显示被选中的内容
IO流【】【】【】
Models corresponding to each architecture instruction set
十年磨一剑!数字藏品行情软件,链读APP正式开放内测!
链读好文:热加密存储与冷加密存储有什么区别?
A little knowledge point every day
view【】【】【】【】
各个架构指令集对应的机型
2021-06-22
Collection Map
Collection set interface
优先队列
程序员副业赚钱之道,实现月收入增加20K
String常用方法
微信小程序wx.writeBLECharacteristicValue汉字转buffer问题
视图【】【】【】【】