# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/sky1ove/fastai/blob/master/MoA_generalDataset.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="QH6Ub4p2Y5U1"
#
#
# # Mount
#
#
#
#

# + colab={"base_uri": "https://localhost:8080/"} id="YRSuOcdGXqbH" outputId="1069e812-ba68-4edc-f826-4c2baabc394b"
from google.colab import drive
drive.mount('/content/drive')

# + colab={"base_uri": "https://localhost:8080/"} id="JEDEpNd6IdTU" outputId="a432739c-607f-4a39-a613-72c8946bb4e1"
# cd drive/MyDrive/organize

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="R1dV8Bdt7hvA" outputId="1ceeee37-08d8-4679-b15e-58ecefb2abf1"
 # !pip install -U fastai #-U means updates

# + id="n-nQBtTtJW8z"
import torch
import torch.nn as nn

import pandas as pd
from fastai.data.core import DataLoaders
from fastai.vision.all import * 

# + [markdown] id="dsuTQ_WuZF9u"
# # Data: Dataset & Dataloader

# + id="utyhrPIaJeQj"
#load data
train_features = pd.read_csv('train_features.csv')
train_targets_scored = pd.read_csv('train_targets_scored.csv')
test_features = pd.read_csv('test_features.csv')

# + id="HYJZsGbWJkc7"
#merge x, y to the same df
train = train_features.merge(train_targets_scored, on='sig_id')

# + id="46fB7VT_Jkwr"
#preprocessing
train = train[train['cp_type']!='ctl_vehicle'].reset_index(drop=True)
test = test_features[test_features['cp_type']!='ctl_vehicle'].reset_index(drop=True)

# + id="x3qjUcprJnl7"
#feature & target columns
feature_cols = train_features.columns[4:].tolist()
target_cols = train_targets_scored.columns[1:].tolist()


# + id="gG6ugVjXJtqD"
class GeneralDataset:
    def __init__(self, df, feature_cols, target_cols):
        self.x = df[feature_cols].values 

        self.y = df[target_cols].values
        self.len = df.shape[0]

    def __len__(self):
        return self.len

    def __getitem__(self, index):
        return torch.Tensor(self.x[index]),torch.Tensor(self.y[index]) 


# + id="hdI5yA_TJ2XT"
#split
cut = int(0.8 * len(train))
train_files = train.iloc[:cut]
valid_files = train.iloc[cut:]

# + id="QWo9R1mXKZjz"
train_ds = GeneralDataset(train_files, feature_cols, target_cols)
valid_ds = GeneralDataset(valid_files, feature_cols, target_cols)

# + id="duPnMr9pKbaK"
dls = DataLoaders.from_dsets(train_ds, valid_ds, num_workers=4)

# + id="rzSlZbUwKdHK"
#check if dls work
x,y = dls.one_batch()

# + colab={"base_uri": "https://localhost:8080/"} id="ODB-uBxi8b4d" outputId="ae0ffa83-bcf7-4201-863c-50cc828d53f7"
y

# + [markdown] id="WUPEvbCQZTdO"
# # Loss function

# + id="LKVZje2vKheM"
loss = nn.BCEWithLogitsLoss()

# + [markdown] id="XBlIUoVCZWil"
# # Model

# + id="AYIbHleHKjsa"
model = nn.Sequential(nn.Linear(len(feature_cols), 512),
                          nn.BatchNorm1d(512),
                          nn.Dropout(0.2),
                          nn.PReLU(),
                          nn.Linear(512, 218),
                          nn.BatchNorm1d(218),
                          nn.Dropout(0.2),
                          nn.PReLU(),
                          nn.Linear(218, len(target_cols)),)

# + [markdown] id="vAPvq4QTZgH9"
# # Learner and train

# + id="amDdGVs2KlPK"
learn = Learner(dls.cuda(), model.cuda(), loss)

# + colab={"base_uri": "https://localhost:8080/", "height": 544} id="PYiHmiOJKopS" outputId="fb9a5f12-4043-41bb-d601-f1b953290d52"
learn.summary()

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="4XJ5BabAKqty" outputId="6720acc2-8371-44a6-b94a-e79ec3a4709f"
lr = learn.lr_find()

# + colab={"base_uri": "https://localhost:8080/"} id="9X1c9_5nIHCt" outputId="d968be83-7a7c-4ec2-aa3a-31269626b488"
lr

# + colab={"base_uri": "https://localhost:8080/", "height": 173} id="sTBMp29mKsza" outputId="bfbba78c-80d6-4cca-c317-6058af22ccb4"
learn.fit_one_cycle(4, 1e-2)

# + [markdown] id="T5iGhNqkHR_2"
# # Save model

# + colab={"base_uri": "https://localhost:8080/"} id="XwYPmw23Hr5O" outputId="1a2a02ef-5c8e-43fe-fa56-9726bff05ff9"
learn.save('temp')


# + [markdown] id="pRVpm6o5I4br"
# # Prediction (use pytorch method)

# + [markdown] id="NKE75D9NI8cY"
# Suppose we start a new notebook, and we only have information for test set and the model

