Kaggle competition: Abstraction and Reasoning Challenge
<SOS> metric: MeanBestErrorAtK Kaggle data source: abstraction-and-reasoning-challenge<predict_on_test>
import numpy as np
import pandas as pd
import itertools
import random
import os
import json
from pathlib import Path
from tqdm.notebook import tqdm
import matplotlib.pyplot as plt
from matplotlib import colors
results = model.predict(test)
<save_to_csv>
data_path = Path('/kaggle/input/abstraction-and-reasoning-challenge/')
 training_path = data_path / 'training'
training_tasks = sorted(os.listdir(training_path))
evaluation_path = data_path / 'evaluation'
evaluation_tasks = sorted(os.listdir(evaluation_path))
test_path = data_path / 'test'
test_tasks = sorted(os.listdir(test_path))
results = np.argmax(results,axis = 1)
 submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 submission['label'] = results
submission.to_csv('submission.csv', index=False)
 submission.head()<set_options>
def identity(x: [np.array]):
return x
def tail(x):
if len(x)> 1:
return x[1:]
else:
return x
def init(x):
if len(x)> 1:
return x[:1]
else:
return x
def union(x):

 if len(x)< 2:
return x
first_shape = tuple(x[0].shape)
 for pixmap in x[1:]:
if first_shape != tuple(pixmap.shape):
return []
return [np.bitwise_or.reduce(np.array(x ).astype(int)) ]
def intersect(x):

 if len(x)< 2:
return x
first_shape = tuple(x[0].shape)
 for pixmap in x[1:]:
if first_shape != tuple(pixmap.shape):
return []
return [(np.prod(np.array(x), axis=0)> 0 ).astype(int)]
def sortByColor(xs):

 xs = [x for x in xs if len(x.reshape(-1)) > 0]
return list(sorted(xs, key=lambda x: x.max()))
def sortByWeight(xs):

 xs = [x for x in xs if len(x.reshape(-1)) > 0]
return list(sorted(xs, key=lambda x:(x>0 ).sum()))
def reverse(x):

 return x[::-1]
def sortByNumberOfColors(xs):

 return list(sorted(xs, key=lambda x: len(np.unique(x))))
def xor(x):

 if len(x)< 2:
return x
first_shape = tuple(x[0].shape)
 for pixmap in x[1:]:
if first_shape != tuple(pixmap.shape):
return []
return [np.bitwise_xor.reduce(np.array(x ).astype(int)) ]
%matplotlib inline
BATCH_SIZE = 256
print(os.listdir('.. /input/Kannada-MNIST/'))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 print(device )<load_from_csv>
def lift(fct):
def lifted_function(xs):
list_of_results = [fct(x)for x in xs]
return list(itertools.chain(*list_of_results))
lifted_function.__name__ = re.sub('_unlifted$', '_lifted', fct.__name__)
 return lifted_function
def lift_functions_list(unlifted_functions):
lifted_functions=[]
for fct in unlifted_functions:
def lifted_function(xs):
list_of_results = [fct(x)for x in xs]
return list(itertools.chain(*list_of_results))
lifted_function.__name__ = re.sub('_unlifted$', '_lifted', fct.__name__)
 lifted_functions.append(lifted_function)
 return lifted_functions
df_train = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 target = df_train['label']
df_train.drop('label', axis=1, inplace=True)
 X_test = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 X_test.drop('id', axis=1, inplace=True)
 X_test.head()<split>
cropToContent = lift(cropToContent_unlifted)
 groupByColor = lift(groupByColor_unlifted)
 splitH = lift(splitH_unlifted)
 negative = lift(negative_unlifted)
 initial_functions=[tail, init, union, intersect, sortByColor, sortByWeight, reverse,\
cropToContent, groupByColor, splitH, negative]
to_c0=lift(to_c0_unlifted)
 to_c1=lift(to_c1_unlifted)
 to_c2=lift(to_c2_unlifted)
 to_c3=lift(to_c3_unlifted)
 to_c4=lift(to_c4_unlifted)
 to_c5=lift(to_c5_unlifted)
 to_c6=lift(to_c6_unlifted)
 to_c7=lift(to_c7_unlifted)
 to_c8=lift(to_c8_unlifted)
 to_c9=lift(to_c9_unlifted)
 single_color_functions=[to_c1,to_c2, to_c3, to_c4, to_c5, to_c6, to_c7, to_c8, to_c9]
