Kaggle competition: Abstraction and Reasoning Challenge
<SOS> metric: MeanBestErrorAtK Kaggle data source: abstraction-and-reasoning-challenge<load_pretrained>
import torch
from torch.utils.data import Dataset
import numpy as np
import json
from glob import glob
import os
import random
import pickle
import torch.nn.functional as F
import matplotlib.pyplot as plt
from matplotlib import colors
import pandas as pd
import torch.nn as nn
import math
preds_start = np.zeros(( input_ids_t.shape[0],MAX_LEN))
preds_end = np.zeros(( input_ids_t.shape[0],MAX_LEN))
DISPLAY=1
for i in range(5):
print('
print('
print('
K.clear_session()
model = build_model()
model.load_weights('.. /input/model8/v8-roberta-%i.h5'%i)
 print('Predicting Test...')
 preds = model.predict([input_ids_t,attention_mask_t,token_type_ids_t],verbose=DISPLAY)
 preds_start += preds[0]/n_splits
preds_end += preds[1]/n_splits<string_transform>
seed = 42
print(f'setting everything to seed {seed}')
 random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
cmap = colors.ListedColormap(
 ['
'
norm = colors.Normalize(vmin=0, vmax=10 )
all = []
for k in range(input_ids_t.shape[0]):
a = np.argmax(preds_start[k,])
 b = np.argmax(preds_end[k,])
 if a>b:
st = test_df.loc[k,'text']
else:
text1 = " "+" ".join(test_df.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 st = tokenizer.decode(enc.ids[a-1:b])
 st1=st.strip()
if st1=='****' or st1=='****!' or st1=='****!' or st1=='****!' or st1=='****,' or st1=='****,' or st1=='****.' or st1=='****.':
st=text1
elif st1=='(good':
st='good'
elif st1=='__joy':
st='joy'
all.append(st )<feature_engineering>
class ARCTest(Dataset):
def __init__(self, root, imgsz=30):

 super(ARCTest, self ).__init__()
self.out_rows, self.out_cols = imgsz, imgsz
task_paths = sorted(glob(f'{root}/test/*.json'))
self.support_x_batch, self.support_y_batch, self.query_x_batch, self.task_paths = self.create_batch(task_paths)
 def pad_im(self, task, out_rows, out_cols, cval=10):
ip = []
op = []
for mode in ['train']:
num_pairs = len(task[mode])
 input_im = np.zeros(( num_pairs, 1, out_rows, out_cols))
output_im = np.zeros(
(num_pairs, 1, out_rows, out_cols), dtype=np.long)
 for task_num in range(num_pairs):
im = np.array(task[mode][task_num]['input'])
 nrows, ncols = im.shape
if(nrows > out_rows)or(ncols > out_cols):
return 0, 0, 1, 0
im = np.pad(im,(( out_rows-nrows, 0),(out_cols-ncols, 0)) , mode='constant',
constant_values=(cval, cval))
input_im[task_num, 0] = im
im = np.array(task[mode][task_num]['output'])
 nrows, ncols = im.shape
if(nrows > out_rows)or(ncols > out_cols):
return 0, 0, 1, 0
im = np.pad(im,(( out_rows-nrows, 0),(out_cols-ncols, 0)) , mode='constant',
constant_values=(cval, cval))
output_im[task_num, 0] = im
ip.extend(input_im)
 op.extend(output_im)
 test_ip = []
num_pairs = len(task['test'])
 input_im = np.zeros(( num_pairs, 1, out_rows, out_cols))
for task_num in range(num_pairs):
im = np.array(task['test'][task_num]['input'])
 nrows, ncols = im.shape
if(nrows > out_rows)or(ncols > out_cols):
return 0, 0, 1, 0
im = np.pad(im,(( out_rows-nrows, 0),(out_cols-ncols, 0)) , mode='constant',
constant_values=(cval, cval))
input_im[task_num, 0] = im
test_ip.extend(input_im)
 return np.vstack(ip), np.vstack(op), 0, np.vstack(test_ip)
 def create_batch(self, task_paths):

 x_batch, y_batch,query_x_batch = [], [], []
all_task_paths = []
for task_file in task_paths:
with open(task_file, 'r')as f:
task = json.load(f)
 input_im, output_im, not_valid, query_im = self.pad_im(task, self.out_rows,
self.out_cols)
 if not_valid:
continue
x_batch.extend(input_im[None])
 y_batch.extend(output_im[None])
 query_x_batch.extend(query_im[None])
 all_task_paths.append(task_file)
 return x_batch, y_batch, query_x_batch, all_task_paths
def __getitem__(self, index):

 support_x = torch.tensor(
 self.support_x_batch[index], dtype=torch.float32)
 support_y = torch.tensor(
 self.support_y_batch[index], dtype=torch.long)
 return support_x[:, None], support_y.reshape(-1), self.task_paths[index]
def __len__(self):
return len(self.support_x_batch)
<save_to_csv>
class PositionalEncoding(nn.Module):
def __init__(self, d_model, dropout=0.1, max_len=5000):
super(PositionalEncoding, self ).__init__()
self.dropout = nn.Dropout(p=dropout)
 pe = torch.zeros(max_len, d_model)
 position = torch.arange(0, max_len, dtype=torch.float ).unsqueeze(1)
 div_term = torch.exp(torch.arange(
 0, d_model, 2 ).float() *(-math.log(10000.0)/ d_model))
pe[:, 0::2] = torch.sin(position * div_term)
 pe[:, 1::2] = torch.cos(position * div_term)
 pe = pe.unsqueeze(0 ).transpose(0, 1)
 self.register_buffer('pe', pe)
 def forward(self, x):
x = x + self.pe[:x.size(0), :]
return self.dropout(x)
 class TransformerModel(nn.Module):
def __init__(self, ntoken, ninp, nhead, nhid, nlayers, dropout=0.5):
super(TransformerModel, self ).__init__()
self.model_type = 'Transformer'
self.src_mask = None
self.pos_encoder = PositionalEncoding(ninp, dropout)
 encoder_layers = TransformerEncoderLayer(ninp, nhead, nhid, dropout)
 self.transformer_encoder = TransformerEncoder(encoder_layers, nlayers)
 self.encoder = nn.Embedding(ntoken, ninp)
 self.ninp = ninp
self.decoder = nn.Linear(ninp, ntoken)
 def _generate_square_subsequent_mask(self, sz):
mask =(torch.triu(torch.ones(sz, sz)) == 1 ).transpose(0, 1)
 mask = mask.float().masked_fill(mask == 0, float(
 '-inf')).masked_fill(mask == 1, float(0.0))
return mask
def init_weights(self):
initrange = 0.1
self.encoder.weight.data.uniform_(-initrange, initrange)
 self.decoder.bias.data.zero_()
self.decoder.weight.data.uniform_(-initrange, initrange)
 def forward(self, src):
if self.src_mask is None or self.src_mask.size(0)!= len(src):
device = src.device
mask = self._generate_square_subsequent_mask(len(src)).to(device)
 self.src_mask = mask
src = self.encoder(src)* math.sqrt(self.ninp)
 src = self.pos_encoder(src)
 output = self.transformer_encoder(src, self.src_mask)
 output = self.decoder(output)
 return output
test_df['selected_text'] = all
test_df[['textID','selected_text']].to_csv('submission.csv',index=False)
<import_modules>
innerstepsize = 1e-2
innerepochs = 1000
device = torch.device('cpu')
 arc_dataset = ARCTest(
 root='/kaggle/input/abstraction-and-reasoning-challenge/', imgsz=15)
 all_train_acc = []
imgsz, num_class = 15, 11
for step,(( x, y, task_path), q)in enumerate(zip(arc_dataset, arc_dataset.query_x_batch)) :
task_id = task_path.split('/')[-1]
state = torch.load('.. /input/arctransformermodel/epoch_1_step_264_acc_0.688.pth', map_location='cpu')
 model.load_state_dict(state)
 optimizer = torch.optim.AdamW(model.parameters() , lr=innerstepsize)
 x, y = x.to(device), y.to(device)
 x = x.to(device ).reshape(-1, imgsz*imgsz ).long()
train_losses = []
train_acc = []
model.train()
for _ in range(innerepochs):
optimizer.zero_grad()
outputs = model(x ).reshape(-1, num_class)
 loss = F.cross_entropy(outputs, y)
 loss.backward()
optimizer.step()
train_losses.append(loss.item())
 acc =(outputs.argmax(1)== y ).float().mean().item()
train_acc.append(acc)
 print('\ttraining loss:',
np.mean(train_losses), '\ttraining acc:', np.mean(train_acc))
all_train_acc.append(np.mean(train_acc))
model.eval()
with torch.no_grad() :
q = torch.tensor(
 q.reshape(-1, imgsz*imgsz)).to(device ).long()
outputs = F.softmax(model(q), dim=1)
 outputs = outputs.argmax(2 ).reshape(-1, imgsz, imgsz)
 plot_figure(x, y, q, outputs, im_num=task_id, img_sz=imgsz)
 for task_num, preds in enumerate(outputs.cpu().numpy()):
frow = np.nonzero(np.count_nonzero(preds-10, axis=1)) [0][0]
fcol = np.nonzero(np.count_nonzero(preds-10, axis=0)) [0][0]
preds = np.copy(preds[frow:, fcol:])
 preds[preds == 10] = 0
sample_sub.loc[f'{task_id[:-5]}_{task_num}',
'output'] = flattener(preds.astype(int ).tolist())
 print('
mean train acc:', np.mean(all_train_acc),
'stddev train acc:', np.std(all_train_acc))
<import_modules>
sample_sub.head()
sample_sub.to_csv('submission.csv' )