• 欢迎光临~

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

开发技术 开发技术 2022-05-21 次浏览

目前以自己的能力只能过medium baseline。

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

medium baseline需要加一些优化。

首先前面表示州的one-hot向量过于稀疏,所以我直接忽略。

由于输入数据全是正数,而且我使用了relu,所以训练中有时会导致loss一直不变,所以我将输入数据进行了归一化。

加了dropout正则化,和kaiming初始化。

最好成绩能到1.03426

数据读取器:

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


def loader(location):
    data = genfromtxt(location, delimiter=',', skip_header=1)
    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 dataloader import loader
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'
PATH = './runs/newest'
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')
        self.optim = optim.Adam(params=self.parameters(), lr=learning_rate)
        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):
        self.optim.zero_grad()
        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)
    data = torch.tensor(loader(train_data_location), requires_grad=False)
    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()
        writer.add_scalar('Loss/epoch', loss, epoch)
        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))
    data = norm(torch.tensor(loader(test_data_location)))
    predicts = net.forward(data)
    tmp = predicts.detach().numpy()
    ret = pandas.DataFrame(tmp)
    ret.to_csv('result.csv', index=True, header=['tested_positive'], )

 

程序员灯塔
转载请注明原文链接:李宏毅2021深度学习 作业1 COVID-19 Cases Prediction
喜欢 (0)