c1_to_c0=lift(c1_to_c0_unlifted)
 c1_to_c2=lift(c1_to_c2_unlifted)
 c1_to_c3=lift(c1_to_c3_unlifted)
 c1_to_c4=lift(c1_to_c4_unlifted)
 c1_to_c5=lift(c1_to_c5_unlifted)
 c1_to_c6=lift(c1_to_c6_unlifted)
 c1_to_c7=lift(c1_to_c7_unlifted)
 c1_to_c8=lift(c1_to_c8_unlifted)
 c1_to_c9=lift(c1_to_c9_unlifted)
 c2_to_c0=lift(c2_to_c0_unlifted)
 c2_to_c1=lift(c2_to_c1_unlifted)
 c2_to_c3=lift(c2_to_c3_unlifted)
 c2_to_c4=lift(c2_to_c4_unlifted)
 c2_to_c5=lift(c2_to_c5_unlifted)
 c2_to_c6=lift(c2_to_c6_unlifted)
 c2_to_c7=lift(c2_to_c7_unlifted)
 c2_to_c8=lift(c2_to_c8_unlifted)
 c2_to_c9=lift(c2_to_c9_unlifted)
 c3_to_c0=lift(c3_to_c0_unlifted)
 c3_to_c1=lift(c3_to_c1_unlifted)
 c3_to_c2=lift(c3_to_c2_unlifted)
 c3_to_c4=lift(c3_to_c4_unlifted)
 c3_to_c5=lift(c3_to_c5_unlifted)
 c3_to_c6=lift(c3_to_c6_unlifted)
 c3_to_c7=lift(c3_to_c7_unlifted)
 c3_to_c8=lift(c3_to_c8_unlifted)
 c3_to_c9=lift(c3_to_c9_unlifted)
 c4_to_c0=lift(c4_to_c0_unlifted)
 c4_to_c1=lift(c4_to_c1_unlifted)
 c4_to_c2=lift(c4_to_c2_unlifted)
 c4_to_c3=lift(c4_to_c3_unlifted)
 c4_to_c5=lift(c4_to_c5_unlifted)
 c4_to_c6=lift(c4_to_c6_unlifted)
 c4_to_c7=lift(c4_to_c7_unlifted)
 c4_to_c8=lift(c4_to_c8_unlifted)
 c4_to_c9=lift(c4_to_c9_unlifted)
 c5_to_c0=lift(c5_to_c0_unlifted)
 c5_to_c1=lift(c5_to_c1_unlifted)
 c5_to_c2=lift(c5_to_c2_unlifted)
 c5_to_c3=lift(c5_to_c3_unlifted)
 c5_to_c4=lift(c5_to_c4_unlifted)
 c5_to_c6=lift(c5_to_c6_unlifted)
 c5_to_c7=lift(c5_to_c7_unlifted)
 c5_to_c8=lift(c5_to_c8_unlifted)
 c5_to_c9=lift(c5_to_c9_unlifted)
 c6_to_c0=lift(c6_to_c0_unlifted)
 c6_to_c1=lift(c6_to_c1_unlifted)
 c6_to_c2=lift(c6_to_c2_unlifted)
 c6_to_c3=lift(c6_to_c3_unlifted)
 c6_to_c4=lift(c6_to_c4_unlifted)
 c6_to_c5=lift(c6_to_c5_unlifted)
 c6_to_c7=lift(c6_to_c7_unlifted)
 c6_to_c8=lift(c6_to_c8_unlifted)
 c6_to_c9=lift(c6_to_c9_unlifted)
 c7_to_c0=lift(c7_to_c0_unlifted)
 c7_to_c1=lift(c7_to_c1_unlifted)
 c7_to_c2=lift(c7_to_c2_unlifted)
 c7_to_c3=lift(c7_to_c3_unlifted)
 c7_to_c4=lift(c7_to_c4_unlifted)
 c7_to_c5=lift(c7_to_c5_unlifted)
 c7_to_c6=lift(c7_to_c6_unlifted)
 c7_to_c8=lift(c7_to_c8_unlifted)
 c7_to_c9=lift(c7_to_c9_unlifted)
 c1_to_c0=lift(c1_to_c0_unlifted)
 c8_to_c0=lift(c8_to_c0_unlifted)
 c8_to_c1=lift(c8_to_c1_unlifted)
 c8_to_c2=lift(c8_to_c2_unlifted)
 c8_to_c3=lift(c8_to_c3_unlifted)
 c8_to_c4=lift(c8_to_c4_unlifted)
 c8_to_c5=lift(c8_to_c5_unlifted)
 c8_to_c6=lift(c8_to_c6_unlifted)
 c8_to_c7=lift(c8_to_c7_unlifted)
 c8_to_c9=lift(c8_to_c9_unlifted)
 c9_to_c0=lift(c9_to_c0_unlifted)
 c9_to_c1=lift(c9_to_c1_unlifted)
 c9_to_c2=lift(c9_to_c2_unlifted)
 c9_to_c3=lift(c9_to_c3_unlifted)
 c9_to_c4=lift(c9_to_c4_unlifted)
 c9_to_c5=lift(c9_to_c5_unlifted)
 c9_to_c6=lift(c9_to_c6_unlifted)
 c9_to_c7=lift(c9_to_c7_unlifted)
 c9_to_c8=lift(c9_to_c8_unlifted)
 color_switch_functions = [c1_to_c0, c1_to_c2, c1_to_c3, c1_to_c4, c1_to_c5, c1_to_c6,\
c1_to_c7, c1_to_c8, c1_to_c9, c2_to_c0, c2_to_c1, c2_to_c3,\
c2_to_c4, c2_to_c5, c2_to_c6, c2_to_c7, c2_to_c8,\
c2_to_c9, c3_to_c0, c3_to_c1, c3_to_c2, c3_to_c4, c3_to_c5,\
c3_to_c6, c3_to_c7, c3_to_c8, c3_to_c9, c4_to_c0, c4_to_c1,\
c4_to_c2, c4_to_c3, c4_to_c5, c4_to_c6, c4_to_c7,\
c4_to_c8, c4_to_c9, c5_to_c0, c5_to_c1, c5_to_c2, c5_to_c3,\
c5_to_c4, c5_to_c6, c5_to_c7, c5_to_c8, c5_to_c9, c6_to_c0,\
c6_to_c1, c6_to_c2, c6_to_c3, c6_to_c4, c6_to_c5,\
c6_to_c7, c6_to_c8, c6_to_c9, c7_to_c0, c7_to_c1, c7_to_c2,\
c7_to_c3, c7_to_c4, c7_to_c5, c7_to_c6, c7_to_c8,\
c7_to_c9, c8_to_c0, c8_to_c1, c8_to_c2, c8_to_c3, c8_to_c4,\
c8_to_c5, c8_to_c6, c8_to_c7, c8_to_c9, c9_to_c0, c9_to_c1,\
c9_to_c2, c9_to_c3, c9_to_c4, c9_to_c5, c9_to_c6,\
c9_to_c7, c9_to_c8]
unite_c1_horizontally=lift(unite_c1_horizontally_unlifted)
 unite_c2_horizontally=lift(unite_c2_horizontally_unlifted)
 unite_c3_horizontally=lift(unite_c3_horizontally_unlifted)
 unite_c4_horizontally=lift(unite_c4_horizontally_unlifted)
 unite_c5_horizontally=lift(unite_c5_horizontally_unlifted)
 unite_c6_horizontally=lift(unite_c6_horizontally_unlifted)
 unite_c7_horizontally=lift(unite_c7_horizontally_unlifted)
 unite_c8_horizontally=lift(unite_c8_horizontally_unlifted)
 unite_c9_horizontally=lift(unite_c9_horizontally_unlifted)
 unite_c1_vertically=lift(unite_c1_vertically_unlifted)
 unite_c2_vertically=lift(unite_c2_vertically_unlifted)
 unite_c3_vertically=lift(unite_c3_vertically_unlifted)
 unite_c4_vertically=lift(unite_c4_vertically_unlifted)
 unite_c5_vertically=lift(unite_c5_vertically_unlifted)
 unite_c6_vertically=lift(unite_c6_vertically_unlifted)
 unite_c7_vertically=lift(unite_c7_vertically_unlifted)
 unite_c8_vertically=lift(unite_c8_vertically_unlifted)
 unite_c9_vertically=lift(unite_c9_vertically_unlifted)
 unite_colors_functions=[unite_c1_horizontally, unite_c2_horizontally, unite_c3_horizontally,\
unite_c4_horizontally, unite_c5_horizontally, unite_c6_horizontally,\
unite_c7_horizontally, unite_c8_horizontally, unite_c9_horizontally,\
unite_c1_vertically, unite_c2_vertically, unite_c3_vertically,\
unite_c4_vertically, unite_c5_vertically, unite_c6_vertically,\
unite_c7_vertically, unite_c8_vertically, unite_c9_vertically]
add_c1_frame_to_single_squares=lift(add_c1_frame_to_single_squares_unlifted)
 add_c2_frame_to_single_squares=lift(add_c2_frame_to_single_squares_unlifted)
 add_c3_frame_to_single_squares=lift(add_c3_frame_to_single_squares_unlifted)
 add_c4_frame_to_single_squares=lift(add_c4_frame_to_single_squares_unlifted)
 add_c5_frame_to_single_squares=lift(add_c5_frame_to_single_squares_unlifted)
 add_c6_frame_to_single_squares=lift(add_c6_frame_to_single_squares_unlifted)
 add_c7_frame_to_single_squares=lift(add_c7_frame_to_single_squares_unlifted)
 add_c8_frame_to_single_squares=lift(add_c8_frame_to_single_squares_unlifted)
 add_c9_frame_to_single_squares=lift(add_c9_frame_to_single_squares_unlifted)
 add_frame_colors_functions= [add_c1_frame_to_single_squares, add_c2_frame_to_single_squares, add_c3_frame_to_single_squares, add_c4_frame_to_single_squares, add_c5_frame_to_single_squares, add_c6_frame_to_single_squares, add_c7_frame_to_single_squares, add_c8_frame_to_single_squares, add_c9_frame_to_single_squares]
