首页 > 其他分享 >PyTorch 深度学习实践第六讲(逻辑回归)

PyTorch 深度学习实践第六讲(逻辑回归)

时间:2022-12-22 13:31:24浏览次数:52  
标签:逻辑 torch nn pred self 第六 PyTorch plt data

Logistic Function

PyTorch 深度学习实践第六讲(逻辑回归)_MSE

Sigmoid Functions

PyTorch 深度学习实践第六讲(逻辑回归)_基本框架_02

交叉熵

PyTorch 深度学习实践第六讲(逻辑回归)_基本框架_03

备注:BCE越小分布越接近,越好

深度学习基本框架

PyTorch 深度学习实践第六讲(逻辑回归)_深度学习_04

上课代码

import torch
import numpy as np
import torch.nn.functional as F
import matplotlib.pyplot as plt

x_data = torch.Tensor([[1.0],[2.0],[3.0]])
y_data = torch.Tensor([[0],[0],[1]]) #分别表示分类

class LinearModel(torch.nn.Module):
def __init__(self):
super(LinearModel,self).__init__()
self.linear = torch.nn.Linear(1,1)
def forward(self,x):
y_pred = F.sigmoid(self.linear(x)) #加入逻辑变换
return y_pred

model = LinearModel()

criterion = torch.nn.BCELoss(size_average = False) #将原来MSE变为BCE
optimizer = torch.optim.SGD(model.parameters(),lr = 0.01) #优化器

for epoch in range(1000): #training cycle
y_pred = model(x_data)
loss = criterion(y_pred,y_data)
print(epoch,loss.item())

optimizer.zero_grad()
loss.backward()
optimizer.step()

x = np.linspace(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 = 'c')
plt.xlabel('Hours')
plt.ylabel('probablility of pass')
plt.grid()
plt.show()

标签:逻辑,torch,nn,pred,self,第六,PyTorch,plt,data
From: https://blog.51cto.com/u_15698454/5962689

相关文章