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):
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__":
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)
traindataset=Traindataset(x_train,y_train)
traindataloader=DataLoader(traindataset,batch_size=32,shuffle=True,pin_memory=True)
model=MyFirstModel(input_dim=x_train.shape[1]).to(device)
criterion=nn.MSELoss(reduction="mean")
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)