hollow_c1=lift(hollow_c1_unlifted)
 hollow_c2=lift(hollow_c2_unlifted)
 hollow_c3=lift(hollow_c3_unlifted)
 hollow_c4=lift(hollow_c4_unlifted)
 hollow_c5=lift(hollow_c5_unlifted)
 hollow_c6=lift(hollow_c6_unlifted)
 hollow_c7=lift(hollow_c7_unlifted)
 hollow_c8=lift(hollow_c8_unlifted)
 hollow_c9=lift(hollow_c9_unlifted)
 hollow_all=lift(hollow_all_unlifted)
 hollow_functions=[hollow_c1,hollow_c2,hollow_c3,hollow_c4,hollow_c5,hollow_c6,hollow_c7,hollow_c8,hollow_c9, hollow_all]
fill_enclosed_area_c1=lift(fill_enclosed_area_c1_unlifted)
 fill_enclosed_area_c2=lift(fill_enclosed_area_c2_unlifted)
 fill_enclosed_area_c3=lift(fill_enclosed_area_c3_unlifted)
 fill_enclosed_area_c4=lift(fill_enclosed_area_c4_unlifted)
 fill_enclosed_area_c5=lift(fill_enclosed_area_c5_unlifted)
 fill_enclosed_area_c6=lift(fill_enclosed_area_c6_unlifted)
 fill_enclosed_area_c7=lift(fill_enclosed_area_c7_unlifted)
 fill_enclosed_area_c8=lift(fill_enclosed_area_c8_unlifted)
 fill_enclosed_area_c9=lift(fill_enclosed_area_c9_unlifted)
 fill_enclosed_functions=[fill_enclosed_area_c1, fill_enclosed_area_c2, fill_enclosed_area_c3, fill_enclosed_area_c4, fill_enclosed_area_c5, fill_enclosed_area_c6, fill_enclosed_area_c7, fill_enclosed_area_c8, fill_enclosed_area_c9]
