<jupyter_start><jupyter_text>Contest [Link](https://hack.univ.ai/)<jupyter_code>import os

BASE_DIR = '/content/drive/MyDrive/Hackerearth/univ_ai'
os.chdir(BASE_DIR)
import warnings
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>Define Model<jupyter_code>import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from torch.optim import Adam
from torchvision import transforms, utils
RANDOM_SEED = 22
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# dimention of each data point in input
n_feats = 11

class Classifier(nn.Module):
    def __init__(self, inp_dim=n_feats):
        super(Classifier, self).__init__()
        self.inp_dim = inp_dim
        self.l1 = nn.Linear(inp_dim, 10)
        self.bn1 = nn.BatchNorm1d(10)
        self.l2 = nn.Linear(10, 5)
        self.bn2 = nn.BatchNorm1d(5)
        self.l3 = nn.Linear(5, 2)

    def forward(self, x):
        x = F.relu(self.bn1(self.l1(x)))
        x = F.relu(self.bn2(self.l2(x)))
        x = self.l3(x)

        return x<jupyter_output><empty_output><jupyter_text>motivation for chossing criterian for this problem [1](https://stats.stackexchange.com/questions/485551/1-neuron-bce-loss-vs-2-neurons-ce-loss),  &nbsp; BCE vs CrossEntropy<jupyter_code>model = Classifier().to(device)

optimizer = Adam(model.parameters())
criterian = nn.CrossEntropyLoss()<jupyter_output><empty_output><jupyter_text>Create DataLoader and Dataset<jupyter_code>class OurDataset(Dataset):
    def __init__(self, df):
        super(Dataset, self).__init__()
        self.df = df
    
    def __len__(self):
        return len(self.df)

    def __getitem__(self, idx):
        # return self.df.iloc[idx].to_list()
        feats = self.df[['income', 'age', 'experience', 'married', 'house_ownership',
                         'car_ownership', 'profession', 'city', 'state', 'current_job_years', 'current_house_years']].iloc[idx].to_list()
        label = -1 if 'risk_flag' not in self.df.columns else self.df[['risk_flag']].iloc[idx].item()
        id = -1 if 'id' not in self.df.columns else self.df[['id']].iloc[idx].item()
        return {'feats': torch.tensor(feats), 'label': label, 'id': id}<jupyter_output><empty_output><jupyter_text>Load the data<jupyter_code>import pandas as pd
import numpy as np
b_size = 486
train_df = pd.read_csv(BASE_DIR + '/processed_data/train.csv') #.head(7000) 
train_df = train_df.groupby('risk_flag', group_keys=False).apply(lambda x: x.sample(min(len(x), 24000)))

train_dataset = OurDataset(train_df)
train_dataloader = DataLoader(train_dataset, batch_size=b_size*10, shuffle=True)
train_df.groupby('risk_flag').count()
for x in [train_dataset, train_dataloader]:
    y = next(iter(x))
    print(type(y['label']))
val_df = pd.read_csv(BASE_DIR + '/processed_data/val.csv').head(7000)

val_dataset = OurDataset(val_df)
val_dataloader = DataLoader(val_dataset, batch_size=b_size, shuffle=True)
len(train_dataloader.dataset), len(train_dataloader) * b_size, len(train_dataset)<jupyter_output><empty_output><jupyter_text>Training Loop<jupyter_code>from tqdm.autonotebook import tqdm

def train(n_epoch, train_dataloader, val_dataloader, model, criterian, optimizer):
    _benchmark = [{
        'train': [], 'val': []
    }, {
        'train': [], 'val': []
    }]
    for epoch in range(n_epoch):
        print('='*20, f' [Epoch {epoch+1}] ', '='*20)
        for phase, loader in [('train', train_dataloader), ('val', val_dataloader)]:
            running_loss, running_acc = 0.0, 0.0
            if phase == 'train':
                model.train()
            else:
                model.eval()

            for batch in tqdm(loader, leave=False, position=0):
                optimizer.zero_grad()

                inputs, labels = batch['feats'], batch['label'].to(device)
                outs = model(inputs.to(device))
                loss = criterian(outs, labels.to(device))

                if phase == 'train':
                    loss.backward()
                    optimizer.step()
                
                running_loss += loss.item() * inputs.shape[0]
                running_acc += torch.sum(torch.argmax(outs, dim=1) == labels)

            running_loss /= len(loader.dataset)
            running_acc /= len(loader.dataset)
            _benchmark[0][phase].append(running_loss)
            _benchmark[1][phase].append(running_acc)

            print(f'{phase}: Loss {running_loss:.2f}, Acc: {running_acc:.2f}')

    return _benchmark

_losses, _accs = train(10, train_dataloader, val_dataloader, model, criterian, optimizer)
_benchmarks = [_losses, _accs]
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter

fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8,4), gridspec_kw={'hspace': 0.2, 'wspace': 0.9})

