【python】第一个pytorch训练的完整模型

# encoding=utf-8
import torch
from tqdm import tqdm
from torch import nn,optim
import time
from torch.utils.data import Dataset,DataLoader
import pandas as pd

#定义自己的模型
class Traindataset(Dataset):
    #x features
    #y Targets 可以为none
    def __init__(self,x,y=None):
        super(Traindataset,self).__init__()
        if y is None:
            self.y=y
        else:
            self.y=torch.FloatTensor(y)
        self.x=torch.FloatTensor(x)
    def __getitem__(self,index):
        if self.y is None:
            return self.x[index]
        else:
            return self.x[index],self.y[index]
    def __len__(self):
        return len(self.x)


#定义自己的模型
class MyFirstModel(nn.Module):
    def __init__(self,input_dim):
        super(MyFirstModel,self).__init__()
        #定义神经网络层
        self.net=nn.Sequential(
            nn.Linear(input_dim,64),
            nn.ReLU(),
            nn.Linear(64,32),
            nn.ReLU(),
            nn.Linear(32,1)
        )
    def forward(self,x):
        x=self.net(x)
        #将其转换为一维的
        x=x.squeeze(1)
        return x

if __name__=="__main__":
    #layer=nn.Linear(94,64)
    #print(layer.weight.shape)
    #print(layer.bias.shape)
    device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
    train_path="/home/lenovo/LwkFolder/Lhy_Machine_Learning-main/2021 ML/01 Introduction/作业HW1/covid.train.csv"
    train_data=pd.read_csv(train_path,na_values="Nan").values
    print(train_data.shape)
    x_train,y_train=train_data[:,:-1],train_data[:,-1]
    print(x_train.shape)
    print(y_train.shape)

    #生成dataset
    traindataset=Traindataset(x_train,y_train)
    #生成Dataloader
    #batch_size 分组每组分的个数 shuffle一般设置为True
    traindataloader=DataLoader(traindataset,batch_size=32,shuffle=True,pin_memory=True)

    #定义model
    model=MyFirstModel(input_dim=x_train.shape[1]).to(device)
    #选择loss
    criterion=nn.MSELoss(reduction="mean")
    #选择optimzer 设置优化选择器
    optimzer=optim.SGD(model.parameters(),lr=1e-5,momentum=0.9)

    #执行训练
    epochs=10
    for epoch in range(epochs):
        #设置为训练模式
        model.train()
        train_pbar=tqdm(traindataloader,position=0)
        for x,y in train_pbar:
            #梯度归零
            x,y=x.to(device),y.to(device)
            optimzer.zero_grad()
            pred=model(x)
            loss=criterion(pred,y)
            loss.backward()
            optimzer.step()


    #执行测试
    test_path="/home/lenovo/LwkFolder/Lhy_Machine_Learning-main/2021 ML/01 Introduction/作业HW1/covid.test.csv"
    xtestcsv=pd.read_csv(test_path,na_values="Nan").values
    x_test=xtestcsv[:,:]
    print(x_test.shape)
    testdataset=Traindataset(x_test)
    testdatalodaer=DataLoader(testdataset,batch_size=64,shuffle=True)
    #执行预测
    model.eval()
    preds=[]
    testdatalodaertq=tqdm(testdatalodaer,position=0)
    for x in testdatalodaertq:
        x=x.to(device)
        with torch.no_grad():
            pred=model(x)
            preds.append(pred)
    print(preds)

    #保存模型
    pthpath="/home/lenovo/LwkFolder/deepLearningCode/firstmodel.pth"
    torch.save(model.state_dict(),pthpath)

    #加载模型
    cpkg=torch.load(pthpath)
    model.load_state_dict(cpkg)











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值