splitV = lift(splitV_unlifted)
 rotate_counterclockwise=lift(rotate_counterclockwise_unlifted)
 rotate_clockwise=lift(rotate_clockwise_unlifted)
 triplicate=lift(triplicate_unlifted)
 duplicate=lift(duplicate_unlifted)
 get_enclosed_area=lift(get_enclosed_area_unlifted)
 extrapolate=lift(extrapolate_unlifted)
 lower_elements=lift(lower_elements_unlifted)
 duplicate_horizontally=lift(duplicate_horizontally_unlifted)
 duplicate_horizontally_symmetrically=lift(duplicate_horizontally_symmetrically_unlifted)
 duplicate_vertically=lift(duplicate_vertically_unlifted)
 duplicate_vertically_symmetrically=lift(duplicate_vertically_symmetrically_unlifted)
 flip_horizontally=lift(flip_horizontally_unlifted)
 flip_vertically=lift(flip_vertically_unlifted)
 rotate_colors=lift(rotate_colors_unlifted)
 paint_all_most_common_color=lift(paint_all_most_common_color_unlifted)
 added_functions=[splitV, rotate_counterclockwise, rotate_clockwise, triplicate,\
duplicate, get_enclosed_area, lower_elements, sortByNumberOfColors,\
xor, duplicate_horizontally, duplicate_horizontally_symmetrically,\
duplicate_vertically, duplicate_vertically_symmetrically,\
flip_vertically, flip_horizontally, rotate_colors]
all_functions=initial_functions+single_color_functions+color_switch_functions+unite_colors_functions+add_frame_colors_functions+\
hollow_functions+fill_enclosed_functions+added_functions
X_train, X_dev, y_train, y_dev = train_test_split(df_train, target, stratify=target, random_state=42, test_size=0.2)
 print('X_train', len(X_train))
