Python 带有Theano的奇怪类型错误

Python 带有Theano的奇怪类型错误,python,neural-network,theano,deep-learning,conv-neural-network,Python,Neural Network,Theano,Deep Learning,Conv Neural Network,这真的很奇怪,因为Jupyter中没有抛出错误,但它需要很长时间才能运行,最后内核停止运行,我真的不知道为什么,但当我将它移动到shell并运行python fileName.py时,抛出了错误。问题在于来自scipy的loadmat。您得到的typeerror是由以下代码引发的: 如果未对齐data.flags.aligned: ... 引发类型错误。。。 现在,当您从原始数据在numpy中创建新数组时,它通常是对齐的: import scipy.io as sio import numpy

这真的很奇怪,因为Jupyter中没有抛出错误,但它需要很长时间才能运行,最后内核停止运行,我真的不知道为什么,但当我将它移动到shell并运行python fileName.py时,抛出了错误。

问题在于来自scipy的loadmat。您得到的typeerror是由以下代码引发的:

如果未对齐data.flags.aligned: ... 引发类型错误。。。 现在,当您从原始数据在numpy中创建新数组时,它通常是对齐的:

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})
但是,如果您将其保存为savemat/loadmat,则标志的值将丢失:

>>> a = np.array(2)
>>> a.flags.aligned
True
这个问题似乎已经讨论过了

解决此问题的一种快速而肮脏的方法是从加载的数组创建一个新的numpy数组:

>>> savemat('test', {'a':a})
>>> a2 = loadmat('test')['a']
>>> a2.flags.aligned
False
因此,对于您的代码:

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

非常感谢。这很有趣。
>>> a2 = loadmat('test')['a']
>>> a3 = np.array(a2)
>>> a3.flags.aligned
True
dataReadyForCNN = np.array(sio.loadmat("DataReadyForCNN.mat"))