• 欢迎光临~

# 李宏毅2021深度学习 作业1 COVID-19 Cases Prediction

simple baseline过很简单，每行数据不做任何处理，直接作为网络的输入就行，网络也不用优化。

medium baseline需要加一些优化。

```from collections import namedtuple
import csv
import pandas as pd
from numpy import genfromtxt

return data[:, 41:]```

main：

```import os
import shutil

import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from torch.utils.tensorboard import SummaryWriter
import numpy as np
import pandas

train_data_location = './hw1/covid.train.csv'
test_data_location = './hw1/covid.test.csv'
batch_size = 2400
tot_epoch = 20000
learning_rate = 5e-3
hidden_size = 32

class Net(nn.Module):

def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(53, hidden_size)
self.fc2 = nn.Linear(hidden_size, 1)
self.dropout = nn.Dropout(p=0.2)
self.relu = nn.ReLU()
self.bn = nn.BatchNorm1d(hidden_size)
self.sigmoid = nn.Sigmoid()
self.tanh = nn.Tanh()
self.loss_func = nn.MSELoss(reduction='mean')
torch.nn.init.kaiming_normal_(self.fc1.weight)
torch.nn.init.kaiming_normal_(self.fc2.weight)

def forward(self, x):
ret = self.dropout(self.bn(self.relu(self.fc1(x))))
ret = self.fc2(ret)
return ret

def update(self, data, target):
loss = self.cal_loss(data, target)
loss.backward()
self.optim.step()
return loss

def cal_loss(self, data, target):
predict = self.forward(data)
loss = self.loss_func(predict, target)
return loss

def norm(x):
mean = torch.mean(x, dim=0)
std = torch.std(x, dim=0)
return (x - mean) / std

if __name__ == '__main__':
use_gpu = torch.cuda.is_available()
print(use_gpu)
torch.set_default_tensor_type(torch.DoubleTensor)
epoch = 0
shutil.rmtree(PATH)
os.mkdir(PATH)
writer = SummaryWriter(log_dir=PATH)
net = Net()
if use_gpu:
net = net.cuda()
net.train()
tmp = data[torch.randperm(data.size(0))]
batch_data = tmp[0:batch_size]
test_data = tmp[batch_size:]
while epoch < tot_epoch:
train_data, train_target = torch.split(batch_data, [53, 1], dim=1)
inputs = norm(train_data)
if use_gpu:
inputs = inputs.cuda()
train_target = train_target.cuda()
loss = net.update(inputs, train_target).cpu()
if epoch % 100 == 0:
print(loss)
epoch += 1
net.eval()
epoch = 0
net = net.cpu()
train_data, train_target = torch.split(batch_data, [53, 1], dim=1)
train_data = norm(train_data)
print('train_loss: ', net.cal_loss(train_data, train_target))
test_data, test_target = torch.split(test_data, [53, 1], dim=1)
test_data = norm(test_data)
print('test_loss: ', net.cal_loss(test_data, test_target))