# Path: dlearn/models/block.py
# class Block(object):
# 
#     """Construct a base class to represent an input-output black box.
# 
#     Attributes
#     ----------
#     parameters : list of theano.tensor
#         The block parameters. *Read-only*.
#     input : theano.tensor or list of theano.tensor
#         The input symbol(s) of the block. *Read-only*.
#     output : theano.tensor or list of theano.tensor
#         The output symbol(s) of the block. *Read-only*.
#     dropout_input : None, theano.tensor, or list of theano.tensor
#         None if no dropout input symbol is specified, and the `input` will be
#         used as `dropout_input`. *Read-only*.
#     dropout_output : None, theano.tensor, or list of theano.tensor
#         None if no dropout for this layer, and the `output` will be used as
#         `dropout_output`. *Read-only*.
# 
#     Parameters
#     ----------
#     input : theano.tensor or list of theano.tensor
#         The input symbol(s) of the block.
#     dropout_input : None, theano.tensor, or list of theano.tensor, optional
#         None if no dropout input symbol is specified, and the `input` will be
#         used as `dropout_input`. Default is None.
# 
#     """
# 
#     def __init__(self, input, dropout_input=None):
#         super(Block, self).__init__()
# 
#         self._params = []
#         self._input = input
#         self._output = input
# 
#         if dropout_input is None:
#             self._dropout_input = self._input
#         else:
#             self._dropout_input = dropout_input
# 
#         self._dropout_output = self._dropout_input
# 
#     @property
#     def parameters(self):
#         return self._params
# 
#     @property
#     def input(self):
#         return self._input
# 
#     @property
#     def output(self):
#         return self._output
# 
#     @property
#     def dropout_input(self):
#         return self._dropout_input
# 
#     @property
#     def dropout_output(self):
#         return self._dropout_output
# 
# Path: dlearn/utils/actfuncs.py
# def identity(x):
# def relu(x):
# 
# Path: dlearn/utils/math.py
# def create_shared(x):
# def create_empty(p):
# def dropout(x, ratio):


import numpy as np
import theano
import theano.tensor as T
from theano.tensor.signal.downsample import max_pool_2d
    from theano.sandbox.cuda.basic_ops import gpu_contiguous
    from pylearn2.sandbox.cuda_convnet.filter_acts import FilterActs
from .block import Block
from ..utils import actfuncs
from ..utils.math import nprng, dropout
                    pad = self._filter_shape[2] - 1

                conv_op = FilterActs(stride=1, partial_sum=1, pad=pad)
                x = gpu_contiguous(x.dimshuffle(1, 2, 3, 0))
                W = gpu_contiguous(self._W.dimshuffle(1, 2, 3, 0))
                z = conv_op(x, W).dimshuffle(3, 0, 1, 2)

            if self._pool_shape != (1, 1):
                z = max_pool_2d(
                    input=z, ds=self._pool_shape,
                    ignore_border=True)

            if isinstance(self._b, float):
                z = z + self._b
            else:
                z = z + self._b.dimshuffle('x', 0, 'x', 'x')

            if self._active_func is not None:
                z = self._active_func(z)

            if self._flatten:
                z = z.flatten(2)

            return z

        self._output = f(self._input)
        self._dropout_output = f(self._dropout_input)

        if self._dropout_ratio is not None and self._dropout_ratio > 0:
            self._output = (1.0 - self._dropout_ratio) * self._output

# Path: dlearn/models/block.py
# class Block(object):
# 
#     """Construct a base class to represent an input-output black box.
# 
#     Attributes
#     ----------
#     parameters : list of theano.tensor
#         The block parameters. *Read-only*.
#     input : theano.tensor or list of theano.tensor
#         The input symbol(s) of the block. *Read-only*.
#     output : theano.tensor or list of theano.tensor
#         The output symbol(s) of the block. *Read-only*.
#     dropout_input : None, theano.tensor, or list of theano.tensor
#         None if no dropout input symbol is specified, and the `input` will be
#         used as `dropout_input`. *Read-only*.
#     dropout_output : None, theano.tensor, or list of theano.tensor
#         None if no dropout for this layer, and the `output` will be used as
#         `dropout_output`. *Read-only*.
# 
#     Parameters
#     ----------
#     input : theano.tensor or list of theano.tensor
#         The input symbol(s) of the block.
#     dropout_input : None, theano.tensor, or list of theano.tensor, optional
#         None if no dropout input symbol is specified, and the `input` will be
#         used as `dropout_input`. Default is None.
# 
#     """
# 
#     def __init__(self, input, dropout_input=None):
#         super(Block, self).__init__()
# 
#         self._params = []
#         self._input = input
#         self._output = input
# 
#         if dropout_input is None:
#             self._dropout_input = self._input
#         else:
#             self._dropout_input = dropout_input
# 
#         self._dropout_output = self._dropout_input
# 
#     @property
#     def parameters(self):
#         return self._params
# 
#     @property
#     def input(self):
#         return self._input
# 
#     @property
#     def output(self):
#         return self._output
# 
#     @property
#     def dropout_input(self):
#         return self._dropout_input
# 
#     @property
#     def dropout_output(self):
#         return self._dropout_output
# 
# Path: dlearn/utils/actfuncs.py
# def identity(x):
# def relu(x):
# 
# Path: dlearn/utils/math.py
# def create_shared(x):
# def create_empty(p):
# def dropout(x, ratio):


# Path: dlearn/models/layer.py
import numpy as np
import theano
import theano.tensor as T
from theano.tensor.signal.downsample import max_pool_2d
    from theano.sandbox.cuda.basic_ops import gpu_contiguous
    from pylearn2.sandbox.cuda_convnet.filter_acts import FilterActs
from .block import Block
from ..utils import actfuncs
from ..utils.math import nprng, dropout

                    pad = self._filter_shape[2] - 1

                conv_op = FilterActs(stride=1, partial_sum=1, pad=pad)
                x = gpu_contiguous(x.dimshuffle(1, 2, 3, 0))
                W = gpu_contiguous(self._W.dimshuffle(1, 2, 3, 0))
                z = conv_op(x, W).dimshuffle(3, 0, 1, 2)

            if self._pool_shape != (1, 1):
                z = max_pool_2d(
                    input=z, ds=self._pool_shape,
                    ignore_border=True)

            if isinstance(self._b, float):
                z = z + self._b
            else:
                z = z + self._b.dimshuffle('x', 0, 'x', 'x')

            if self._active_func is not None:
                z = self._active_func(z)

            if self._flatten:
                z = z.flatten(2)

            return z

        self._output = f(self._input)
        self._dropout_output = f(self._dropout_input)

        if self._dropout_ratio is not None and self._dropout_ratio > 0:
            self._output = (1.0 - self._dropout_ratio) * self._output

            self._dropout_output = dropout(self._dropout_output,