Kaggle competition: COVID19 Global Forecasting (Week 2)
def train_func(model, optimizer, scheduler, loss_func, dataloader, device):
train_loss = 0
model.train()
for inputs, labels in dataloader:
optimizer.zero_grad()
inputs, labels = inputs.to(device), labels.to(device)
 outputs = model(inputs)
 loss = loss_func(outputs, labels)
 loss.backward()
optimizer.step()
scheduler.step()
train_loss += loss.item()
train_loss /= len(dataloader)
 return train_loss<compute_train_metric>
if not sys.warnoptions:
warnings.simplefilter("ignore")
def valid_func(model, loss_func, dataloader, device):
model.eval()
valid_loss = 0
valid_preds = []
for inputs, labels in dataloader:
inputs, labels = inputs.to(device), labels.to(device)
 outputs = model(inputs)
 loss = loss_func(outputs, labels)
 valid_loss += loss.item()
valid_preds.append(outputs.sigmoid().detach().cpu().numpy())
 valid_loss /= len(dataloader)
 valid_preds = np.concatenate(valid_preds)
 return valid_loss, valid_preds<predict_on_test>
def num_days(date1, date2):
dt1 = date1.split("-")
 dt2 = date2.split("-")
 d2 = date(int(dt2[0]),int(dt2[1]),int(dt2[2]))
d1 = date(int(dt1[0]),int(dt1[1]),int(dt1[2]))
delta = d2-d1
return(delta.days )
def inference_fn(model, dataloader, device):
model.eval()
preds = []
for data in dataloader:
inputs = data.to(device)
 with torch.no_grad() :
outputs = model(inputs)
 preds.append(outputs.sigmoid().detach().cpu().numpy())
 preds = np.concatenate(preds)
 return preds<normalization>
train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/train.csv")
 test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-2/test.csv" )
class SmoothBCEwLogits(_WeightedLoss):
def __init__(self, weight=None, reduction='mean', smoothing=0.0):
super().__init__(weight=weight, reduction=reduction)
 self.smoothing = smoothing
self.weight = weight
self.reduction = reduction
@staticmethod
def _smooth(targets:torch.Tensor, n_labels:int, smoothing=0.0):
assert 0 <= smoothing < 1
with torch.no_grad() :
targets = targets *(1.0 - smoothing)+ 0.5 * smoothing
return targets
def forward(self, inputs, targets):
targets = SmoothBCEwLogits._smooth(targets, inputs.size(-1),
self.smoothing)
 loss = F.binary_cross_entropy_with_logits(inputs, targets,self.weight)
 if self.reduction == 'sum':
loss = loss.sum()
elif self.reduction == 'mean':
loss = loss.mean()
return loss<normalization>
train['Province_State'] = train['Province_State'].apply(lambda x: "CountryLevel" if pd.isna(x)else x)
 test['Province_State'] = test['Province_State'].apply(lambda x: "CountryLevel" if pd.isna(x)else x )
class Model(nn.Module):
def __init__(self, num_features, num_targets, hidden_size):
super(Model, self ).__init__()
self.batch_norm1 = nn.BatchNorm1d(num_features)
 self.dense1 = nn.utils.weight_norm(nn.Linear(num_features, hidden_size))
self.batch_norm2 = nn.BatchNorm1d(hidden_size)
 self.dropout2 = nn.Dropout(0.25)
 self.dense2 = nn.utils.weight_norm(nn.Linear(hidden_size, hidden_size))
self.batch_norm3 = nn.BatchNorm1d(hidden_size)
 self.dropout3 = nn.Dropout(0.25)
 self.dense3 = nn.utils.weight_norm(nn.Linear(hidden_size, num_targets))
def forward(self, x):
x = self.batch_norm1(x)
 x = F.leaky_relu(self.dense1(x))
x = self.batch_norm2(x)
 x = self.dropout2(x)
 x = F.leaky_relu(self.dense2(x))
