Kaggle competition: Abstraction and Reasoning Challenge
<SOS> metric: MeanBestErrorAtK Kaggle data source: abstraction-and-reasoning-challenge<string_transform>
rc('animation', html='jshtml')
 device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu' )
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.loc[k,'text']
else:
text1 = " "+" ".join(test.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 st = tokenizer.decode(enc.ids[a-2:b-1])
 all.append(st )<save_to_csv>
data_path = Path('/kaggle/input/abstraction-and-reasoning-challenge')
 train_path = data_path / 'training'
valid_path = data_path / 'evaluation'
test_path = data_path / 'test'
train_tasks = { task.stem: json.load(task.open())for task in train_path.iterdir() }
valid_tasks = { task.stem: json.load(task.open())for task in valid_path.iterdir() }
test['selected_text'] = all
test[['textID','selected_text']].to_csv('submission.csv',index=False)
 pd.set_option('max_colwidth', 60)
 test.sample(25 )<install_modules>
class CAModel(nn.Module):
def __init__(self, num_states):
super(CAModel, self ).__init__()
self.transition = nn.Sequential(
 nn.Conv2d(num_states, 32, kernel_size=3, padding=1),
nn.ReLU() ,
nn.Conv2d(32, num_states, kernel_size=1)
)
 def forward(self, x, steps=1):
for _ in range(steps):
x = self.transition(torch.softmax(x, dim=1))
return x
!pip install -qU '.. /input/libraries/tokenizers-0.7.0-cp37-cp37m-linux_x86_64.whl'
!pip install -qU '.. /input/libraries/pytorch_lightning-0.7.5-py3-none-any.whl'
!pip install -qU '.. /input/libraries/wandb-0.8.35-py2.py3-none-any.whl'
!pip install -qU '.. /input/libraries/transformers-2.8.0-py3-none-any.whl'<set_options>
def solve_task(task, max_steps=12):
model = CAModel(10 ).to(device)
 num_epochs = 100
num_repeat = 25
criterion = nn.CrossEntropyLoss()
losses = np.zeros(( num_repeat - 1)* num_epochs)
 for i in range(1, num_repeat):
if i > num_repeat-4:
num_steps = max_steps
else:
num_steps = random.randint(1, max_steps)
 optimizer = torch.optim.Adam(model.parameters() , lr=(0.04 / i))
for e in range(num_epochs):
optimizer.zero_grad()
loss = 0.0
for sample in task:
x = torch.from_numpy(inp2img(sample["input"])).unsqueeze(0 ).float().to(device)
 y = torch.tensor(sample["output"] ).long().unsqueeze(0 ).to(device)
 y_pred = model(x, num_steps)
 loss += criterion(y_pred, y)
 y_in = torch.from_numpy(inp2img(sample["output"])).unsqueeze(0 ).float().to(device)
 y_pred = model(y_in, 1)
 loss += criterion(y_pred, y)
 loss.backward()
optimizer.step()
losses[(i - 1)* num_epochs + e] = loss.item()
return model, num_steps, losses
@torch.no_grad()
def predict(model, task):
predictions = []
for sample in task:
x = torch.from_numpy(inp2img(sample["input"])).unsqueeze(0 ).float().to(device)
 pred = model(x, 200 ).argmax(1 ).squeeze().cpu().numpy()
predictions.append(pred)
 return predictions
task = train_tasks["db3e9e38"]["train"]
model, num_steps, losses = solve_task(task )
%matplotlib inline
warnings.filterwarnings('ignore' )<set_options>
predictions = predict(model, task)
 for i in range(len(task)) :
plot_sample(task[i], predictions[i] )
def seed_everything(seed=7):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.cuda.manual_seed_all(seed)
 torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True<define_variables>
test = train_tasks["db3e9e38"]["test"]
predictions = predict(model, test)
 for i in range(len(test)) :
plot_sample(test[i], predictions[i] )
class GlobalConfig:
def __init__(self):
self.seed = 7
self.path = Path('.. /input/tweet-sentiment-extraction/')
 self.max_length = 128
self.roberta_path = Path('.. /input/roberta-base/')
 self.num_workers = os.cpu_count()
self.batch_size = 64
self.accum_steps = 1
self.epochs = 5
self.warmup_steps = 0
self.lr = 3e-5
self.offline = True
self.saved_model_path = Path('/kaggle/working/final_models')
 self.n_splits = 5
self.saved_model_path.mkdir(parents=True, exist_ok=True )<set_options>
def evaluate(tasks):
result = []
predictions = []
for idx, task in tqdm(tasks.items()):
if input_output_shape_is_same(task):
model, _, _ = solve_task(task["train"])
 pred = predict(model, task["test"])
 score = calk_score(task["test"], pred)
 else:
pred = [el["input"] for el in task["test"]]
score = [0] * len(task["test"])
 predictions.append(pred)
 result.append(score)
 return result, predictions
GCONF = GlobalConfig()
if not GCONF.offline: wandb.login(key=UserSecretsClient().get_secret('WANDB_KEY'))
else: wandb.login(key='X'*40)
 seed_everything(GCONF.seed)
 [f.name for f in GCONF.path.iterdir() ]<define_variables>
train_result, train_predictions = evaluate(train_tasks)
 train_solved = [any(score)for score in train_result]
total = sum([len(score)for score in train_result])
 print(f"solved : {sum(train_solved)} from {total}({sum(train_solved)/total})" )
spaces = ['\u200b', '\u200e', '\u202a', '\u202c', '\ufeff', '\uf0d8', '\u2061', '\x10', '\x7f', '\x9d', '\xad', '\xa0', '\u202f']<string_transform>
import pandas as pd
def remove_space(text):
for space in spaces: text = text.replace(space, ' ')
 text = text.strip()
text = re.sub(r'\s+', ' ', text)
 return text.strip()<load_from_csv>
import pandas as pd
train_df = pd.read_csv(GCONF.path/'train.csv')
 train_df = train_df.dropna().reset_index(drop=True)
 print(train_df.shape)
 train_df.head()<feature_engineering>
submission = pd.read_csv(data_path / 'sample_submission.csv', index_col='output_id')
 display(submission.head() )
train_df['text'] = train_df['text'].apply(remove_space)
 train_df['selected_text'] = train_df['selected_text'].apply(remove_space )<string_transform>
def flattener(pred):
str_pred = str([row for row in pred])
 str_pred = str_pred.replace(', ', '')
 str_pred = str_pred.replace('[[', '|')
 str_pred = str_pred.replace('][', '|')
 str_pred = str_pred.replace(']]', '|')
 return str_pred
<load_pretrained><EOS> 
for output_id in tqdm(submission.index):
task_id = output_id.split('_')[0]
pair_id = int(output_id.split('_')[1])
 f = str(test_path / str(task_id + '.json'))
with open(f, 'r')as read_file:
task = json.load(read_file)
 if input_output_shape_is_same(task):
print(task_id)
 model, _, _ = solve_task(task["train"])
 pred = predict(model, task["train"])
 for i in range(len(task['train'])) :
plot_sample(task['train'][i], pred[i])
 pred = predict(model, task["test"])
 for i in range(len(task['test'])) :
plot_sample(task['test'][i], pred[i])
 pred = flattener(pred[pair_id].tolist())
 else:
data = task['test'][pair_id]['input']
pred_1 = flattener(data)
 data = [[5 if i==0 else i for i in j] for j in data]
pred_2 = flattener(data)
 data = [[0 for i in j] for j in data]
pred_3 = flattener(data)
 pred = pred_1 + ' ' + pred_2 + ' ' + pred_3 + ' '
submission.loc[output_id, 'output'] = pred
submission.to_csv('submission.csv' )