【Pytorch深度学习开发实践学习】B站刘二大人课程笔记整理lecture06 Logistic回归
课程网址
Pytorch深度学习实践
部分课件内容:
import torchx_data =torch.tensor([[1.0],[2.0],[3.0]])
y_data =torch.tensor([[0.0],[0.0],[1.0]])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(reduction='sum')
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)for epoch in range(100):y_pred = model(x_data)loss = criterion(y_pred, y_data)optimizer.zero_grad()loss.backward()optimizer.step()print(epoch,loss.data)