x = self.batch_norm3(x)
 x = self.dropout3(x)
 x = self.dense3(x)
 return x<prepare_x_and_y>
train['Day'] = train['Date'].apply(lambda x: num_days('2020-01-22',x))
test['Day'] = test['Date'].apply(lambda x: num_days('2020-01-22',x))
def run_training(fold, seed):
seed_everything(seed)
 train = preprocess(folds.drop(columns = ['sig_id', 'cp_type']))
train_mask = train['kfold'] != fold
valid_idc = train.loc[~train_mask].index
X_train = train.loc[train_mask].reset_index(drop=True)
 y_train = train_targets.loc[train_mask].reset_index(drop=True)
 X_val = train.loc[~train_mask].reset_index(drop=True)
 y_val = train_targets.loc[~train_mask].reset_index(drop=True)
 X_train.drop(columns=['kfold'], inplace=True)
 X_val.drop(columns=['kfold'], inplace=True)
 test_ = preprocess(test.drop(columns = ['sig_id', 'cp_type']))
train_ds = TabularDataset(X_train.values, y_train.values)
 valid_ds = TabularDataset(X_val.values, y_val.values)
 test_ds = TabularDatasetTest(test_.values)
 train_dl = DataLoader(train_ds, batch_size=params['batch_size'], shuffle=True)
 valid_dl = DataLoader(valid_ds, batch_size=params['batch_size'], shuffle=False)
 test_dl = DataLoader(test_ds, batch_size=params['batch_size'], shuffle=False)
 model = Model(num_features=params['in_size'], num_targets=params['out_size'],
hidden_size=params['hidden_size'])
 model.to(params['device'])
 optimizer = torch.optim.Adam(model.parameters() , lr=params['lr'], weight_decay=params['weight_decay'])
 scheduler = optim.lr_scheduler.OneCycleLR(optimizer=optimizer, pct_start=0.1, div_factor=1e3,
max_lr=1e-2, epochs=params['epochs'], steps_per_epoch=len(train_dl))
loss_fn = nn.BCEWithLogitsLoss()
loss_tr = SmoothBCEwLogits(smoothing=0.001)
 early_stopping_steps = params['early_stopping_steps']
early_step = 0
oof = np.zeros(( train.shape[0], params['out_size']))
best_loss = np.inf
for epoch in range(params['epochs']):
train_loss = train_func(model, optimizer,scheduler, loss_tr, train_dl, params['device'])
 print(f"FOLD: {fold}, EPOCH: {epoch}, train_loss: {train_loss}")
 valid_loss, valid_preds = valid_func(model, loss_fn, valid_dl, params['device'])
 print(f"FOLD: {fold}, EPOCH: {epoch}, valid_loss: {valid_loss}")
 if valid_loss < best_loss:
best_loss = valid_loss
oof[valid_idc] = valid_preds
torch.save(model.state_dict() , f"FOLD{fold}_.pth")
 elif(params['early_stop'] == True):
early_step += 1
if(early_step >= early_stopping_steps):
break
model = Model(num_features=params['in_size'], num_targets=params['out_size'],
hidden_size=params['hidden_size'])
 model.load_state_dict(torch.load(f"FOLD{fold}_.pth"))
model.to(params['device'])
 predictions = np.zeros(( test.shape[0], params['out_size']))
predictions = inference_fn(model, test_dl, params['device'])
 return oof, predictions 
<categorify>
def rmsle(y, y_pred):
assert len(y)== len(y_pred)
 return np.sqrt(np.mean(( np.log(1+y)- np.log(1+y_pred)) **2))
def run_k_fold(n_folds, seed):
oof = np.zeros(( train.shape[0], params['out_size']))
predictions = np.zeros(( test.shape[0], params['out_size']))
for fold in range(n_folds):
oof_, pred_ = run_training(fold, seed)
 predictions += pred_ / n_folds
