赞
踩
import torch import torch.nn.functional as F # functional下有很多函数包 x_data = torch.Tensor([[1.0], [2.0], [3.0]]) # 3*1 Tensor y_data = torch.Tensor([[0], [0], [1]]) # 3*1 Tensor 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 = F.sigmoid(self.linear(x)) return y_pred model = LogisticRegressionModel() critetion = torch.nn.BCELoss(size_average=False) optimizer = torch.optim.SGD(model.parameters(), lr=0.01) for epoch in range(100): y_pred = model(x_data) loss = critetion(y_pred, y_data) print(epoch, loss.item()) optimizer.zero_grad()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。