当前位置:网站首页>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()
边栏推荐
猜你喜欢

generic notes()()()

The latest and most complete digital collection sales calendar-07.27

【List练习】遍历集合并且按照价格从低到高排序,

最新最全的数字藏品发售日历-07.26

Common class BigDecimal

定时器(setInterval)的开启与关闭

The Principle of Union Search and API Design

Database Notes Create Database, Table Backup

泛型笔记()()()

ORACLE系统表空间SYSTEM占满无法扩充表空间问题解决过程
随机推荐
Batch add watermark to pictures batch scale pictures to specified size
连接 Nacos 报超时错误
ZigBee 网络设备相关内容
The latest and most complete digital collection sales calendar-07.26
LeetCode 剑指offer 10-I.斐波那契数列(简单)
wiki confluence 安装
Timer (setInterval) on and off
常用类 BigDecimal
开源免费WMS仓库管理系统【推荐】
Canal reports Could not find first log file name in binary log index file
ORACLE system table space SYSTEM is full and cannot expand table space problem solving process
Collection工具类
我不喜欢我的代码
【笔记】集合框架体系 Collection
深度学习TensorFlow入门环境配置
索引笔记【】【】
cesium listens to map zoom or zoom to control whether the content added on the map is displayed
Index Notes【】【】
[Notes] Collection Framework System Collection
One step ahead, don't miss it again, the chain reading APP will be launched soon!