oof += oof_
return oof, predictions<set_options>
predict_df = pd.DataFrame()
for country in unique_countries:
localdf = train.copy()
testdf = test.copy()
localdf = localdf[localdf.Country_Region == country]
unique_province = localdf['Province_State'].unique()
for province in unique_province:
print(country+" - "+province)
 tdf = localdf[localdf['Province_State']==province]
testdf = test[(test['Country_Region'] == country)&(test['Province_State'] == province)]
polynomial_features= PolynomialFeatures(degree=5)
 x_train = tdf['Day'].values
y_train = tdf['ConfirmedCases'].values
x_train = x_train[:, np.newaxis]
y_train = y_train[:, np.newaxis]
x_poly = polynomial_features.fit_transform(x_train)
 x_poly1 = xgb.DMatrix(x_poly)
 param = {'max_depth':2, 'eta':1, 'objective':'linear' }
model = RandomForestRegressor()
num_round = 2
clf = GridSearchCV(model,
{'max_depth': [2,4,6]
}, verbose=1)
 clf.fit(x_poly, y_train)
 y_insample_pred = clf.predict(x_poly)
 print("RMSE : "+str(np.sqrt(mean_squared_error(y_train,y_insample_pred))))
print("R-sq : "+str(np.sqrt(r2_score(y_train,y_insample_pred))))
print("RMSLE : "+str(np.sqrt(rmsle(y_train,y_insample_pred))))
x_test = testdf['Day']
testdf['ConfirmedCases'] = clf.predict(polynomial_features.fit_transform(x_test[:,np.newaxis]))
polynomial_features1= PolynomialFeatures(degree=4)
 y_train = tdf['Fatalities'].values
y_train = y_train[:, np.newaxis]
model = RandomForestRegressor()
clf = GridSearchCV(model,
{'max_depth': [2,4,6]
}, verbose=1)
 clf.fit(x_poly, y_train)
 y_insample_pred = clf.predict(x_poly)
 print("RMSE : "+str(np.sqrt(mean_squared_error(y_train,y_insample_pred))))
print("R-sq : "+str(np.sqrt(r2_score(y_train,y_insample_pred))))
print("RMSLE : "+str(np.sqrt(rmsle(y_train,y_insample_pred))))
x_test = testdf['Day']
testdf['Fatalities'] = clf.predict(polynomial_features.fit_transform(x_test[:,np.newaxis]))
predict_df = pd.concat([predict_df,testdf],axis=0 )
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(seed=42 )<categorify>
predict_df.ConfirmedCases = predict_df.ConfirmedCases.apply(lambda x: np.round(x,0))
predict_df.Fatalities = predict_df.Fatalities.apply(lambda x: np.round(x,0))
seeds = [0, 1, 2, 3, 4, 5, 6]
oof = np.zeros(( train.shape[0], params['out_size']))
predictions = np.zeros(( test.shape[0], params['out_size']))
for seed in seeds:
oof_, predictions_ = run_k_fold(params['n_folds'], seed)
 oof += oof_ / len(seeds)
 predictions += predictions_ / len(seeds )<concatenate>
predict_df[['ForecastId','ConfirmedCases','Fatalities']].to_csv("submission.csv",index=False )
<concatenate><EOS> 
newdf = predict_df.merge(right=train, how="inner",on=['Province_State', 'Country_Region', 'Date'])
 newdf.drop(['Day_x','Day_y','Id','ForecastId'],axis=1, inplace=True)
 newdf['ConfirmedCases_LSE'] =(np.log(newdf.ConfirmedCases_x+1)-np.log(newdf.ConfirmedCases_y+1)) **2
newdf['Fatalities_LSE'] =(np.log(newdf.Fatalities_x+1)-np.log(newdf.Fatalities_y+1)) **2
print(np.sqrt(np.sum(newdf.ConfirmedCases_LSE)/len(newdf)))
 print(np.sqrt(np.sum(newdf.Fatalities_LSE)/len(newdf)) )