print('X_dev', len(X_dev))
print('X_test', len(X_test))<categorify>
def evaluate(program: [], input_image: np.array):
input_image = np.array(input_image)
 assert type(input_image)== np.ndarray
image_list = [input_image]
for fct in program:
image_list = fct(image_list)
 image_list = [img for img in image_list if img.shape[0] > 0 and img.shape[1] > 0]
if image_list == []:
return []
return image_list
class CharData(Dataset):

 def __init__(self,
images,
labels=None,
transform=None, 
):
self.X = images
self.y = labels
self.transform = transform
def __len__(self):
return len(self.X)
 def __getitem__(self, idx):
img = np.array(self.X.iloc[idx, :], dtype='uint8' ).reshape([28, 28, 1])
 if self.transform is not None:
img = self.transform(img)
 if self.y is not None:
y = np.zeros(10, dtype='float32')
 y[self.y.iloc[idx]] = 1
return img, y
else:
return img<categorify>
def are_two_images_equals(a, b):
if tuple(a.shape)== tuple(b.shape):
if(np.abs(b-a)< 1 ).all() :
return True
return False
def is_solution(program, task, verbose=True):
for sample in task:
i = np.array(sample['input'])
 o = np.array(sample['output'])
 images = evaluate(program, i)
 if len(images)< 1:
return False
images = images[:3]
is_program_of_for_sample = any([are_two_images_equals(x, o)for x in images])
 if not is_program_of_for_sample:
return False
return True
train_transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=5, translate=(0.1, 0.1), scale=(0.9, 1.1), shear=5),
transforms.ToTensor()
])
 test_transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor()
] )<prepare_x_and_y>
def width_fitness(predicted, expected_output):

 return np.abs(predicted.shape[0] - expected_output.shape[0])
 def height_fitness(predicted, expected_output):

 return np.abs(predicted.shape[1] - expected_output.shape[1])
 def activated_pixels_fitness(p, e):

 shape =(max(p.shape[0], e.shape[0]), max(p.shape[1], e.shape[1]))
diff = np.zeros(shape, dtype=int)
 diff[0:p.shape[0], 0:p.shape[1]] =(p > 0 ).astype(int)
 diff[0:e.shape[0], 0:e.shape[1]] -=(e > 0 ).astype(int)
 return(diff != 0 ).sum()
def colors_fitness(p, e):
p_colors = np.unique(p)
 e_colors = np.unique(e)
 nb_inter = len(np.intersect1d(p_colors, e_colors))
return(len(p_colors)- nb_inter)+(len(e_colors)- nb_inter)
 fitness_functions = [colors_fitness, activated_pixels_fitness, height_fitness, width_fitness]
