知识点补充
- torch.nn.BCELoss(size_average=False) 求交叉熵的函数
实现的步骤
- prepare dataset 准备数据集
- design model using class 使用类来设计模型
- constuct loss and optimizer 创建loss 和优化器
- taining cycle 循环训练
斜体样式整体与linear的实现差不多,就把y^和loss fuction 两处做了修改
完整代码
import torch
import torch.nn.functional as F
#prepare dataset
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[0], [0], [1]])
#design model
class LogistiicRegressionModel(torch.nn.Module):
def __init__(self):
super(LogistiicRegressionModel,self).__init__()
self.linear = torch.nn.Linear(1, 1)
def forward(self, x):
y_pred = F.sigmoid(self.linear(x)) #在y^外边套一层sigmoid函数
return y_pred
model = LogistiicRegressionModel()
#constuct loss and optimizer
criterion = torch.nn.BCELoss(size_average=False) #有MSE改为BCE
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
#Training
for epoch in range(10000):
y_pred = model(x_data)
loss = criterion(y_pred,y_data)
print(epoch, loss.item())
optimizer.zero_grad() #对之前的梯度清零
loss.backward() #反向传播计算梯度
optimizer.step() #更新参数
print('w = ', model.linear.weight.item())
print('b = ', model.linear.bias.item())
x_test = torch.Tensor([4.0])
y_test_pred = model(x_test)
print("y_pred:", y_test_pred.item())