# Path: sop_embed/layers.py
# class DeepConvolutionLayer(object):
#     """A deep convolution network: convolution, pooling, convolution, pooling,
#     ... . Can use it to build Transposed convolution by using unpooling
#     instead of pooling."""
#     def __init__(self, input, layers, crop_size):
#         """crope_size = [Din, hight_img, width_img]"""
#         self.input = input
#         self.x = input
#         self.input_features_dim = crop_size
#         self.layers = []
#         self.params = []
#         tmp_in = self.x
#         nbr_f_in, h, w = crop_size[0], crop_size[1], crop_size[2]
#         image_shape = (None, crop_size[0], h, w)
# 
#         for i in range(len(layers)):
#             layer = layers[i]
#             if layer["type"] == "conv":
#                 print "output filter shape", layer["filter_shape"][0]
#                 self.layers.append(ConvLayer(
#                     rng=layer["rng"],
#                     input=tmp_in,
#                     filter_shape=(layer["filter_shape"][0],
#                                   nbr_f_in,
#                                   layer["filter_shape"][1],
#                                   layer["filter_shape"][2]),
#                     image_shape=image_shape,
#                     activation=get_non_linearity_fn(layer["activation"]),
#                     padding=layer["padding"],
#                     W=layer["W"],
#                     b=layer["b"],
#                     b_v=layer["b_v"],
#                     stride=layer["stride"]))
#                 self.params.extend(self.layers[-1].params)
#                 tmp_in = self.layers[-1].output
#                 nbr_f_in = layer["filter_shape"][0]
# 
#                 h = (h-layer["filter_shape"][1] + 2 * layer["padding"][0] + 1)
#                 w = (w-layer["filter_shape"][2] + 2 * layer["padding"][1] + 1)
# #                if i < (len(layers) - 1):
# #                    assert layers[i+1]["type"] in ["upsample", "downsample"]
#             if layer["type"] == "downsample":
#                 self.layers.append(PoolingLayer(
#                     input=tmp_in,
#                     poolsize=layer["poolsize"],
#                     ignore_border=layer["ignore_border"],
#                     mode=layer["mode"]))
#                 self.params.extend(self.layers[-1].params)
#                 tmp_in = self.layers[-1].output
#                 # assert layers[i-1]["type"] in ["conv"]
#                 h = h / layer["poolsize"][0]
#                 w = w / layer["poolsize"][1]
#             if layer["type"] == "upsample":
#                 self.layers.append(UnPoolingLayer(
#                     input=tmp_in,
#                     ratio=layer["ratio"],
#                     use_1D_kernel=layer["use_1D_kernel"]))
#                 self.params.extend(self.layers[-1].params)
#                 tmp_in = self.layers[-1].output
#                 h = h * layer["ratio"]
#                 w = w * layer["ratio"]
#             image_shape = (None, nbr_f_in, h, w)
#         # End building.
#         self.output_features_dim = [nbr_f_in, h, w]
#         self.output = self.layers[-1].output
#         self.output_size_flatten = nbr_f_in * h * w
# 
# Path: sop_embed/extra.py
# def sharedX_value(value, name=None, borrow=None, dtype=None):
#     """Share a single value after transforming it to floatX type.
# 
#     value: a value
#     name: variable name (str)
#     borrow: boolean
#     dtype: the type of the value when shared. default: theano.config.floatX
#     """
#     if dtype is None:
#         dtype = theano.config.floatX
#     return theano.shared(
#         theano._asarray(value, dtype=dtype), name=name, borrow=borrow)


import theano
import theano.tensor as T
import numpy as np
import cPickle as pkl
from theano.tensor.shared_randomstreams import RandomStreams
from ae import Autoencoder, CostType, NonLinearity
from sop_embed.layers import DeepConvolutionLayer
from sop_embed.extra import sharedX_value
        self.params.extend(self.layers[0].params)
        code = self.layers[0].output
        output_features_dim = self.layers[0].output_features_dim
        # Decoder
        self.layers.append(
            DeepConvolutionLayer(input=code,
                                 layers=decoder_config,
                                 crop_size=output_features_dim))
        self.decoder = self.layers[1]
        self.output = self.layers[1].output
        self.params.extend(self.layers[1].params)
        self.L1 = 0
        self.L2 = 0
        self.L1_reg = l1_reg
        self.L2_reg = l2_reg
        for param in self.params:
            if ("w" in param.name) or ("W" in param.name):
                if l1_reg != 0.:
                    self.L1 += abs(param).sum()
                if l2_reg != 0.:
                    self.L2 += (param**2).sum()
            elif ("b" in param.name) or ("B" in param.name):
                if l1_reg != 0. and reg_bias:
                    self.L1 += abs(param).sum()
                if l2_reg != 0. and reg_bias:
                    self.L2 += (param**2).sum()
        if self.L1 != 0.:
            self.L1 *= sharedX_value(self.L1_reg, "l1_reg")
            print "Performing l1 reg. Lambda=", l1_reg
        if self.L2 != 0.:

# Path: sop_embed/layers.py
# class DeepConvolutionLayer(object):
#     """A deep convolution network: convolution, pooling, convolution, pooling,
#     ... . Can use it to build Transposed convolution by using unpooling
#     instead of pooling."""
#     def __init__(self, input, layers, crop_size):
#         """crope_size = [Din, hight_img, width_img]"""
#         self.input = input
#         self.x = input
#         self.input_features_dim = crop_size
#         self.layers = []
#         self.params = []
#         tmp_in = self.x
#         nbr_f_in, h, w = crop_size[0], crop_size[1], crop_size[2]
#         image_shape = (None, crop_size[0], h, w)
# 
#         for i in range(len(layers)):
#             layer = layers[i]
#             if layer["type"] == "conv":
#                 print "output filter shape", layer["filter_shape"][0]
#                 self.layers.append(ConvLayer(
#                     rng=layer["rng"],
#                     input=tmp_in,
#                     filter_shape=(layer["filter_shape"][0],
#                                   nbr_f_in,
#                                   layer["filter_shape"][1],
#                                   layer["filter_shape"][2]),
#                     image_shape=image_shape,
#                     activation=get_non_linearity_fn(layer["activation"]),
#                     padding=layer["padding"],
#                     W=layer["W"],
#                     b=layer["b"],
#                     b_v=layer["b_v"],
#                     stride=layer["stride"]))
#                 self.params.extend(self.layers[-1].params)
#                 tmp_in = self.layers[-1].output
#                 nbr_f_in = layer["filter_shape"][0]
# 
#                 h = (h-layer["filter_shape"][1] + 2 * layer["padding"][0] + 1)
#                 w = (w-layer["filter_shape"][2] + 2 * layer["padding"][1] + 1)
# #                if i < (len(layers) - 1):
# #                    assert layers[i+1]["type"] in ["upsample", "downsample"]
#             if layer["type"] == "downsample":
#                 self.layers.append(PoolingLayer(
#                     input=tmp_in,
#                     poolsize=layer["poolsize"],
#                     ignore_border=layer["ignore_border"],
#                     mode=layer["mode"]))
#                 self.params.extend(self.layers[-1].params)
#                 tmp_in = self.layers[-1].output
#                 # assert layers[i-1]["type"] in ["conv"]
#                 h = h / layer["poolsize"][0]
#                 w = w / layer["poolsize"][1]
#             if layer["type"] == "upsample":
#                 self.layers.append(UnPoolingLayer(
#                     input=tmp_in,
#                     ratio=layer["ratio"],
#                     use_1D_kernel=layer["use_1D_kernel"]))
#                 self.params.extend(self.layers[-1].params)
#                 tmp_in = self.layers[-1].output
#                 h = h * layer["ratio"]
#                 w = w * layer["ratio"]
#             image_shape = (None, nbr_f_in, h, w)
#         # End building.
#         self.output_features_dim = [nbr_f_in, h, w]
#         self.output = self.layers[-1].output
#         self.output_size_flatten = nbr_f_in * h * w
# 
# Path: sop_embed/extra.py
# def sharedX_value(value, name=None, borrow=None, dtype=None):
#     """Share a single value after transforming it to floatX type.
# 
#     value: a value
#     name: variable name (str)
#     borrow: boolean
#     dtype: the type of the value when shared. default: theano.config.floatX
#     """
#     if dtype is None:
#         dtype = theano.config.floatX
#     return theano.shared(
#         theano._asarray(value, dtype=dtype), name=name, borrow=borrow)


# Path: sop_embed/da.py
import theano
import theano.tensor as T
import numpy as np
import cPickle as pkl
from theano.tensor.shared_randomstreams import RandomStreams
from ae import Autoencoder, CostType, NonLinearity
from sop_embed.layers import DeepConvolutionLayer
from sop_embed.extra import sharedX_value

        self.params.extend(self.layers[0].params)
        code = self.layers[0].output
        output_features_dim = self.layers[0].output_features_dim
        # Decoder
        self.layers.append(
            DeepConvolutionLayer(input=code,
                                 layers=decoder_config,
                                 crop_size=output_features_dim))
        self.decoder = self.layers[1]
        self.output = self.layers[1].output
        self.params.extend(self.layers[1].params)
        self.L1 = 0
        self.L2 = 0
        self.L1_reg = l1_reg
        self.L2_reg = l2_reg
        for param in self.params:
            if ("w" in param.name) or ("W" in param.name):
                if l1_reg != 0.:
                    self.L1 += abs(param).sum()
                if l2_reg != 0.:
                    self.L2 += (param**2).sum()
            elif ("b" in param.name) or ("B" in param.name):
                if l1_reg != 0. and reg_bias:
                    self.L1 += abs(param).sum()
                if l2_reg != 0. and reg_bias:
                    self.L2 += (param**2).sum()
        if self.L1 != 0.:
            self.L1 *= sharedX_value(self.L1_reg, "l1_reg")
            print "Performing l1 reg. Lambda=", l1_reg
        if self.L2 != 0.:

            self.L2 *= sharedX_value(self.L2_reg, "l2_reg")