Kaggle competition: Abstraction and Reasoning Challenge
<SOS> metric: MeanBestErrorAtK Kaggle data source: abstraction-and-reasoning-challenge<data_type_conversions>
import numpy as np
import pandas as pd
import os
import json
from pathlib import Path
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
train_text = train['clean_comment'].fillna("")
 test_text = test['clean_comment'].fillna("" )<feature_engineering>
data_path = Path('/kaggle/input/abstraction-and-reasoning-challenge/')
 training_path = data_path / 'training'
evaluation_path = data_path / 'evaluation'
test_path = data_path / 'test'
np.random.seed(32)
 os.environ["OMP_NUM_THREADS"] = "4" 
<compute_train_metric>
task_file = str(training_path / '00d62c1b.json')
 with open(task_file, 'r')as f:
task = json.load(f)
 print(task.keys() )
class RocAucEvaluation(Callback):
def __init__(self, validation_data=() , interval=1):
super(Callback, self ).__init__()
self.interval = interval
self.X_val, self.y_val = validation_data
def on_epoch_end(self, epoch, logs={}):
if epoch % self.interval == 0:
y_pred = self.model.predict(self.X_val, verbose=0)
 score = roc_auc_score(self.y_val, y_pred)
 print("
ROC-AUC - epoch: {:d} - score: {:.6f}".format(epoch+1, score))<split>
n_train_pairs = len(task['train'])
 n_test_pairs = len(task['test'])
 print(f'task contains {n_train_pairs} training pairs')
 print(f'task contains {n_test_pairs} test pairs' )
y = train[class_names].values
X_train, X_valid, Y_train, Y_valid = train_test_split(train_text, y, test_size = 0.05 )<string_transform>
def getFillValue(im, out, maxValue=10):
Holes = findHoles(im)
 for FillValue in range(maxValue+1):
Filled = np.where(Holes, FillValue, im)
 if(Filled==out ).all() :
return FillValue
return 0
raw_text_train = X_train
raw_text_valid = X_valid
raw_text_test = test_text
tk = Tokenizer(num_words = max_features, lower = True)
 tk.fit_on_texts(raw_text_train)
 X_train["comment_seq"] = tk.texts_to_sequences(raw_text_train)
 X_valid["comment_seq"] = tk.texts_to_sequences(raw_text_valid)
 test["comment_seq"] = tk.texts_to_sequences(raw_text_test)
 X_train = pad_sequences(X_train.comment_seq, maxlen = max_len)
 X_valid = pad_sequences(X_valid.comment_seq, maxlen = max_len)
 test = pad_sequences(test.comment_seq, maxlen = max_len )<feature_engineering>
submission = pd.read_csv(data_path / 'sample_submission.csv', index_col='output_id')
 display(submission.head() )
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embedding_index = dict(get_coefs(*o.strip().split(" ")) for o in open(embedding_path))
word_index = tk.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.zeros(( nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embedding_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector<choose_model_class>
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
file_path = "best_model.hdf5"
check_point = ModelCheckpoint(file_path, monitor = "val_loss", verbose = 1,
save_best_only = True, mode = "min")
 ra_val = RocAucEvaluation(validation_data=(X_valid, Y_valid), interval = 1)
 early_stop = EarlyStopping(monitor = "val_loss", mode = "min", patience = 5)
 def build_model(lr = 0.0, lr_d = 0.0, units = 0, dr = 0.0):
inp = Input(shape =(max_len,))
x = Embedding(max_features, embed_size, weights = [embedding_matrix], trainable = False )(inp)
 x = SpatialDropout1D(dr )(x)
 x = Bidirectional(GRU(units, return_sequences = True))(x)
 x = Conv1D(64, kernel_size = 2, padding = "valid", kernel_initializer = "he_uniform" )(x)
 avg_pool = GlobalAveragePooling1D()(x)
 max_pool = GlobalMaxPooling1D()(x)
 x = concatenate([avg_pool, max_pool])
 x = Dense(6, activation = "sigmoid" )(x)
 model = Model(inputs = inp, outputs = x)
 model.compile(loss = "binary_crossentropy", optimizer = Adam(lr = lr, decay = lr_d), metrics = ["accuracy"])
 model.summary()
history = model.fit(X_train, Y_train, batch_size = 128, epochs = 2, validation_data =(X_valid, Y_valid),
verbose = 1, callbacks = [ra_val, check_point, early_stop])
 model = load_model(file_path)
 return model<choose_model_class>
example_grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
display(example_grid)
 print(flattener(example_grid))
<save_to_csv><EOS> 
def getDefaultPred(task, pair_id):
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 + ' '
return pred
FillerCount = 0
for output_id in 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 all([np.array(tr['input'] ).shape == np.array(tr['output'] ).shape for tr in task['train']]):
FillValues = np.unique([getFillValue(np.array(tr['input']), np.array(tr['output'])) for tr in task['train']])
 if len(FillValues)==1 and FillValues[0] > 0:
print('Found a Filler')
 FillerCount += 1
FillValue = FillValues[0]
im = np.array(task['test'][pair_id]['input'])
 Holes = findHoles(im)
 Filled = np.where(Holes, FillValue, im ).tolist()
pred = flattener(Filled)
 else:
pred = getDefaultPred(task, pair_id)
 else:
pred = getDefaultPred(task, pair_id)
 submission.loc[output_id, 'output'] = pred
submission.to_csv('submission.csv')
 print(f'{FillerCount} Fillers found in total' )