处理多维特征的输入
import torch
import numpy as np
import matplotlib.pyplot as plt
import sklearn
import pandas as pd
xy = np.loadtxt("./datasets/diabetes.csv", delimiter=',', skiprows=1, dtype=np.float32)
x_data = torch.from_numpy(xy[:, :-1])
y_data = torch.from_numpy(xy[:, [-1]])
class Model(torch.nn.Module):
def __init__(self):
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()
self.relu = torch.nn.ReLU()
self.leakyrelu = torch.nn.LeakyReLU()
def forward(self, x):
x = self.relu(self.linear1(x))
x = self.relu(self.linear2(x))
x = self.sigmoid(self.linear3(x))
return x
model = Model()
criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
for epoch in range(1000):
y_pred = model(x_data)
l = criterion(y_pred, y_data)
print(f"epoch: {epoch}\tloss: {l.item()}")
optimizer.zero_grad()
l.backward()
optimizer.step()