Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<train_model>
import os
import random
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import Adam
from torch.utils.data import DataLoader
from torchvision import transforms
import numpy as np
import matplotlib.pyplot as plt
from torch.nn import CrossEntropyLoss
from tqdm import tqdm
from collections import Counter
history=model.fit(X_train,y_train,batch_size=4,epochs=5,validation_data=(X_test,y_test),verbose=2 )<compute_train_metric>
root = "/kaggle/input/digit-recognizer"
train_data = np.loadtxt(os.path.join(root,"train.csv"),delimiter=",",skiprows=1)
 test_data = np.loadtxt(os.path.join(root,"test.csv"),delimiter=",",skiprows=1 )
y_pre=model.predict(X_test)
 y_pre=np.round(y_pre ).astype(int ).reshape(1142)
 print(roc_auc_score(y_pre,y_test))<compute_test_metric>
!nvidia-smi
scores_model.append({'Model': 'Bidirectional-LSTM','AUC_Score': roc_auc_score(y_pre,y_test)} )<load_from_url>
class Dataset:

 def __init__(self,data,targets,transform=None):
self.data = data
self.targets = targets
self.transform = transform
def __len__(self):
return len(self.data)
 def __getitem__(self,idx):
if self.transform == None:
return self.data[idx],self.targets[idx]
else:
return self.transform(self.data[idx]),self.targets[idx]
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<import_modules>
transform = transforms.Compose([transforms.ToPILImage() ,
transforms.RandomResizedCrop(size=28,scale=(0.9,1.0),ratio=(0.9,1,15)) ,
transforms.RandomAffine(degrees=12,translate=(1/7,1/7),shear=12),
transforms.RandomRotation(degrees=12),
transforms.ToTensor() ])
 x_train = train_data[:,1:].reshape(-1,28,28 ).astype(np.uint8)
 y_train = torch.LongTensor(train_data[:,0])
 train_dataset = Dataset(x_train,y_train,transform )
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import ModelCheckpoint
import tensorflow_hub as hub
import tokenization<categorify>
class CNN(nn.Module):
def __init__(self):
super(CNN,self ).__init__()
self.layer1 = self.get_conv_block(1,64)
 self.layer2 = self.get_conv_block(64,128,paddings=(0,1))
self.layer3 = self.get_conv_block(128,256)
 self.fc1 = nn.Linear(256*3*3,2048)
 self.fc2 = nn.Linear(2048,512)
 self.fc3 = nn.Linear(512,10)
 def forward(self,x):
x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x ).flatten(start_dim=1)
 x = F.dropout(F.relu(self.fc1(x)) ,0.7,training=self.training)
 out = self.fc3(F.relu(self.fc2(x)))
 return out
def get_conv_block(self,in_chan,out_chan,strides=(1,1),paddings=(1,1)) :
return nn.Sequential(
 nn.Conv2d(in_chan,out_chan,3,strides[0],paddings[0]),
nn.ReLU() ,
nn.BatchNorm2d(out_chan),
nn.Conv2d(out_chan,out_chan,3,strides[0],paddings[1]),
nn.ReLU() ,
nn.BatchNorm2d(out_chan),
nn.MaxPool2d(2)
 )
def bert_encode(texts, tokenizer, max_len=512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<choose_model_class>
device = torch.device("cuda")if torch.cuda.is_available() else torch.device("cpu")
 random.seed(1234)
 np.random.seed(1234)
 torch.random.manual_seed(1234)
 epochs = 80
batch_size = 512
trainloader = DataLoader(train_dataset,batch_size,shuffle=True,pin_memory=True)
 def train(dataloader,net,optimizer,loss_fn,epochs=50):
for n in range(epochs):
with tqdm(dataloader,desc=f"{n+1}/{epochs} epochs")as t:
running_loss = 0.0
running_correct = 0
running_total = 0
for x,y in t:
out = net(x.to(device))
pred = out.max(dim=1)[1]
loss = loss_fn(out,y.to(device))
opt.zero_grad()
loss.backward()
opt.step()
running_loss += loss.item() *x.size(0)
 running_correct +=(pred==y.to(device)).sum().item()
running_total += x.size(0)
 t.set_postfix({"train_loss":running_loss/running_total,"train_acc":running_correct/running_total})
 models = []
for i in range(5):
print("Training {} model".format(str(i+1)+["st","nd","rd","th","th"][i]))
cnn = CNN()
cnn.to(device)
 opt = Adam(cnn.parameters() ,lr=1e-4)
 loss_fn = CrossEntropyLoss()
train(trainloader,cnn,opt,loss_fn,epochs)
 models.append(cnn )
def build_model(bert_layer, max_len=512):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 clf_output = sequence_output[:, 0, :]
out = Dense(1, activation='sigmoid' )(clf_output)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(Adam(lr=1e-5), loss='binary_crossentropy', metrics=['accuracy'])
 return model<choose_model_class>
cnn.eval()
train_dataset.transform = transforms.ToTensor()
trainloader = DataLoader(train_dataset,batch_size=512,shuffle=False,pin_memory=True)
 train_preds = []
for cnn in models:
train_pred = []
with torch.no_grad() :
for x,_ in trainloader:
out = cnn(x.to(device))
pred = out.max(dim=1)[1]
train_pred.append(pred.detach().cpu().numpy())
 train_preds.append(np.concatenate(train_pred))
train_preds = list(zip(*train_preds))
train_pred = np.array(list(map(lambda x: Counter(x ).most_common(1)[0][0],train_preds)))
 train_acc =(train_pred == y_train.numpy() ).astype("float" ).mean()
print("The training accurary is {}".format(train_acc))
%%time
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )<load_from_csv>
x_test = test_data.reshape(-1,1,28,28)
 x_test = torch.Tensor(x_test)/255.
 test_preds = []
for cnn in models:
test_pred = []
with torch.no_grad() :
for i in range(0,len(x_test),batch_size):
out = cnn(x_test[i:i+batch_size].to(device))
pred = out.max(dim=1)[1]
test_pred.append(pred.detach().cpu().numpy())
 test_preds.append(np.concatenate(test_pred))
test_preds = list(zip(*test_preds))
test_pred = list(map(lambda x: Counter(x ).most_common(1)[0][0],test_preds))
train = pd.read_csv(".. /input/nlp-getting-started/train.csv")
 test = pd.read_csv(".. /input/nlp-getting-started/test.csv")
 submission = pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv")
<feature_engineering>
imageid = pd.Series(np.arange(len(test_pred)))+1
df = pd.DataFrame({"ImageId":imageid,"Label":test_pred})
 df.set_index("ImageId")
 df.to_csv("/kaggle/working/test_pred.csv",index=False )