【问题标题】:Strange TypeError with TheanoTheano 的奇怪类型错误
【发布时间】:2016-07-19 04:45:48
【问题描述】:
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 并运行 @ 987654323@错误被抛出。

【问题讨论】:

    标签: python neural-network theano deep-learning conv-neural-network


    【解决方案1】:

    问题在于loadmat 来自scipy。您收到的类型错误是由 Theano 中的这段代码引发的:

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

    【讨论】:

    • 谢谢!这很有趣。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多