#处理多维特征的输入
#prepare Dateset
import numpy as np
import torch
xy = np.loadtxt('./diabetes.csv',delimiter = ',',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.activate = torch.nn.ReLU()
self.sigmoid= torch.nn.Sigmoid()
def forward(self, x):
x = self.activate(self.linear1(x))
x = self.activate(self.linear2(x))
x = self.sigmoid(self.linear3(x))
return x
model = Model()
#LOSS
criterion = torch.nn.BCELoss(reduction='mean')
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
#train
#激活函数 Relu + Sigmoid 效果更好
epochs = 10000
for epoch in range(epochs):
# Forward
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, loss.item())
# Backward
optimizer.zero_grad()
loss.backward()
# Update
optimizer.step()
处理多维特征的输出(糖尿病数据)
猜你喜欢
转载自blog.csdn.net/weixin_46815330/article/details/115327066
今日推荐
周排行