Kaggle competition: Conway's Reverse Game of Life
import matplotlib.pyplot as plt<save_to_csv>
%load_ext Cython
my_submission = pd.DataFrame({'ForecastId': list(range(1,len(pc2)+1)) , 'ConfirmedCases': pc2, 'Fatalities': pf2})
 print(my_submission)
 my_submission.to_csv('submission.csv', index=False )<set_options>
%%cython
c
@cython.cdivision(True)
 @cython.boundscheck(False)
 @cython.nonecheck(False)
 @cython.wraparound(False)
 cdef int calc_neighs(unsigned char[:, :] field, int i, int j, int n, int k):
cdef:
int neighs = 0;
int i_min = i - 1;
int i_pl = i + 1;
int j_min = j - 1;
int j_pl = j + 1;
neighs = 0
if i_min >= 0:
if j_min >= 0:
neighs += field[i_min, j_min]
neighs += field[i_min, j]
if j_pl < k:
neighs += field[i_min, j_pl]
if j_min >= 0:
neighs += field[i, j_min]
if j_pl < k:
neighs += field[i, j_pl]
if i_pl < n:
if j_min >= 0:
neighs += field[i_pl, j_min]
neighs += field[i_pl, j]
if j_pl < k:
neighs += field[i_pl, j_pl]
return neighs
@cython.cdivision(True)
 @cython.boundscheck(False)
 @cython.nonecheck(False)
 @cython.wraparound(False)
 cpdef make_move(unsigned char[:, :] field, int moves):
cdef:
int _, i, j, neighs;
int n, k;
int switch = 0;
unsigned char[:, :] cur_field;
unsigned char[:, :] next_field;
cur_field = np.copy(field)
 next_field = np.zeros_like(field, 'uint8')
 n = field.shape[0]
k = field.shape[1]
for _ in range(moves):
if switch == 0:
for i in range(n):
for j in range(k):
neighs = calc_neighs(cur_field, i, j, n, k)
 if cur_field[i, j] and neighs == 2:
next_field[i, j] = 1
elif neighs == 3:
next_field[i, j] = 1
else:
next_field[i, j] = 0
else:
for i in range(n):
for j in range(k):
neighs = calc_neighs(next_field, i, j, n, k)
 if next_field[i, j] and neighs == 2:
cur_field[i, j] = 1
elif neighs == 3:
cur_field[i, j] = 1
else:
cur_field[i, j] = 0
switch =(switch + 1)% 2
return np.array(next_field if switch else cur_field )
%matplotlib inline 
<define_variables>
NROW, NCOL = 20, 20
def generate_samples(delta=1, n=32):

 batch = np.split(np.random.binomial(1, 0.5,(NROW * n, NCOL)).astype('uint8'), n)
 Yy = [life.make_move(state, 5)for state in batch]
Xx = [life.make_move(state, 1)for state in Yy]
Y = np.array([y.ravel() for y in Yy])
 X = np.array([x.ravel() for x in Xx])
 return X, Y
def data_generator(delta=1, batch_size=32, ravel=True):

 while True:
batch = np.split(np.random.binomial(1, 0.5,(NROW * batch_size, NCOL)).astype('uint8'), batch_size)
 Yy = [make_move(state, 5)for state in batch]
Xx = [make_move(state, delta)for state in Yy]
if ravel:
Y = np.array([y.ravel() for y in Yy])
 X = np.array([x.ravel() for x in Xx])
 yield X, Y
else:
yield np.array(Xx)[:,:, :, np.newaxis], np.array(Yy)[:, :, :, np.newaxis]
base_dir = '.. /input/dogs-vs-cats-redux-kernels-edition'
train_dir = '.. /data/train'
test_dir = '.. /data/test'<load_pretrained>
def create_model(n_hidden_convs=2, n_hidden_filters=128, kernel_size=5):
nn = Sequential()
nn.add(Conv2D(n_hidden_filters, kernel_size, padding='same', activation='relu', input_shape=(20, 20, 1)))
 nn.add(BatchNormalization())
 for i in range(n_hidden_convs):
nn.add(Conv2D(n_hidden_filters, kernel_size, padding='same', activation='relu'))
nn.add(BatchNormalization())
 nn.add(Conv2D(1, kernel_size, padding='same', activation='sigmoid'))
nn.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 return nn
def model_v2() :
nn = Sequential()
nn.add(Conv2D(128, 5, padding='same', activation=lrelu, input_shape=(20, 20, 1)))
 nn.add(BatchNormalization())
 nn.add(Conv2D(128, 5, padding='valid', activation=lrelu))
nn.add(BatchNormalization())
 nn.add(MaxPool2D())
 nn.add(Conv2DTranspose(128, 2, strides=(2, 2), padding='valid', activation=lrelu))
nn.add(BatchNormalization())
 nn.add(Conv2DTranspose(128, 5, strides=(1, 1), padding='valid', activation=lrelu))
nn.add(BatchNormalization())
 nn.add(Conv2D(1, 5, padding='same', activation='sigmoid'))
nn.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 return nn
with zipfile.ZipFile(os.path.join(base_dir, 'train.zip')) as train_zip:
train_zip.extractall('.. /data')
 with zipfile.ZipFile(os.path.join(base_dir, 'test.zip')) as test_zip:
test_zip.extractall('.. /data' )<define_variables>
models = []
for delta in range(1, 6):
model = create_model(n_hidden_convs=6, n_hidden_filters=256)
 es = EarlyStopping(monitor='loss', patience=9, min_delta=0.001)
 model.fit_generator(data_generator(delta=delta, ravel=False), steps_per_epoch=500, epochs=50, verbose=1, callbacks=[es])
 models.append(model )
train_list = glob.glob(os.path.join(train_dir, '*.jpg'))
test_list = glob.glob(os.path.join(test_dir, '*.jpg'))<split>
train_df = pd.read_csv('.. /input/train.csv', index_col=0)
 test_df = pd.read_csv('.. /input/test.csv', index_col=0 )
train_list,val_list = train_test_split(train_list,test_size=0.1 )<normalization>
submit_df = pd.DataFrame(index=test_df.index, columns=['start.' + str(_)for _ in range(1, 401)] )
class ImageTransform() :
def __init__(self, resize, mean, std):
self.data_transform = {
'train': transforms.Compose([
transforms.RandomResizedCrop(resize, scale=(0.5, 1.0)) ,
transforms.RandomHorizontalFlip() ,
transforms.ToTensor() ,
transforms.Normalize(mean, std)
 ]),
'val': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(resize),
transforms.ToTensor() ,
transforms.Normalize(mean, std)
 ])
 }
def __call__(self, img, phase):
return self.data_transform[phase](img )<categorify>
for delta in range(1, 6):
mod = models[delta-1]
delta_df = test_df[test_df.delta == delta].iloc[:, 1:].values.reshape(-1, 20, 20, 1)
 submit_df[test_df.delta == delta] = mod.predict(delta_df ).reshape(-1, 400 ).round(0 ).astype('uint8' )
<set_options><EOS> 
submit_df.to_csv('cnns_40.csv' )