1,代码如下:
import numpy as np
import torch
import matplotlib.pyplot as plt
xy = np.loadtxt('deep_learn/diabetes.csv.gz',delimiter=',',dtype=np.float32)
print(xy)
x_data = torch.from_numpy(xy[:,:-1])
y_data = torch.from_numpy(xy[:,[-1]])
class Model(torch.nn.Module):
def __init__(self) -> None:
super(Model, self).__init__()
self.linear1 = torch.nn.Linear(8, 6)
self.linear2 = torch.nn.Linear(6, 4)
self.linear3 = torch.nn.Linear(4, 1)
self.sigmoid = torch.nn.Sigmoid()
def forward(self, x):
x = self.sigmoid(self.linear1(x))
x = self.sigmoid(self.linear2(x))
x = self.sigmoid(self.linear3(x))
return x
model = Model()
criterion = torch.nn.BCELoss(reduction='mean')
optimizer = torch.optim.SGD(model.parameters(),lr=0.1)
epoch_list = []
loss_list = []
for epoch in range(100):
# Forward
y_pred = model(x_data)
loss = criterion(y_pred,y_data)
print(epoch,loss.item())
epoch_list.append(epoch)
loss_list.append(loss.item())
# Backward
optimizer.zero_grad()
loss.backward()
# Update
optimizer.step()
plt.plot(epoch_list, loss_list)
plt.ylabel('loss')
plt.xlabel('epoch')
plt.show()