<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Construyendo AutoEncoders sobre MNIST con Learninspy
<img style="display
Step1:  Carga de datos
Step2:  <h2><center>## Modelado con un AutoEncoder ##</center></h2>
Selección de parámetros para el modelo
Step3:  Selección de parámetros para la optimización
Step4:  Fit de AE
Step5:  Resultados visuales
<ASSISTANT_TASK:>
Python Code:
from learninspy.core.model import NetworkParameters, NeuralNetwork
from learninspy.core.autoencoder import AutoEncoder, StackedAutoencoder
from learninspy.core.optimization import OptimizerParameters
from learninspy.core.stops import criterion
from learninspy.utils.data import StandardScaler, LocalLabeledDataSet, split_data, label_data, load_mnist
from learninspy.utils.evaluation import RegressionMetrics
from learninspy.utils.plots import *
from learninspy.context import sc
import matplotlib.pyplot as plt
%matplotlib inline
Explanation: Construyendo AutoEncoders sobre MNIST con Learninspy
<img style="display: inline;" src="../../docs/img/Learninspy-logo_grande2.png" width="300" />
Dependencias
End of explanation
print "Cargando base de datos de entrenamiento..."
t, train, valid = load_mnist()
# Tiro el primer dataset
t = None
train = LocalLabeledDataSet(train)
rows, cols = train.shape
print "Dimensiones: ", rows, " x ", cols
valid = LocalLabeledDataSet(valid)
rows, cols = valid.shape
print "Dimensiones: ", rows, " x ", cols
Explanation: Carga de datos
End of explanation
units = [784, 100]
net_params = NetworkParameters(units_layers=units, activation=['ReLU', 'Sigmoid'], 
                                    strength_l2=1e-4, strength_l1=3e-5,
                                    classification=False)
Explanation: <h2><center>## Modelado con un AutoEncoder ##</center></h2>
Selección de parámetros para el modelo
End of explanation
local_stops = [criterion['MaxIterations'](10),
               criterion['AchieveTolerance'](0.95, key='hits')]
global_stops = [criterion['MaxIterations'](20),
            criterion['AchieveTolerance'](0.95, key='hits')]
opt_params = OptimizerParameters(algorithm='Adadelta', 
                                 options={'step-rate': 1, 'decay': 0.995, 'momentum': 0.7, 'offset': 1e-8},
                                 stops=local_stops, merge_criter='w_avg')
Explanation: Selección de parámetros para la optimización
End of explanation
# Crear AutoEncoder simple
ae = AutoEncoder(net_params, dropout_in=0.0)
    
# Fit
hits_valid = ae.fit(train, valid, mini_batch=256, parallelism=4, valid_iters=10,
                     stops=global_stops, optimizer_params=opt_params, keep_best=False, reproducible=True)
Explanation: Fit de AE
End of explanation
n = 10  # how many digits we will display
plt.figure(figsize=(20, 4))
for i in range(n):
    # display original
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(valid.data[i].features.reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    # display reconstruction
    ax = plt.subplot(2, n, i + 1 + n)
    predict = ae.predict(valid.data[i].features).matrix
    plt.imshow(predict.reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()
from matplotlib.gridspec import GridSpec
n_units = 100 # (*)
n_input = 784 # (*)
plt.figure(figsize=(20, 20))
width, height = (10, 10) # (*)
gs = GridSpec(width, height)
w = ae.list_layers[0].weights.matrix # (*) np.array
# Reescala
w = w - np.mean(w)
x_max = []
for i in range(n_units):
    x_max.append(map(lambda j: w[i, j] / float(np.sqrt(sum([w_i_j ** 2 for w_i_j in w[i, ]]))),
                     range(n_input)))
    
for i in range(width):
    for j in range(height):
        x = x_max[i*(height) + j]
        
        ax = plt.subplot(gs[i, j])
        ax.imshow(np.array(x).reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
    
plt.show()
Explanation: Resultados visuales
End of explanation
<END_TASK>