Logistic Function
Sigmoid Functions
交叉熵
备注:BCE越小分布越接近,越好
深度学习基本框架
上课代码
import torch标签:逻辑,torch,nn,pred,self,第六,PyTorch,plt,data From: https://blog.51cto.com/u_15698454/5962689
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()