这是一个奇怪的类型错误

2024-03-29 10:38:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是我的密码:

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})

这真的很奇怪,因为这个错误没有在Jupyter中抛出(但是运行需要很长时间,最后内核关闭了,我真的不知道为什么),但是当我把它移到shell并运行python fileName.py时,这个错误被抛出了。你知道吗


Tags: infrompyimportinputevallinetheano
1条回答
网友
1楼 · 发布于 2024-03-29 10:38:27

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

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

现在,当您在numpy中从原始数据创建一个新数组时,它通常是对齐的:

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

但是如果savemat/loadmat它,标志的值就会丢失:

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

(这个问题似乎已经讨论过了here

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

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

因此,对于您的代码:

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

相关问题 更多 >