text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png
import pandas as pd

import pickle

import torch

import torch
from torch.utils.data import Dataset
from torchvision import datasets
from torchvision.transforms import ToTensor
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
from tqdm import tqdm
import pickle
device = 'cuda' if torch.cuda.is_available() else 'cpu'
import pandas as pd
import numpy as np

valid_ratio = 0.2

X = pickle.load(open('/kaggle/input/embedder/train_embedding', 'rb'))
y = pd.read_csv('/kaggle/input/embedder/preprocessed_train.csv').target_relabeled.values

valid_size = int(y.shape[0] * valid_ratio)

train_X_tensor = torch.tensor(X[:-valid_size]).to(device)
train_y_tensor = torch.LongTensor(y[:-valid_size]).to(device)
valid_X_tensor = torch.tensor(X[-valid_size:]).to(device)
valid_y_tensor = torch.LongTensor(y[-valid_size:]).to(device)

in_features = X.shape[1]

mlp = torch.nn.Sequential(torch.nn.modules.linear.Linear(in_features=in_features, out_features=in_features), torch.nn.ReLU(), torch.nn.Dropout(), torch.nn.modules.linear.Linear(in_features=in_features, out_features=in_features), torch.nn.ReLU(), torch.nn.Dropout(), torch.nn.modules.linear.Linear(in_features=in_features, out_features=2), torch.nn.Softmax()).to(device)

Loss = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(mlp.parameters(), lr=0.0001)
epoch = 400
max_patience = 5
patience = max_patience

train_loss_history = []
valid_loss_history = []
mlp.train()
for e in range(epoch):
    train_loss = 0
    valid_loss = 0
    outputs = mlp(train_X_tensor)
    loss = Loss(outputs, train_y_tensor)
    loss.backward()
    optimizer.step()
    optimizer.zero_grad()
    train_loss += loss.item()
    train_loss_history.append(train_loss)
    outputs = mlp(valid_X_tensor)
    loss = Loss(outputs, valid_y_tensor)
    valid_loss += loss.item()
    valid_loss_history.append(valid_loss)
    if len(valid_loss_history) > 1 and valid_loss_history[-1] > valid_loss_history[-2]:
        patience -= 1
        if patience < 0:
            break
    else:
        patience = max_patience

outputs = mlp(train_X_tensor)
outputs = torch.argmax(outputs, 1)
torch.mean((outputs == train_y_tensor).to(torch.float))
print('train_accuracy:    \t', end='')
print(torch.mean((outputs == train_y_tensor).to(torch.float)).item())
outputs = mlp(valid_X_tensor)
outputs = torch.argmax(outputs, 1)
print('valid_accuracy:    \t', end='')
print(torch.mean((outputs == valid_y_tensor).to(torch.float)).item())