import torch
import numpy as np
import torchvision
import numpy as np
import torch.nn.functional as F
import matplotlib.pyplot as plt
classModel(torch.nn.Module):def__init__(self):super(Model, self).__init__()
self.linear = torch.nn.Linear(8,1)#输入八维,输出一维
self.sigmoid -= torch
classModel(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.relu = torch.nn.ReLU()#torch.nn.Sigmoid()
self.sigmoid = torch.nn.Sigmoid()defforward(self,x):
x = self.relu(self.linear1(x))
x = self.relu(self.linear2(x))
x = self.sigmoid(self.linear3(x))#使用 ReLU + Sigmoid 的结合, 最后一层嵌套 Sigmoidreturn x
model = Model()
criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(model.parameters(),lr=0.1)
xy = np.loadtxt('diabetes.csv.gz',delimiter=',',dtype=np.float32)
x_data = torch.from_numpy(xy[:,:-1])#-1 表示最后一列不要
y_data = torch.from_numpy(xy[:,[-1]])for epoch inrange(1000):#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()
#out:00.752414524555206310.746213793754577620.740322411060333330.73473298549652140.729437589645385750.724413871765136760.719658136367797970.715151250362396280.710871636867523290.7068101763725281100.7029609680175781110.6993327736854553120.6958991885185242130.6926288604736328140.6895169019699097150.6865927577018738160.6838260889053345170.6811893582344055180.67868971824646190.6763197183609009200.6740730404853821210.671952486038208220.6699475049972534230.6680536866188049240.6662566661834717250.6645610332489014260.6629753112792969270.6615006327629089280.6601430177688599290.6588588953018188300.657647967338562310.6565213799476624320.6554760336875916330.654509425163269340.653608500957489350.652764081954956360.6519767642021179370.6512464284896851380.6505675911903381390.649936318397522400.6493468880653381410.648797869682312420.6482827663421631430.6478033065795898440.6473581790924072450.646946132183075460.6465625762939453470.6462015509605408480.645862340927124490.6455459594726562500.6452481150627136510.6449684500694275520.6447051763534546530.6444559693336487540.6442214250564575550.6439983248710632560.6437851786613464570.6435820460319519580.6433899402618408590.6432061791419983600.643031120300293610.6428627967834473620.6426998972892761630.6425420641899109640.6423884034156799650.6422398686408997660.6420952677726746670.6419550776481628680.6418185234069824690.6416840553283691700.641552209854126710.6414230465888977720.6412957310676575730.6411715149879456740.6410495638847351750.6409288644790649760.640809178352356770.6406914591789246780.6405748724937439790.6404593586921692800.6403442025184631810.6402301788330078820.6401163935661316830.6400025486946106840.639888346195221850.6397725939750671860.6396569013595581870.6395405530929565880.6394234299659729890.6393055319786072900.639186680316925910.6390676498413086920.6389491558074951930.6388294696807861940.6387082934379578950.6385860443115234960.6384627819061279970.638337254524231980.638209342956543990.6380797624588013
Process finished with exit code 0