In [5]:
import numpy as np
import numpy.random as npr
import theano as th
import theano.tensor as T
import pylab as pl
%matplotlib inline

In [69]:
class AE():
    def __init__(self,dim=10,batch_size=10):
        self.dim = dim
        self.init_params()
        self.create_gradient_functions()
        self.batch_size=batch_size
        
    def init_params(self):
        self.W1 = npr.randn(self.dim,self.dim)
        self.W2 = npr.randn(self.dim,self.dim)
        self.b1 = npr.randn(self.dim)
        self.b2 = npr.randn(self.dim)
    
    def transform(self,x):
        return self.W1.dot(x) + self.b1
    
    def reconstruct(self,x):
        y = self.transform(x)
        return self.W2.dot(y) + self.b2
    
    def create_gradient_functions(self):
        W1,W2,x = T.dmatrices('W1','W2','x')
        b1,b2 = T.cols('b1','b2')
        hidden = T.nnet.sigmoid(T.dot(W1,x)+b1)
        
        reconstructed=T.dot(W2,hidden) + b2
        grad_var = [W1,W2,b1,b2]
        
        reconstruction_error = T.sum((x-reconstructed)**2)
        
        #self.reconstruct = th.function()
        
        derivatives = T.grad(reconstruction_error,grad_var)
        
        self.gradientfunction = th.function(grad_var + [x],derivatives,on_unused_input='ignore')
    
    def get_gradients(self,mini_batch):
        
    
    def iterate(self,data):
        N,dim = data.shape
        batches=np.arange(0,N,self.batch_size)
        if batches[-1] != N:
            batches = np.append(batches,N)
        for i in xrange(len(batches) - 2):
            mini_batch = data[batches[i]:batches[i+1]]
            total_grad = self.

In [70]:
ae = AE()

In [93]:
x = npr.randn(10)

In [94]:
np.sum((ae.reconstruct(x) - x)**2)


Out[94]:
1924.7402276858072

In [83]:
ae.gradientfunction(x)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-83-b7cef5938ac0> in <module>()
----> 1 ae.gradientfunction(x)

/Users/joshuafass/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/compile/function_module.pyc in __call__(self, *args, **kwargs)
    495                     try:
    496                         s.storage[0] = s.type.filter(arg, strict=s.strict,
--> 497                                 allow_downcast=s.allow_downcast)
    498 
    499                     except Exception, e:

/Users/joshuafass/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/tensor/type.pyc in filter(self, data, strict, allow_downcast)
    155             raise TypeError("Wrong number of dimensions: expected %s,"
    156                             " got %s with shape %s." % (self.ndim, data.ndim,
--> 157                                                         data.shape))
    158         if not data.flags.aligned:
    159             try:

TypeError: ('Bad input argument to theano function with name "<ipython-input-69-7f4d02e22e9a>:34"  at index 0(0-based)', 'Wrong number of dimensions: expected 2, got 1 with shape (10,).')

In [ ]: