Kaggle competition: COVID19 Global Forecasting (Week 2)
<init_hyperparams><EOS> 
import pandas as pd
<SOS> metric: MCRMSLE Kaggle data source: covid19-global-forecasting-(week-2)<choose_model_class>
import pandas as pd
class MLP_1HL(nn.Module):
def __init__(self, dim_in, dim_hidden1, dim_hidden2, sparse=False, bn=True):
super(MLP_1HL, self ).__init__()
self.layer1 = nn.Sequential(
 nn.Dropout(0.2),
nn.Linear(dim_in, dim_hidden1), 
)
 self.layer2 = nn.Sequential(
 nn.ReLU() ,
nn.Linear(dim_hidden1, 206))
if bn:
self.bn = nn.BatchNorm1d(dim_hidden1)
 self.bn2 = nn.BatchNorm1d(dim_in)
 def forward(self, x, lower_f):
if lower_f is not None:
x = torch.cat([x, lower_f], dim=1)
 x = self.bn2(x)
 out = self.layer1(x)
 return out, self.layer2(out)
 @classmethod
def get_model(cls, stage, params):
if stage == 0:
dim_in = params["feat_d"]
else:
dim_in = params["feat_d"] + params["hidden_size"]
model = MLP_1HL(dim_in, params["hidden_size"], params["hidden_size"])
 return model
class MLP_2HL(nn.Module):
def __init__(self, dim_in, dim_hidden1, dim_hidden2, sparse=False, bn=True):
super(MLP_2HL, self ).__init__()
self.bn2 = nn.BatchNorm1d(dim_in)
 self.layer1 = nn.Sequential(
 nn.Dropout(0.2),
nn.Linear(dim_in, dim_hidden1),
nn.ReLU() ,
nn.BatchNorm1d(dim_hidden1),
nn.Dropout(0.4),
nn.Linear(dim_hidden1, dim_hidden2)
)
 self.layer2 = nn.Sequential(
 nn.ReLU() ,
nn.Linear(dim_hidden2, 206)
)
 def forward(self, x, lower_f):
if lower_f is not None:
x = torch.cat([x, lower_f], dim=1)
 x = self.bn2(x)
 middle_feat = self.layer1(x)
 out = self.layer2(middle_feat)
 return middle_feat, out
@classmethod
def get_model(cls, stage, params):
if stage == 0:
dim_in = params["feat_d"]
else:
dim_in = params["feat_d"] + params["hidden_size"]
model = MLP_2HL(dim_in, params["hidden_size"], params["hidden_size"])
 return model<normalization>
sub_expo = pd.read_csv('.. /input/submissions-covid19-public/submission_expo.csv')
 sub_expo = sub_expo.rename({'ConfirmedCases': 'ConfirmedCases_expo', 'Fatalities': 'Fatalities_expo'}, axis=1)
 sub_gam = pd.read_csv('.. /input/submissions-covid19-public/submission_gam.csv')
 sub_gam = sub_gam.rename({'ConfirmedCases': 'ConfirmedCases_gam', 'Fatalities': 'Fatalities_gam'}, axis=1)
 sub_power = pd.read_csv('.. /input/submissions-covid19-public/submission_power.csv')
 sub_power = sub_power.rename({'ConfirmedCases': 'ConfirmedCases_power', 'Fatalities': 'Fatalities_power'}, axis=1 )
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<choose_model_class>
sub = sub_expo.copy()
sub = sub.merge(sub_gam, on='ForecastId', how='left')
 sub = sub.merge(sub_power, on='ForecastId', how='left')
 sub.head()
def get_optim(params, lr, weight_decay):
optimizer = optim.Adam(params, lr, weight_decay=weight_decay)
 return optimizer
def logloss(net_ensemble, test_loader):
loss = 0
total = 0
loss_f = nn.BCEWithLogitsLoss()
for data in test_loader:
x = data["x"].cuda()
y = data["y"].cuda()
with torch.no_grad() :
_, out = net_ensemble.forward(x)
 loss += loss_f(out, y)
 total += 1
return loss / total 
<set_options>
sub['ConfirmedCases'] = sub[[c for c in sub.columns if c.startswith('ConfirmedCases_')]].mean(axis=1)
 sub['Fatalities'] = sub[[c for c in sub.columns if c.startswith('Fatalities_')]].mean(axis=1)
 sub.head()
device = "cuda" if torch.cuda.is_available() else "cpu"<feature_engineering>
sub[['ForecastId', 'ConfirmedCases', 'Fatalities']].to_csv('submission.csv', index=False )