Strange TypeError with Theano
Question: 
    Traceback (most recent call last):
          File "test.py", line 37, in <module>
            print convLayer1.output.shape.eval({x:xTrain})
          File "/Volumes/TONY/anaconda/lib/python2.7/site-packages/theano/gof/graph.py", line 415, in eval
            rval = self._fn_cache[inputs](*args)
          File "/Volumes/TONY/anaconda/lib/python2.7/site-packages/theano/compile/function_module.py", line 513, in __call__
            allow_downcast=s.allow_downcast)
          File "/Volumes/TONY/anaconda/lib/python2.7/site-packages/theano/tensor/type.py", line 180, in filter
            "object dtype", data.dtype)
        TypeError
    

And here is my code:

    
    
    import scipy.io as sio
    import numpy as np
    import theano.tensor as T
    from theano import shared
    
    from convnet3d import ConvLayer, NormLayer, PoolLayer, RectLayer
    from mlp import LogRegr, HiddenLayer, DropoutLayer
    from activations import relu, tanh, sigmoid, softplus
    
    dataReadyForCNN = sio.loadmat("DataReadyForCNN.mat")
    
    xTrain = dataReadyForCNN["xTrain"]
    # xTrain = np.random.rand(10, 1, 5, 6, 2).astype('float64')
    xTrain.shape
    
    dtensor5 = T.TensorType('float64', (False,)*5)
    x = dtensor5('x') # the input data
    
    yCond = T.ivector()
    
    # input = (nImages, nChannel(nFeatureMaps), nDim1, nDim2, nDim3)
    
    kernel_shape = (5,6,2)
    fMRI_shape = (51, 61, 23)
    n_in_maps = 1 # channel
    n_out_maps = 5 # num of feature maps, aka the depth of the neurons
    num_pic = 2592
    
    layer1_input = x
    
    # layer1_input.eval({x:xTrain}).shape
    # layer1_input.shape.eval({x:numpy.zeros((2592, 1, 51, 61, 23))})
    
    convLayer1 = ConvLayer(layer1_input, n_in_maps, n_out_maps, kernel_shape, fMRI_shape, 
                           num_pic, tanh)
    
    print convLayer1.output.shape.eval({x:xTrain})
    

It is really weird as the error was not thrown in Jupyter (but it takes long
long time to run and finally the kernel is down I really don't know why), but
as I move it to the shell and run `python fileName.py` the error was thrown.


Answer: The problem lies in `loadmat` from `scipy`. The typeerror you are getting is
thrown by this code in Theano:

    
    
    if not data.flags.aligned:
        ...
        raise TypeError(...)
    

Now, when you create a new array in numpy from raw data, it would usually be
aligned:

    
    
    >>> a = np.array(2)
    >>> a.flags.aligned
    True
    

But if you `savemat` / `loadmat` it, the value of the flag gets lost:

    
    
    >>> savemat('test', {'a':a})
    >>> a2 = loadmat('test')['a']
    >>> a2.flags.aligned
    False
    

(seems like this particular issue is discussed
[here](https://mail.scipy.org/pipermail/scipy-user/2009-December/023646.html))

One quick and dirty way to address it is to create a new numpy array from the
array you loaded:

    
    
    >>> a2 = loadmat('test')['a']
    >>> a3 = np.array(a2)
    >>> a3.flags.aligned
    True
    

So, for your code:

    
    
    dataReadyForCNN = np.array(sio.loadmat("DataReadyForCNN.mat"))
    