train_dataset = CharData(X_train, y_train, train_transform)
 dev_dataset = CharData(X_dev, y_dev, test_transform)
 test_dataset = CharData(X_test, transform=test_transform )<load_pretrained>
def product_less(a, b):

 a = np.array(a)
 b = np.array(b)
 return(np.array(a)< np.array(b)).all()
train_loader = DataLoader(dataset=train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)
 dev_loader = DataLoader(dataset=dev_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4)
 test_loader = DataLoader(dataset=test_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4 )<define_search_model>
def evaluate_fitness(program, task):

 score = np.zeros(( len(fitness_functions)))
 for sample in task:
i = np.array(sample['input'])
 o = np.array(sample['output'])
 for index, fitness_function in enumerate(fitness_functions):
images = evaluate(program, i)
 if images == []:
score[index] += 500
else:
score[index] = fitness_function(images[0], o)
 return tuple(score )
DEPTH_MULT = 4
class ConvLayer(nn.Module):
def __init__(self, input_size, output_size, kernel_size=3):
super(ConvLayer, self ).__init__()
self.ops = nn.Sequential(
 nn.Conv2d(input_size, output_size, kernel_size=kernel_size, stride=1, padding=kernel_size//2),
nn.BatchNorm2d(output_size),
nn.ReLU(inplace=True)
)
 def forward(self, x):
return self.ops(x)
 class FCLayer(nn.Module):
def __init__(self, input_size, output_size):
super(FCLayer, self ).__init__()
self.ops = nn.Sequential(
 nn.Linear(input_size, output_size),
nn.BatchNorm1d(output_size),
nn.ReLU(inplace=True)
)
 def forward(self, x):
return self.ops(x)
 def mixup(x, shuffle, lam, i, j):
if shuffle is not None and lam is not None and i == j:
x = lam * x +(1 - lam)* x[shuffle]
return x
class Net(nn.Module):
def __init__(self, num_classes):
super(Net, self ).__init__()
self.conv1 = ConvLayer(1, DEPTH_MULT * 32)
 self.conv2 = ConvLayer(DEPTH_MULT * 32, DEPTH_MULT * 32)
 self.conv3 = ConvLayer(DEPTH_MULT * 32, DEPTH_MULT * 32)
 self.conv4 = ConvLayer(DEPTH_MULT * 32, DEPTH_MULT * 32)
 self.conv5 = ConvLayer(DEPTH_MULT * 32, DEPTH_MULT * 64)
 self.conv6 = ConvLayer(DEPTH_MULT * 64, DEPTH_MULT * 64)
 self.conv7 = ConvLayer(DEPTH_MULT * 64, DEPTH_MULT * 64)
 self.conv8 = ConvLayer(DEPTH_MULT * 64, DEPTH_MULT * 64)
 self.conv9 = ConvLayer(DEPTH_MULT * 64, DEPTH_MULT * 64)
 self.conv10 = ConvLayer(DEPTH_MULT * 64, DEPTH_MULT * 64)
 self.mp = nn.MaxPool2d(kernel_size=2, stride=2)
 self.fc1 = FCLayer(DEPTH_MULT * 64 * 7 * 7, DEPTH_MULT * 512)
 self.fc2 = FCLayer(DEPTH_MULT * 512, DEPTH_MULT * 512)
 self.fc3 = FCLayer(DEPTH_MULT * 512, DEPTH_MULT * 512)
 self.fc4 = FCLayer(DEPTH_MULT * 512, DEPTH_MULT * 512)
 self.projection = nn.Linear(DEPTH_MULT * 512, 10)
 def forward(self, x):
x = self.conv1(x)
 x = self.conv2(x)
 x = self.conv3(x)
 x = self.conv4(x)
 x = self.mp(x)
 x = self.conv5(x)
 x = self.conv6(x)
 x = self.conv7(x)
 x = self.conv8(x)
 x = self.conv9(x)
 x = self.conv10(x)
 x = self.mp(x)
 x = x.view(x.size(0), -1)
 x = self.fc1(x)
 x = self.fc2(x)
 x = self.fc3(x)
 x = self.fc4(x)
 x = self.projection(x)
 return x<compute_test_metric>
def build_candidates(allowed_nodes=[identity], best_candidates=[], nb_candidates=200):

 new_candidates = []
length_limit = 4
def random_node() :
return random.choice(allowed_nodes)
 while(len(new_candidates)< nb_candidates):
for i in range(5):
new_candidates += [[random_node() ]]
for best_program in best_candidates:
if len(best_program)< length_limit - 1:
new_candidates += [[random_node() ] + best_program]
if len(best_program)< length_limit - 1:
new_candidates += [best_program + [random_node() ]]
new_candidates += [list(best_program)]
new_candidates[-1][random.randrange(0, len(best_program)) ] = random_node()
np.random.shuffle(new_candidates)
 return new_candidates[:nb_candidates]
def criterion(input, target, size_average=True):

 l = -(target * torch.log(F.softmax(input, dim=1)+ 1e-10)).sum(1)
 if size_average:
l = l.mean()
else:
l = l.sum()
return l<choose_model_class>
def remove_functions(all_functions, to_remove):
reduced_functions=[]
for function in all_functions:
fname=str(function.__name__)
 if '_lifted' in fname:
fname=fname[:-7]
if fname not in to_remove:
reduced_functions.append(function)
 return reduced_functions
def color_change_heuristics(all_functions_list, task):

 outputs=np.zeros(0)
 for test_or_train in [task['train'], task['test']]:
for x in test_or_train:
if len(x)==2:
outputs=np.append(outputs,x['output'])
 to_remove=[]
for color in range(0,10):
if color not in np.unique(outputs):
to_remove+=['to_c'+str(color)]
other_colors= list(range(0,10))
other_colors.remove(color)
 for other_color in other_colors:
to_remove+=['c'+str(other_color)+'_to_c'+str(color)]
inputs=np.zeros(0)
 for test_or_train in [task['train'], task['test']]:
for x in test_or_train:
inputs=np.append(inputs,x['input'])
 for color in range(1,10):
if color not in np.unique(inputs):
other_colors= list(range(1,10))
other_colors.remove(color)
 for other_color in other_colors:
to_remove+=['c'+str(color)+'_to_c'+str(other_color)]
return remove_functions(all_functions, to_remove)
 def initial_color_heuristic(all_functions_list, task):

 present_colors=np.zeros(0)
 for test_or_train in [task['train'], task['test']]:
for x in test_or_train:
present_colors=np.append(present_colors,x['input'])
 if len(x)==2:
present_colors=np.append(present_colors,x['output'])
 colors_not_present=[]
for colorcode in range(0,10):
if colorcode not in np.unique(present_colors):
colors_not_present=np.append(colors_not_present, colorcode)
 to_remove=[]
for colorcode in colors_not_present:
for function in all_functions_list:
if 'c'+str(int(colorcode)) in function.__name__:
fname=function.__name__
if '_lifted' in fname:
fname=fname[:-7]
to_remove+=[fname]
return remove_functions(all_functions, to_remove)
 def heuristics(all_functions_list, task):
reduced_functions=initial_color_heuristic(all_functions_list, task)
 reduced_functions=color_change_heuristics(reduced_functions, task)
 return reduced_functions
<train_model><EOS> 
def build_model(task, candidates_nodes=all_functions, max_iterations=20, verbose=True):
if verbose:
print("Candidates nodes are:", [program_desc([n])for n in candidates_nodes])
 print()
best_candidates = {}
for i in range(max_iterations):
if verbose:
print("Iteration ", i+1)
 print("-" * 10)
 candidates = build_candidates(candidates_nodes, best_candidates.values())
 for candidate in candidates:
score = evaluate_fitness(candidate, task)
 is_uncomparable = True
best_candidates_items = list(best_candidates.items())
 for best_score, best_candidate in best_candidates_items:
if product_less(score, best_score):
del best_candidates[best_score]
best_candidates[score] = candidate
is_uncomparable = False
if product_less(best_score, score)or best_score == score:
is_uncomparable = False
if is_uncomparable:
best_candidates[score] = candidate
for program in best_candidates.values() :
if is_solution(program, task):
return program
if verbose:
print("Best candidates lenght:", len(best_candidates))
random_candidate_score = random.choice(list(best_candidates.keys()))
print("Random candidate score:", random_candidate_score)
 print("Random candidate implementation:", program_desc(best_candidates[random_candidate_score]))
return None