pytorch 入门学习加载数据集

pytorch 入门学习加载数据集

import  torch
import numpy  as np
import  torchvision
import  numpy as np
from  torch.utils.data import Dataset
from  torch.utils.data import DataLoader

import  torch.nn.functional as F
import matplotlib.pyplot as plt



class DiabetesDataset(Dataset):
    def __init__(self, filepath):
        xy = np.loadtxt('diabetes.csv.gz', delimiter=',', dtype=np.float32)
        self.len = xy.shape[0]
        self.x_data = torch.from_numpy(xy[:, :-1])  # -1 表示最后一列不要
        self.y_data = torch.from_numpy(xy[:, [-1]])
    def __getitem__(self, index):
        return self.x_data[index],self.y_data[index]
    def __len__(self):
        return self.len

dataset = DiabetesDataset('diabetes.csv.gz')
train_loder = DataLoader(dataset=dataset,
                         batch_size=32,
                         shuffle=True,
                         num_workers=2)

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.relu = torch.nn.ReLU()      #torch.nn.Sigmoid()
        self.sigmoid  = torch.nn.Sigmoid()

    def forward(self,x):
        x = self.relu(self.linear1(x))
        x = self.relu(self.linear2(x))
        x = self.sigmoid(self.linear3(x))   #使用 ReLU + Sigmoid 的结合, 最后一层嵌套 Sigmoid
        return  x

model = Model()

criterion = torch.nn.BCELoss(size_average=True)
optimizer = torch.optim.SGD(model.parameters(),lr=0.01)

if __name__ == '__main__':

    for epoch in range(100):
        for i,data in enumerate(train_loder,0):
            #1. prepare data
            inputs,labels = data
            #2. Forward
            y_pred = model(inputs)
            loss = criterion(y_pred,labels)
            print(epoch,loss.item())
            #3. Backward
            optimizer.zero_grad()
            loss.backward()
            #4. Update
            optimizer.step()



#outs:
96 0.6488460302352905
96 0.6837149858474731
96 0.6773790717124939
96 0.5895138382911682
96 0.6454289555549622
96 0.6540895700454712
96 0.634219229221344
96 0.5071176886558533
96 0.6521698832511902
96 0.5890663862228394
96 0.6260353326797485
96 0.6036486625671387
96 0.6821258068084717
96 0.6444405913352966
96 0.7117087244987488
96 0.6727374792098999
96 0.5942217707633972
96 0.572909951210022
96 0.5683532953262329
96 0.5906638503074646
96 0.5729745626449585
96 0.6592338681221008
96 0.5666840672492981
96 0.5628921389579773
D:\Anaconda3\envs\pcd\lib\site-packages\torch\nn\_reduction.py:43: UserWarning: size_average and reduce args will be deprecated, please use reduction='mean' instead.
  warnings.warn(warning.format(ret))
D:\Anaconda3\envs\pcd\lib\site-packages\torch\nn\_reduction.py:43: UserWarning: size_average and reduce args will be deprecated, please use reduction='mean' instead.
  warnings.warn(warning.format(ret))
97 0.5504413843154907
97 0.5592724680900574
97 0.6298409700393677
97 0.5115721821784973
97 0.5389221906661987
97 0.5870144963264465
97 0.5902682542800903
97 0.6780911684036255
97 0.6389861702919006
97 0.7095761895179749
97 0.6540379524230957
97 0.6255027055740356
97 0.6726484298706055
97 0.6435151100158691
97 0.6405020952224731
97 0.6269117593765259
97 0.6627392172813416
97 0.6168067455291748
97 0.6124289035797119
97 0.713973879814148
97 0.5994608402252197
97 0.5881710648536682
97 0.6652423143386841
97 0.5875065922737122
D:\Anaconda3\envs\pcd\lib\site-packages\torch\nn\_reduction.py:43: UserWarning: size_average and reduce args will be deprecated, please use reduction='mean' instead.
  warnings.warn(warning.format(ret))
D:\Anaconda3\envs\pcd\lib\site-packages\torch\nn\_reduction.py:43: UserWarning: size_average and reduce args will be deprecated, please use reduction='mean' instead.
  warnings.warn(warning.format(ret))
98 0.6116299033164978
98 0.6460859179496765
98 0.6689874529838562
98 0.5503941774368286
98 0.6141761541366577
98 0.5878812074661255
98 0.692798376083374
98 0.5875721573829651
98 0.5414928793907166
98 0.6070823073387146
98 0.6634365916252136
98 0.6647754311561584
98 0.5649587512016296
98 0.5715025663375854
98 0.6386815905570984
98 0.5531622767448425
98 0.6154085993766785
98 0.6604638695716858
98 0.6641201972961426
98 0.5941271781921387
98 0.6185081005096436
98 0.6462123990058899
98 0.6671874523162842
98 0.685947060585022
D:\Anaconda3\envs\pcd\lib\site-packages\torch\nn\_reduction.py:43: UserWarning: size_average and reduce args will be deprecated, please use reduction='mean' instead.
  warnings.warn(warning.format(ret))
D:\Anaconda3\envs\pcd\lib\site-packages\torch\nn\_reduction.py:43: UserWarning: size_average and reduce args will be deprecated, please use reduction='mean' instead.
  warnings.warn(warning.format(ret))
99 0.5591648817062378
99 0.7278904914855957
99 0.5913693904876709
99 0.5824277400970459
99 0.6379620432853699
99 0.6897075176239014
99 0.5347273349761963
99 0.6482306718826294
99 0.7141227722167969
99 0.5258570313453674
99 0.6128413081169128
99 0.6075040698051453
99 0.521778404712677
99 0.6718929409980774
99 0.661381721496582
99 0.6632771492004395
99 0.6090944409370422
99 0.5503813624382019
99 0.6275145411491394
99 0.698398232460022
99 0.6591424345970154
99 0.5420821309089661
99 0.6173321604728699
99 0.634967565536499

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/weixin_41281151/article/details/108407787