当前位置:网站首页>pytorch-06. Logistic regression
pytorch-06. Logistic regression
2022-08-10 05:55:00 【The study of surface letter ape】
#cross-entropy#BCELoss Binary Cross Entropyimport torchimport numpy as npimport matplotlib.pyplot as pltx_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_predmodel = 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 biasprint('w=',model.linear.weight.item())print('b=',model.linear.bias.item())#--------------------------------------------------------# Test Modelx_test = torch.Tensor([[4.0]])y_test = model(x_test)print('y_pred=',y_test.data.item())x = np.linspace(0,10,200) #0 to 10 took 200 pointsx_t = torch.Tensor(x).view((200,1)) # becomes a matrix with 200 rows and 1 columny_t = model(x_t)y = y_t.data.numpy()plt.plot(x,y)plt.plot([0,10],[0.5,0.5],c='r') #Draw a red lineplt.xlabel('Hours')plt.ylabel('Probability of Pass')plt.grid()plt.show()
边栏推荐
猜你喜欢
随机推荐
The latest and most complete digital collection sales calendar-07.26
分享一款恋爱星座男女配对微信小程序源码
Reflection 【Notes】
棋类游戏-五子棋小游戏
cesium add point, move point
Decentralized and p2p networks and traditional communications with centralization at the core
.NET操作Excel高效低内存的开源框架 - MiniExcel
卷积神经网络(CNN)实现mnist手写数字识别
链读精选:星巴克着眼于数字收藏品并更好地吸引客户
Operation table Function usage
链读好文:热加密存储与冷加密存储有什么区别?
wiki confluence 安装
String common methods
Notes 1
树结构——2-3树图解
泛型笔记()()()
图片批量添加水印批量缩放图片到指定大小
MySQL中MyISAM为什么比InnoDB查询快
堆的原理与实现以及排序
opencv