<jupyter_start><jupyter_text>**Homework 1: COVID-19 Cases Prediction (Regression)**  Download dataDownload it from the Google driver links below, or from Kaggle then upload data manually to the workspace.<jupyter_code>!gdown --id '1kLSW_-cW2Huj7bh84YTdimGBOJaODiOS' --output covid.train.csv
!gdown --id '1iiI5qROrAhZn-o4FPqsE97bMzDEFvIdg' --output covid.test.csv<jupyter_output>Downloading...
From: https://drive.google.com/uc?id=1kLSW_-cW2Huj7bh84YTdimGBOJaODiOS
To: /content/covid.train.csv
100% 2.49M/2.49M [00:00<00:00, 160MB/s]
Downloading...
From: https://drive.google.com/uc?id=1iiI5qROrAhZn-o4FPqsE97bMzDEFvIdg
To: /content/covid.test.csv
100% 993k/993k [00:00<00:00, 75.0MB/s]<jupyter_text>Import packagesImport all packages that are needed.<jupyter_code># Numerical Operations
import math
import numpy as np

# Reading/Writing Data
import pandas as pd
import os
import csv

# Show Trianing Process in Progress Bar
from tqdm import tqdm

# Pytorch
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader, random_split  # can split training dataset into trining and validation

# For Plotting Learning Curve
from torch.utils.tensorboard import SummaryWriter<jupyter_output><empty_output><jupyter_text>Some Utility FunctionsDon't have to modify this part but have to understand the usage of each function.<jupyter_code>from numpy.random.mtrand import triangular
def same_seed(seed):
  '''Fix random number generator seeds for reproducibility.'''
  torch.backends.cudnn.deterministic = True  # if true, the convolutional algorithm is the same each time
  torch.backends.cudnn.benchmark = False  # if true, auto-tuner in cuDNN will find the most efficient algorithm each time automatically
  np.random.seed(seed)  # generate the same random number with the same seed
  torch.manual_seed(seed)  # set the seed for generating random numbers
  if torch.cuda.is_available():
    torch.cuda.manual_seed_all(seed)  # set the seed for generating random numbers on all GPUs

def train_valid_split(data_set, valid_ratio, seed):
  '''Split provided training data into training set and validation set'''
  valid_set_size = int(valid_ratio * len(data_set))
  train_set_size = len(data_set) - valid_set_size
  train_set, valid_set = random_split(data_set, [train_set_size, valid_set_size], generator=torch.Generator().manual_seed(seed))
  return np.array(train_set), np.array(valid_set)  # return in np.array

def predict(test_loader, model, device):
  '''Get model prediction'''
  model.eval()  # set the model to evaluation mode
  preds = []
  for x in tqdm(test_loader):  # tqdm(iterator)
    x = x.to(device)
    with torch.no_grad():
      pred = model(x)  # it's on device right now
      preds.append(pred.detach().cpu())
  preds = torch.cat(preds, dim=0).numpy()
  return preds<jupyter_output><empty_output><jupyter_text>Dataset<jupyter_code>class COVID19Dataset(Dataset):
  '''
  x: Features
  y: Targets, if none, do prediction
  '''
  def __init__(self, x, y=None):
    if y is None:
      self.y = y
    else:
      self.y = torch.FloatTensor(y)
    self.x = torch.FloatTensor(x)

  def __getitem__(self, idx):
    if self.y is None:
      return self.x[idx]
    else:
      return self.x[idx], self.y[idx]

  def __len__(self):
    return len(self.x)<jupyter_output><empty_output><jupyter_text>Neural Network ModelTry out different model architectures by modifying the class below<jupyter_code>class My_Model(nn.Module):
  def __init__(self, input_dim):
    super(My_Model, self).__init__()
    # TODO: modify model's structure, be aware of dimensions
    # 2022/3/3 Try default structure with default input_dim
    self.layers = nn.Sequential(
        nn.Linear(input_dim, 16), 
        nn.ReLU(), 
        nn.Linear(16, 8), 
        nn.ReLU(), 
        nn.Linear(8, 1)
    )
  
  def forward(self, x):
    x = self.layers(x)
    x = x.squeeze(1)  # (B, 1) -> (B)
    return x<jupyter_output><empty_output><jupyter_text>Feature SelectionNot all features are useful. Carefully choose them by modifying the function below.<jupyter_code>def select_feat(train_data, valid_data, test_data, select_all=True):
  '''Select useful features'''
  y_train, y_valid = train_data[:,-1], valid_data[:,-1]
  raw_x_train, raw_x_valid, raw_x_test = train_data[:,:-1], valid_data[:,:-1], test_data[:,:-1]

  if select_all:
    feat_idx = list(range(raw_x_train.shape[1]))
  else:
    feat_idx = [0,1,2,3,4,5] # TODO: Select suitable columns
  
  return raw_x_train[:,feat_idx], raw_x_valid[:,feat_idx], raw_x_test[:,feat_idx], y_train, y_valid<jupyter_output><empty_output><jupyter_text>Training Loop<jupyter_code>def trainer(train_loader, valid_loader, model, config, device):
  criterion = nn.MSELoss(reduction='mean')  # loss function

  # define optimization algorithm
  #<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11055797552375025, 'fraction_numerical': 0.019290603609209707, 'mean_word_length': 4.67962308598351, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '433817', 'license': 'MIT', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1671, 'n_words': 426, 'path': 'HW01.ipynb', 'repo_name': 'cauchy221/ML2022Spring--', 'size': '4821'}