axes[0].plot(_losses['train'], color='red', label='train loss')
axes[0].plot(_losses['val'], color='blue', label='val loss')
axes[0].legend(loc="upper right")
axes[0].set_ylabel('loss')
axes[0].yaxis.set_major_formatter(FormatStrFormatter('%.2f'))

axes[1].plot(_accs['train'], color='red', label='train acc')
axes[1].plot(_accs['val'], color='blue', label='val acc')
axes[1].legend(loc="upper right")
axes[1].set_ylabel('Acc')
axes[1].yaxis.set_major_formatter(FormatStrFormatter('%.2f'))

plt.xlabel('epochs')
plt.show()<jupyter_output><empty_output><jupyter_text>saving and loading models [1](https://pytorch.org/tutorials/beginner/saving_loading_models.htmlid1)<jupyter_code># torch.save({
#     'model_state_dict': model.state_dict(),
#     'benchmarks': _benchmarks,
#     'n_epochs': 3,
#     'details': 'Trained on full dataset.'
# }, BASE_DIR + '/checkpoints/Apr4.h5')
! ls checkpoints<jupyter_output>Apr2.h5  Apr3.h5<jupyter_text>Loading checkpoints<jupyter_code>checkpoint = torch.load(BASE_DIR + '/checkpoints/Apr3.h5')
model.load_state_dict(checkpoint['model_state_dict'])<jupyter_output><empty_output><jupyter_text>Evaluate on test split<jupyter_code>test_df = pd.read_csv(BASE_DIR + '/processed_data/val.csv')

test_dataset = OurDataset(test_df)
test_dataloader = DataLoader(test_dataset, b_size * 3, shuffle=True)
from sklearn.metrics import roc_auc_score, plot_confusion_matrix, ConfusionMatrixDisplay, confusion_matrix

running_loss, running_acc = 0.0, 0.0
model.eval()

_labels, _preds = [], []

for batch in tqdm(test_dataloader, leave=False, position=0):
    inputs, labels = batch['feats'], batch['label'].to(device)

    outs = model(inputs.to(device))
    loss = criterian(outs, labels.to(device))

    running_loss += loss.item() * inputs.shape[0]
    preds = torch.argmax(outs, dim=1)
    running_acc += torch.sum(preds == labels)

    _labels.extend(labels.tolist())
    _preds.extend(preds.tolist())

running_loss /= len(test_dataloader.dataset)
running_acc /= len(test_dataloader.dataset)

print(f'Acc: {running_acc:.2f}, Loss: {running_loss:.2f}, roc_auc_score: {roc_auc_score(y_score=_preds, y_true= _labels)}')
cm = confusion_matrix(_labels, _preds)
cmd = ConfusionMatrixDisplay(cm, display_labels=['0', '1'], )
cmd.plot(values_format='d')<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>pred_df = pd.read_csv(BASE_DIR + '/processed_data/predict.csv')

pred_dataset = OurDataset(pred_df)
pred_dataloader = DataLoader(pred_dataset, b_size * 5)
predictions = {
    'id': [], 'risk_flag': []
}

for batch in tqdm(pred_dataloader, leave=False, position=0):
    inputs, labels, ids = batch['feats'], batch['label'].to(device), batch['id'].tolist()

    outs = model(inputs.to(device))
    outs = torch.argmax(outs, dim=1).tolist()
    # print(labels, ids, outs, sep='\n', end='\n\n\n')
    # print(labels.shape, inputs.shape)
    # break
    predictions['id'].extend(ids)
    predictions['risk_flag'].extend(outs)


preds = pd.DataFrame(predictions)
preds.head()
preds.to_csv(BASE_DIR + '/processed_data/output.csv', index=False)
preds.groupby('risk_flag').count()<jupyter_output><empty_output><jupyter_text>Rough<jupyter_code>%cd processed_data/
! wget https://lead-files.nyc3.cdn.digitaloceanspaces.com/data/hackathon-1/Sample%20Prediction%20Dataset.csv
pd.read_csv('predict.csv').head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15352697095435686, 'fraction_numerical': 0.015255064681474249, 'mean_word_length': 4.398550724637682, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '325128', 'license': 'MIT', 'n_tokens_mistral': 3063, 'n_tokens_neox': 2950, 'n_words': 489, 'path': 'submissions/Univ.ai [Credit Risk Prediction]/univ_ai_deep_learning_approach.ipynb', 'repo_name': 'harshraj22/DeepLearnin', 'size': '8194'}