知识点补充
- 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
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[0], [0], [1]])
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))
return y_pred
model = LogistiicRegressionModel()
criterion = torch.nn.BCELoss(size_average=False)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
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())
|