# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import tensorflow as tf

# What we really want to do is building a *custom estimator* which will simplify our life for results tracking and data visualization. Further improvements might include a distributed Cloud version of it to overcome the important computational cost it requires.

# +
# loading datasets and phreshing folds for evaluations and testing
train_ds, test_ds = np.load("train.npz"), np.load("test.npz")

partition, x_train, y_train = train_ds["partition"], train_ds["X_train"], train_ds["y_train"]
x_test, y_test = test_ds['X_test'], test_ds['y_test']
# -

# First attempt, _without_ cross validation

# This is the actual training phase:
#
# for cross validation, we train the data on 4 different models by splitting the dataset in 4 subsets.

# +
feature_columns = [tf.feature_column.numeric_column("X", shape=(1000, 20))]

num_hidden_units = [512, 256, 128]
    
model = tf.estimator.DDN(feature_columns=feature_columns,
                                       n_classes=10,
                                       model_dir="./checkpoints_tutorial17-1/")
# -

# giving an error, but at least we've fixed it
for i in range(1,5):
    x_part, y_part, x_val, y_val = x_train[np.where(partition != i)], y_train[np.where(partition != i)], x_train[np.where(partition == i)], y_train[np.where(partition == i)] 
    
    train_input_fn = tf.estimator.inputs.numpy_input_fn(x={'X':x_part},
                                                   y=y_part,
                                                   num_epochs=1,
                                                   batch_size=128,
                                                   shuffle=False)
    
    model.train(input_fn=train_input_fn, steps=10)

final_test_input_fn = tf.estimator.inputs.numpy_input_fn(x={'X':x_test},
                                                   y=y_test,
                                                   num_epochs=20,
                                                         batch_size=128,
                                                   shuffle=False)


# +
# the model function
def cnn_clocator(inputs, classes, mode):
    
    conv1 = tf.layers.conv2d(inputs=inputs, filters=50, kernel_size=3,
                             padding="same", activation=tf.nn.relu)
    
    conv2 = tf.layers.conv2d(inputs=conv1, filters=80, kernel_size=5,
                             padding="same", activation=tf.nn.relu)
    
    pool = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
    
    pool_flat = tf.layers.flatten(pool)
    
    
    
# -

len(x_train[np.where(partition == 1)]

# +
train_input_fn = tf.estimator.inputs.numpy_input_fn(x={'X':x_train[np.where(partition == 1)]},
                                                   y=y_train[np.where(partition == 1)],
                                                   num_epochs=1,
                                                   shuffle=False)
    
model.train(input_fn=train_input_fn, steps=10)
# -



StarcoderdataJupyterScriptsDedupFiltered