{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport gc\nimport pickle\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport torch\n\nimport torch.nn as nn\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.metrics import f1_score","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-14T12:07:46.364650Z","iopub.execute_input":"2022-08-14T12:07:46.365651Z","iopub.status.idle":"2022-08-14T12:07:47.518428Z","shell.execute_reply.started":"2022-08-14T12:07:46.365548Z","shell.execute_reply":"2022-08-14T12:07:47.517368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:07:47.520363Z","iopub.execute_input":"2022-08-14T12:07:47.520852Z","iopub.status.idle":"2022-08-14T12:07:47.527027Z","shell.execute_reply.started":"2022-08-14T12:07:47.520821Z","shell.execute_reply":"2022-08-14T12:07:47.525690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Config","metadata":{}},{"cell_type":"code","source":"class CFG:\n    BATCH_SIZE=2048\n    N_EPOCHS=10","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:07:47.529170Z","iopub.execute_input":"2022-08-14T12:07:47.530019Z","iopub.status.idle":"2022-08-14T12:07:47.538001Z","shell.execute_reply.started":"2022-08-14T12:07:47.529976Z","shell.execute_reply":"2022-08-14T12:07:47.537030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# load dataset","metadata":{}},{"cell_type":"code","source":"%%time\ntrain_customer_ids = np.load(\"../input/amex-sequence-dataset-v3/train_sequence/all_train_customerIds.npy\")\n\nxseq = np.load(\"../input/amex-sequence-dataset-v3/train_sequence/all_train_num_feats.npy\")\nxseq_cat = np.load(\"../input/amex-sequence-dataset-v3/train_sequence/all_train_cat_feats.npy\")\nxseq = np.clip(xseq, -3, 3)\n\nytrain = np.load(\"../input/amex-sequence-dataset-v3/train_sequence/all_targets.npy\")\n\n\n#Tabular dataset loading\nxtab = np.load(\"../input/amex-tabular-train-nn-dataset-v3/train_data/x_train.npy\")\nxtab_miss = np.load(\"../input/amex-tabular-train-nn-dataset-v3/train_data/x_miss_train.npy\")\nxtab_count = np.load(\"../input/amex-tabular-train-nn-dataset-v3/train_data/x_count.npy\")\nxtab = np.clip(xtab, -3.0, 3.0)\n\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:07:47.540232Z","iopub.execute_input":"2022-08-14T12:07:47.541226Z","iopub.status.idle":"2022-08-14T12:09:05.462614Z","shell.execute_reply.started":"2022-08-14T12:07:47.541194Z","shell.execute_reply":"2022-08-14T12:09:05.461514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(xtab.shape, xseq.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:09:05.463709Z","iopub.execute_input":"2022-08-14T12:09:05.464008Z","iopub.status.idle":"2022-08-14T12:09:05.469967Z","shell.execute_reply.started":"2022-08-14T12:09:05.463981Z","shell.execute_reply":"2022-08-14T12:09:05.468909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset","metadata":{}},{"cell_type":"code","source":"class AmexDataset(torch.utils.data.Dataset):\n    def __init__(self, xtab, xtab_count, xtab_miss, xseq, xseq_cat, y, idxs, phase='train'):\n        self.idxs=idxs\n        self.y = y\n        self.xtab=xtab\n        self.xtab_count=xtab_count\n        self.xtab_miss=xtab_miss\n        \n        self.xseq=xseq\n        self.xseq_cat=xseq_cat\n        \n        self.phase=phase\n    \n    def get_tab_features(self, idx):\n        x = torch.tensor(self.xtab[idx], dtype=torch.float32)\n        xmissing = 1+(x==-1).type(torch.long)\n        x_count = torch.tensor(self.xtab_count[idx]/13, dtype=torch.float32)\n        xmiss_count = torch.tensor(self.xtab_miss[idx]/13, dtype=torch.float32)\n        \n        return {\n            'x': x,\n            'xmissing': xmissing,\n            'xcount': x_count,\n            'xmiss_count': xmiss_count\n        }\n    \n    def get_seq_features(self, idx):\n        x = torch.tensor(self.xseq[idx], dtype=torch.float32)\n        xmissing = 1+(x==-1).type(torch.long)\n        xcat = torch.tensor(self.xseq_cat[idx], dtype=torch.long)\n        return {\n            'x': x,\n            'xmissing': xmissing,\n            'xcat': xcat\n        }\n    def __getitem__(self, idx):\n        idx = self.idxs[idx]\n        tabfeats = self.get_tab_features(idx)\n        seqfeats = self.get_seq_features(idx)\n        y = torch.tensor(self.y[idx], dtype=torch.float32)\n        \n        return tabfeats, seqfeats, y\n    \n    def __len__(self):\n        return len(self.idxs)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:09:05.471380Z","iopub.execute_input":"2022-08-14T12:09:05.471675Z","iopub.status.idle":"2022-08-14T12:09:05.483712Z","shell.execute_reply.started":"2022-08-14T12:09:05.471648Z","shell.execute_reply":"2022-08-14T12:09:05.482923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Tabular model","metadata":{}},{"cell_type":"code","source":"class TransformBlock(nn.Module):\n    def __init__(self, insize, outsize, dropout=0.1):\n        super().__init__()\n        self.bn = nn.BatchNorm1d(insize)\n        self.linear = nn.Linear(insize, outsize)\n        self.activation = nn.Softplus()\n        self.dropout = nn.Dropout(dropout)\n    \n    def forward(self, x):\n        x = self.bn(x)\n        x = self.linear(x)\n        x = self.activation(x)\n        x = self.dropout(x)\n        return x\n    \nclass TabularModel(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.pre_bn = nn.BatchNorm1d(1401)\n        self.missing_embedd = nn.Embedding(3, 5)\n        self.preprocess_layer = nn.Linear(6, 1)\n        self.dropout=nn.Dropout(0.01)\n        \n        self.layer1 = TransformBlock(1401, 1024, dropout = 0.5)\n        self.layer2 = TransformBlock(1024, 512, dropout = 0.5)\n        self.layer3 = TransformBlock(512, 256, dropout = 0.5)\n        \n        self.head = nn.Sequential(\n            nn.Linear(256, 1)\n        )\n        \n    def forward(self, tabfeats):\n        x=tabfeats['x']\n        xmissing=tabfeats['xmissing']\n        x_count=tabfeats['xcount']\n        xmiss_count=tabfeats['xmiss_count']\n        \n        xmissing = self.missing_embedd(xmissing)\n        x = x.unsqueeze(dim=-1)\n        x = torch.cat([x, xmissing], dim=-1)\n        \n        xmeta = torch.cat([x_count.unsqueeze(dim=-1), xmiss_count], dim=-1)\n        x = self.preprocess_layer(x).squeeze(dim=-1)\n        x = torch.cat([x, xmeta], dim=-1)\n        \n        x = self.dropout(x)\n        \n        x1 = self.layer1(x)\n        x2 = self.layer2(x1)\n        x3 = self.layer3(x2)\n        \n        return (x2, x3)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:09:05.484882Z","iopub.execute_input":"2022-08-14T12:09:05.485169Z","iopub.status.idle":"2022-08-14T12:09:05.498806Z","shell.execute_reply.started":"2022-08-14T12:09:05.485143Z","shell.execute_reply":"2022-08-14T12:09:05.498037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sequence Model","metadata":{}},{"cell_type":"code","source":"class ProjectionMLP(nn.Module):\n    def __init__(self, sz):\n        super().__init__()\n        self.head = nn.Sequential(\n            nn.Dropout(0.4),\n            nn.BatchNorm1d(sz),\n            nn.Linear(sz, sz//2),\n            nn.Softplus(),\n            \n            nn.Dropout(0.4),\n            nn.BatchNorm1d(sz//2),\n            nn.Linear(sz//2, sz//4),\n            nn.Softplus()\n        )\n        \n        self.out = nn.Sequential(\n            nn.Dropout(0.3),\n            nn.BatchNorm1d(sz//4),\n            nn.Linear(sz//4, 1)\n        )\n    def forward(self, x):\n        z = self.head(x)\n        return self.out(z)\n\nclass AmexGruModel(nn.Module):\n    def __init__(self, tabpath):\n        super().__init__()\n        DIM = 200\n        self.tabmodel = torch.load(tabpath, map_location=device)\n        \n        self.embeddings = nn.ModuleList([nn.Embedding(10, 4, padding_idx=0) for _ in range(13)])    \n        self.pre_ln = nn.LayerNorm(175)\n        self.gru = nn.GRU(175+52, DIM, bidirectional = True, batch_first = True)\n        self.activation = nn.Softplus()\n        \n        self.mlp = ProjectionMLP(2*DIM + 256)\n        self.mlp0 = ProjectionMLP(DIM+ 256)\n        self.mlp1 = ProjectionMLP(DIM+ 256)\n        \n        self.embedd_out =  nn.Sequential(\n            nn.Dropout(0.4),\n            nn.Linear(2*DIM, 1)\n        )\n        \n        self.embedd_out0 =  nn.Sequential(\n            nn.Dropout(0.4),\n            nn.Linear(DIM, 1)\n        )\n        \n        self.embedd_out1 =  nn.Sequential(\n            nn.Dropout(0.4),\n            nn.Linear(DIM, 1)\n        )\n    \n    def concat_categorical_embeddings(self, x, xcat):\n        xcat_embedds = []\n        for i in range(13):\n            xcat_embedds.append( self.embeddings[i](xcat[:, :, i]) )\n        xcat_embedds = torch.cat(xcat_embedds, dim=-1)\n        x = torch.cat([x, xcat_embedds], dim=-1)\n        return x\n    \n    def forward(self,tabfeats, seqfeats):\n        self.tabmodel.eval()\n        with torch.no_grad():\n            (ztab1, ztab2) = self.tabmodel(tabfeats)\n        \n        x = seqfeats['x']\n        xmissing= seqfeats['xmissing']\n        xcat = seqfeats['xcat']\n        x = self.pre_ln(x)\n        x = self.concat_categorical_embeddings(x, xcat)\n        \n        (x, h) =  self.gru(x)\n        h = self.activation(h)\n        \n        #Embeddings\n        zseq1 = torch.cat([h[0], ztab2], dim=-1)\n        zseq2 = torch.cat([h[1], ztab2], dim=-1)\n        zseq = torch.cat([h[0], h[1], ztab2], dim=-1)\n        \n        \n        y_embedd = self.embedd_out( torch.cat([h[0], h[1]], dim=-1 ) ).view(-1)\n        y_embedd0 = self.embedd_out0(h[0]).view(-1)\n        y_embedd1 = self.embedd_out1(h[1]).view(-1)\n        \n        y = self.mlp(zseq).view(-1)\n        y0 = self.mlp0(zseq1).view(-1)\n        y1 = self.mlp1(zseq2).view(-1)\n        \n        return {\n            'y_embedd': y_embedd,\n            'y_embedd0': y_embedd0,\n            'y_embedd1': y_embedd1,\n            \n            'y': y,\n            'y0': y0,\n            'y1': y1\n        }","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:54:50.277794Z","iopub.execute_input":"2022-08-14T12:54:50.278199Z","iopub.status.idle":"2022-08-14T12:54:50.300756Z","shell.execute_reply.started":"2022-08-14T12:54:50.278167Z","shell.execute_reply":"2022-08-14T12:54:50.299848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# metrics","metadata":{}},{"cell_type":"code","source":"def top_4percent(pred_df):\n    df = pred_df.copy()\n    df = df.sort_values('pred', ascending=False)\n    df['weight'] = df['target'].apply(lambda v: 20 if v==0 else 1)\n    four_percent_cutoff = 0.04 * sum(df['weight'])\n    df['weight_cumsum'] = df['weight'].cumsum()\n    df_cutoff = df[df.weight_cumsum <= four_percent_cutoff]\n    \n    return df_cutoff['target'].sum()/df['target'].sum()\n\ndef weighted_gini(pred_df):\n    df = pred_df.copy()\n    df = df.sort_values('pred', ascending=False)\n    df['weight'] = df['target'].apply(lambda v: 20 if v==0 else 1)\n    df['random'] = (df['weight'] / df['weight'].sum()).cumsum()\n    total_pos = (df['target'] * df['weight']).sum()\n    df['cum_pos_found'] = (df['target'] * df['weight']).cumsum()\n    df['lorentz'] = df['cum_pos_found'] / total_pos\n    df['gini'] = (df['lorentz'] - df['random']) * df['weight']\n    return df['gini'].sum()\n\n\ndef normalized_gini(df):\n    df_true=df[['target']].copy()\n    df_true['pred'] = df_true['target'].copy()\n    \n    G = weighted_gini(df)/weighted_gini(df_true)\n    return G","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:54:50.314165Z","iopub.execute_input":"2022-08-14T12:54:50.314920Z","iopub.status.idle":"2022-08-14T12:54:50.326161Z","shell.execute_reply.started":"2022-08-14T12:54:50.314882Z","shell.execute_reply":"2022-08-14T12:54:50.325155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# evaluate","metadata":{}},{"cell_type":"code","source":"def evaluate(foldnum, val_index, model, val_dataloader):\n    model.eval()\n    ytrue=[]\n    ypred=[]\n    \n    for (tabfeats, seqfeats, y) in val_dataloader:\n        for k,v in tabfeats.items():\n            tabfeats[k] = v.to(device)\n        for k,v in seqfeats.items():\n            seqfeats[k] = v.to(device)\n        y = y.to(device)\n        \n        with torch.no_grad():\n            outputs = model(tabfeats, seqfeats)\n            yhat = outputs['y']\n            yhat = yhat.sigmoid()\n            ytrue += y.cpu().tolist()\n            ypred += yhat.cpu().tolist()\n    \n    df = pd.DataFrame.from_dict({\n        'customer_ids': train_customer_ids[val_index],\n        'target': ytrue,\n        'pred': ypred\n    })\n    df['predlabel'] = (df['pred'] > 0.5).astype(int)\n    \n    \n    print(\"====================================================\")\n    ypred0 = (df[df.target==0].pred).mean()\n    ypred1 = (df[df.target==1].pred).mean()\n    \n    print(\"avg non-defaulter prob:{:.4f}\".format(ypred0))\n    print(\"avg defaulter prob:{:.4f}\".format(ypred1))\n    \n    print(\"f1_score:{:.4f}\".format(f1_score(df.target, df.predlabel)))\n    print(\"proportion of non defaulter >0.3: {:.4f}\".format(len(df[(df.target==0) & (df.pred>=0.3)])/len(df) ))\n    print(\"proportion of defaulter < 0.7: {:.4f}\".format(len(df[(df.target==1) & (df.pred <= 0.7)])/len(df) ))\n    print(\"====================================================\")\n    print()\n    \n    \n    \n    df.to_csv(\"eval_{}.csv\".format(foldnum))\n    G = normalized_gini(df[['target', 'pred']])\n    D = top_4percent(df[['target', 'pred']])\n    M = (G+D)/2\n    \n    return (G, D, M)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:54:50.347407Z","iopub.execute_input":"2022-08-14T12:54:50.347823Z","iopub.status.idle":"2022-08-14T12:54:50.361079Z","shell.execute_reply.started":"2022-08-14T12:54:50.347778Z","shell.execute_reply":"2022-08-14T12:54:50.360259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# train ops","metadata":{}},{"cell_type":"code","source":"def get_rank_loss(yhat, y):\n    loss = torch.tensor(0.0, device=device)\n    ypos = yhat[y==1]\n    yneg = yhat[y==0]\n    \n    if len(ypos) == 0 or len(yneg) == 0:\n        return loss\n    \n    yneg = yneg.repeat((len(ypos), 1))\n    ypos = ypos.unsqueeze(dim=-1)\n    loss1 = -torch.log( torch.sigmoid( ypos.detach()-yneg) ).mean()\n    loss2 = -torch.log( torch.sigmoid( ypos-yneg.detach()) ).mean()\n    loss = (loss1+loss2)/2\n    return loss\n\ndef get_hinge_loss(yhat, y):\n    yhat = torch.clamp(yhat, -3, 3)\n    yerr = y*(1 - yhat) + (1-y) * (1+yhat)\n    yerr = torch.clamp(yerr, 0, 3)\n    loss = torch.mean(yerr)\n    return loss\n\ndef get_klregularization(y, yhat):\n    loss = torch.tensor(0.0, device=device)\n    ypos = yhat[y==1]\n    yneg = yhat[y==0]\n    cnt=0\n    if len(ypos) > 0:\n        cnt+=1\n        loss = loss - 2*torch.log(1e-9+ypos.sigmoid().mean())\n    if len(yneg) > 0:\n        cnt+=1\n        loss = loss - torch.log(1e-9+1-yneg.sigmoid().mean())\n    loss = loss/max(1, cnt)\n    return loss\n\ndef get_losses(y, yhat, criterion):\n    binary_loss  = criterion(yhat, y)\n    rank_loss  = get_rank_loss(yhat, y)\n    hinge_loss = get_hinge_loss(yhat, y)\n    \n    loss = binary_loss + rank_loss+ hinge_loss\n    \n    return (loss, binary_loss, rank_loss, hinge_loss)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:54:50.385976Z","iopub.execute_input":"2022-08-14T12:54:50.387451Z","iopub.status.idle":"2022-08-14T12:54:50.400334Z","shell.execute_reply.started":"2022-08-14T12:54:50.387409Z","shell.execute_reply":"2022-08-14T12:54:50.399281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_ops(tabfeats, seqfeats, y, model, criterion, optimizer, scheduler):\n    y = y.to(device)\n    for k,v in tabfeats.items():\n        tabfeats[k] = v.to(device)\n\n    for k,v in seqfeats.items():\n        seqfeats[k] = v.to(device)\n    \n    model.train()\n    outputs = model(tabfeats, seqfeats)\n    \n    (loss, binary_loss, rank_loss, hinge_loss) = get_losses(y, outputs['y'], criterion)\n    (loss0, _, _, _) = get_losses(y, outputs['y0'], criterion)\n    (loss1, _, _, _) = get_losses(y, outputs['y1'], criterion)\n\n    (loss_embedd, _, _, _) = get_losses(y, outputs['y_embedd'], criterion)\n    (loss_embedd0, _, _, _) = get_losses(y, outputs['y_embedd0'], criterion)\n    (loss_embedd1, _, _, _) = get_losses(y, outputs['y_embedd1'], criterion)\n\n\n    loss = loss + 0.2*(loss0 + loss1 + loss_embedd + loss_embedd0 + loss_embedd1)\n    \n    optimizer.zero_grad(set_to_none=True)\n    loss.backward()\n    torch.nn.utils.clip_grad_norm_(model.parameters(), 5)\n    optimizer.step()\n    scheduler.step()\n    \n    return {\n        'loss':loss.item(),\n        'binary_loss': binary_loss.item(),\n        'hinge_loss': hinge_loss.item(),\n        'rank_loss': rank_loss.item(),\n        'loss_embedd': loss_embedd.item()\n    }","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:54:50.416357Z","iopub.execute_input":"2022-08-14T12:54:50.416939Z","iopub.status.idle":"2022-08-14T12:54:50.428164Z","shell.execute_reply.started":"2022-08-14T12:54:50.416904Z","shell.execute_reply":"2022-08-14T12:54:50.427144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# train model","metadata":{}},{"cell_type":"code","source":"def train_model(s, foldnum, val_index, train_dataloader, val_dataloader):\n    grupath = \"../input/amex-gru-mean-feats-models/models/model{}.pt\".format(foldnum)\n    tabpath = \"../input/amex-nn-ranking-1024-models/models/tabular_model_0_{}.pt\".format(foldnum)\n    \n    model = AmexGruModel(tabpath).to(device)\n    criterion = nn.BCEWithLogitsLoss()\n    mse_loss = nn.MSELoss()\n    \n    optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3, weight_decay=0.0)\n    scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, \n                                                           T_max = CFG.N_EPOCHS * len(train_dataloader), \n                                                           eta_min=1e-7)\n    \n    \n    best_eval=None\n    for e in range(CFG.N_EPOCHS):\n        model.train()\n        epoch_loss=[]\n        epoch_binary_loss=[]\n        epoch_rank_loss=[]\n        epoch_hinge_loss=[]\n        epoch_embedd_loss=[]\n        \n        \n        for it, (tabfeats, seqfeats, y) in enumerate(train_dataloader):\n            losses = train_ops(tabfeats, seqfeats, y, model, criterion, optimizer, scheduler)\n            epoch_loss.append(losses['loss'])\n            epoch_embedd_loss.append(losses['loss_embedd'])\n            epoch_binary_loss.append(losses['binary_loss'])\n            epoch_rank_loss.append(losses['rank_loss'])\n            epoch_hinge_loss.append(losses['hinge_loss'])\n            \n        \n        (G, D, M) = evaluate(foldnum, val_index, model, val_dataloader)\n        if best_eval is None or best_eval<M:\n            best_eval = M\n            torch.save(model, \"models/model{}_{}.pt\".format(s, foldnum))\n        \n        print(\"epoch:{} | loss:{:.4f}\".format(e, np.mean(epoch_loss)))\n        print(\"embedd loss:{:.4f}\".format(np.mean(epoch_hinge_loss)))\n        print(\"rank loss:{:.4f}\".format(np.mean(epoch_rank_loss)))\n        print(\"binary loss:{:.4f}\".format(np.mean(epoch_binary_loss)))\n        print(\"hinge loss:{:.4f}\".format(np.mean(epoch_hinge_loss)))\n        \n        print(\"current Eval:{:.4f} | best Eval:{:.4f}\".format(M, best_eval))\n        print(\"Gini:{:.4f} | Default Rate:{:4f}\".format(G, D))\n    \n    print()\n    print()\n    print()\n    print(\"Best Eval at the end of foldnumber:{} : {:.6f}\".format(foldnum, best_eval))","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:54:50.449339Z","iopub.execute_input":"2022-08-14T12:54:50.449739Z","iopub.status.idle":"2022-08-14T12:54:50.463514Z","shell.execute_reply.started":"2022-08-14T12:54:50.449703Z","shell.execute_reply":"2022-08-14T12:54:50.462425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.exists('models'):\n    os.mkdir(\"models\")","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:54:50.478637Z","iopub.execute_input":"2022-08-14T12:54:50.479044Z","iopub.status.idle":"2022-08-14T12:54:50.484167Z","shell.execute_reply.started":"2022-08-14T12:54:50.479009Z","shell.execute_reply":"2022-08-14T12:54:50.483269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skf = StratifiedKFold(n_splits=5, random_state=88471, shuffle=True)\n\nfor foldnum, (train_index, val_index) in enumerate(skf.split(ytrain, ytrain)):\n    train_dataset = AmexDataset(xtab, xtab_count, xtab_miss, xseq, xseq_cat, ytrain, train_index)\n    val_dataset = AmexDataset(xtab, xtab_count, xtab_miss, xseq, xseq_cat, ytrain, val_index)\n\n    train_dataloader = torch.utils.data.DataLoader(train_dataset, \n                                                   batch_size=CFG.BATCH_SIZE, \n                                                   shuffle=True,\n                                                   drop_last=True)\n\n    val_dataloader = torch.utils.data.DataLoader(val_dataset, batch_size=CFG.BATCH_SIZE, \n                                                   shuffle=False,\n                                                   drop_last=False)\n\n\n    print(\"Foldnumber:\", foldnum)\n    print(\"number of train iterations:\", len(train_dataloader))\n    print(\"number of val iterations:\", len(val_dataloader))\n\n    for s in range(1):\n        train_model(s, foldnum, val_index, train_dataloader, val_dataloader)\n        model = torch.load(\"models/model{}_{}.pt\".format(s, foldnum), map_location=device)\n        (G, D, M) = evaluate(foldnum, val_index, model, val_dataloader)\n        print(\"End of foldnumber:{} | Seed:{}\".format(foldnum, s))","metadata":{"execution":{"iopub.status.busy":"2022-08-14T12:54:50.521258Z","iopub.execute_input":"2022-08-14T12:54:50.521849Z","iopub.status.idle":"2022-08-14T13:01:30.102864Z","shell.execute_reply.started":"2022-08-14T12:54:50.521812Z","shell.execute_reply":"2022-08-14T13:01:30.100834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}