# + id="gfMNvU7YE66b"
class TestDataset:
    def __init__(self, df, feature_cols):
        self.x = df[feature_cols].values 

        #self.y = df[target_cols].values
        self.len = df.shape[0]

    def __len__(self):
        return self.len

    def __getitem__(self, index):
        return torch.Tensor(self.x[index])


# + id="jMBxi2o5FGNk"
test_dset = TestDataset(test,feature_cols)
test_dl = torch.utils.data.DataLoader(test_dset)

# + id="_chjblr3IZg2"
test_dl = torch.utils.data.DataLoader(test_dset)

# + id="V8GPRt3MIW3O"
learn = Learner(None, model, loss_func=1)

# + colab={"base_uri": "https://localhost:8080/"} id="taANbQfmJKAI" outputId="e42c3f63-786a-4295-89e0-a0e72e99a67d"
learn.load('temp')

# + colab={"base_uri": "https://localhost:8080/"} id="4T3WbN8LJTms" outputId="aa4de50a-dfd8-4e05-8df7-79fc079e7169"
learn.model.eval() # this step is necessary, as it deletes those dropout during prediction

# + id="JZUpebadKajd"
preds = []

for data in test_dl:
    inputs = data.cuda()
    outputs = learn.model(inputs) #learn.model(x).sigmoid().detach().cpu().numpy()
    
    preds.append(outputs.sigmoid().detach().cpu().numpy())
    
preds = np.concatenate(preds)

# + id="K_XuEWu4Mw0K"
sample_submission = pd.read_csv('sample_submission.csv')

# + id="4XTf614mNjMY"
test[target_cols] = preds


# + [markdown] id="j8zO-b5YV4bq"
# # Alternative Prediction(use get_preds)

# + id="463zMEgmV7hA"
class TestDataset:
    def __init__(self, df, feature_cols):
        self.x = df[feature_cols].values 

        #self.y = df[target_cols].values
        self.len = df.shape[0]

    def __len__(self):
        return self.len

    def __getitem__(self, index):
        return torch.Tensor(self.x[index]), torch.Tensor(206)


# + id="<KEY>"
dset2 = TestDataset(test,feature_cols)
dl2 = DataLoader(dset2,bs = 64, device='cuda')

# + colab={"base_uri": "https://localhost:8080/"} id="rh7UlvmSRYCE" outputId="ff7221a9-a3c2-465f-ea69-383cc6789b94"
for batch in dl3:
  a = batch
  break

a

# + id="BHbSY4icUvl8"
learn = Learner(None, model, loss_func=loss)

# + colab={"base_uri": "https://localhost:8080/", "height": 17} id="h2c9J4LbIeEh" outputId="f37f7de5-a752-4bb9-d74c-b2fb89197f68"
preds2,_ = learn.get_preds(dl=dl3,act = torch.sigmoid)

# + colab={"base_uri": "https://localhost:8080/"} id="SMmgtEuKVlFr" outputId="1162f750-97af-47f6-adf7-71e358ebe811"
preds2

# + colab={"base_uri": "https://localhost:8080/"} id="yjs1Tx-CVaBK" outputId="9b2ff598-b3a6-4686-97c0-3b16f04d3ff2"
preds

# + [markdown] id="EyTlqp-nLRdU"
# # Additional function

# + colab={"base_uri": "https://localhost:8080/", "height": 34} id="An4dBk9r_WBc" outputId="e50dc9b0-a21e-4f8a-862d-84d8f7f0116e"
learn.validate(dl=dls.valid) #calculate validation loss

# + colab={"base_uri": "https://localhost:8080/"} id="GYVBRkMnQ_SH" outputId="e0b4047f-9450-44f1-cc59-1292eb4ca830"
for batch in dls.valid:
  a = batch
  break

a


# + id="vNMQyuXQPDCP"
class TestDataset:
    def __init__(self, df, feature_cols):
        self.x = df[feature_cols].values 

        #self.y = df[target_cols].values
        self.len = df.shape[0]

    def __len__(self):
        return self.len

    def __getitem__(self, index):
        return torch.Tensor(self.x[index]), torch.Tensor(206)


# + id="4YuVNegvPvjS"
dl3 = DataLoader(dset2,bs = 64, device='cuda')

# + id="wW06xTb3PK7_"
dset2 = TestDataset(test,feature_cols)
dl2 = torch.utils.data.DataLoader(test_dset)

# + id="BytISPtEGV3a"
x = dls2.one_batch()

# + colab={"base_uri": "https://localhost:8080/"} id="T5Nrz30DGcG5" outputId="13501c4d-4675-43d7-c573-4c85994d4bfa"
learn.model(x)

# + colab={"base_uri": "https://localhost:8080/"} id="mZe8C0HwFWCo" outputId="bb752103-f1ba-4159-d143-0adeea636ddb"
learn.model.eval()

# + colab={"base_uri": "https://localhost:8080/"} id="pTiH1-C6Fzw7" outputId="1a71c92f-7bcb-4a6b-ee44-73162e7ed891"
dls2.cuda()

# + colab={"base_uri": "https://localhost:8080/"} id="YPt7nuvtF4oK" outputId="e7020c39-8f07-4af7-cfd5-047ef49772bd"
dls2.one_batch()

StarcoderdataJupyterScriptsDedupFiltered