【问题标题】:TypeError: Cannot convert Type TensorType(float64, vector) (of Variable Subtensor{int64:int64:}.0) into Type TensorType(float64, matrix)TypeError:无法将类型 TensorType(float64,vector)(变量 Subtensor{int64:int64:}.0)转换为类型 TensorType(float64,matrix)
【发布时间】:2016-01-29 17:59:39
【问题描述】:

我正在使用 theano 和他们的 LeNet 教程使用街景谷歌图像数据集训练 CNN。

我加载数据集:

train_set_x, train_set_y, \
valid_set_x, valid_set_y, \
test_set_x, test_set_y = manager.get_grayscale_data_dim()

我打印了尺寸:

self.train_data_dims (70000, 1024, 1)
self.train_labels_dims (70000, 1)
self.valid_data_dims (3250, 1024, 1)
self.valid_labels_dims (3250, 1)
self.test_data_dims (26000, 1024, 1)
self.test_labels_dims (26000, 1)

然后我按照教程做:

# allocate symbolic variables for the data
index = T.lscalar()  # index to a [mini]batch
x = T.matrix('x')   # the data is presented as rasterized images
y = T.ivector('y')  # the labels are presented as 1D vector of

print "y.type", y.type, "y.ndim", y.ndim
print "test_set_y", test_set_y.type, "test_set_y.ndim", test_set_y.ndim

打印结果:

y.type TensorType(int64, vector) y.ndim 1
test_set_y TensorType(int64, vector) test_set_y.ndim 1

我在这里遇到了问题(这是定义的第一个函数,接下来是验证和训练,只是为了让名称不会让您感到困惑,并且您不会认为使用类似的训练和验证功能一切正常):

创建一个函数来计算模型所犯的错误

test_model = theano.function(
    [index],
    layer3.errors(y),
    givens={
        x: test_set_x[index * batch_size: (index + 1) * batch_size],
        y: test_set_y[index * batch_size: (index + 1) * batch_size]
    }
)

准确地说,我有一个错误:

y: test_set_y[index * batch_size: (index + 1) * batch_size]

错误本身表明 (afau) 我正在尝试将 smth 转换为矩阵。 这是在哪里发生的,我想知道?

TypeError: Cannot convert Type TensorType(float64, vector) (of Variable Subtensor{int64:int64:}.0) into Type TensorType(float64, matrix). You can try to manually convert Subtensor{int64:int64:}.0 into a TensorType(float64, matrix).

AFAIU,y 和 test_set_y 的维度一致。在我的代码中,它们都是一维向量。 为什么切片会出错?切片会创建矩阵吗?更重要的是,我该如何解决这个问题? 提前致谢!

提前感谢您的帮助!

【问题讨论】:

    标签: matrix machine-learning neural-network theano conv-neural-network


    【解决方案1】:

    您的输入数据看起来很奇怪,具体来说,标签显示为矩阵,而不是向量。

    self.train_data_dims (70000, 1024, 1)
    self.train_labels_dims (70000, 1)
    self.valid_data_dims (3250, 1024, 1)
    self.valid_labels_dims (3250, 1)
    self.test_data_dims (26000, 1024, 1)
    self.test_labels_dims (26000, 1)
    

    为什么所有这些最后都有一个大小为 1 的额外维度?

    我认为您需要更改数据加载代码以便打印

    self.train_data_dims (70000, 1024, 1)
    self.train_labels_dims (70000, )
    self.valid_data_dims (3250, 1024, 1)
    self.valid_labels_dims (3250, )
    self.test_data_dims (26000, 1024, 1)
    self.test_labels_dims (26000, )
    

    这确保了,例如,test_labels 是一个向量而不是一个矩阵。

    【讨论】:

    • 我为self.train_data_dims (70000, 1024, 1) 和其他变量添加了一个额外的维度,以使其与尺寸为 (70000, 1024, 3) 的 RGB 图像情况一致,最后一个对应于 RGB 通道。此外,在他们提供的教程中,他们以某种方式重塑 X 矩阵:layer0_input = x.reshape((batch_size, 1, image_rows, images_cols)),其中1 指定图像的深度。我试过0,没用。此外,卷积本身是在图像的整个深度上执行的(在 RGB 情况下深度 = 3),AFAIK。
    • 为什么标签向量不能是大小为 (nx1) 的矩阵?无论如何,我得到的标签尺寸是1。那么标签不是已经被视为向量了吗?即使在代码的开头,它们看起来像是在 (nx1) 矩阵中,我也不太理解这种矩阵和向量之间的区别,除了后者指定的维度少了 1..
    • 好的,所以不需要更改数据本身,但标签确实需要是一个向量。在 Theano 中,形状为 (1, N)(N, 1) 的张量是一个矩阵(即它有 2 个维度;大小为 1 的张量是无关紧要的)。由于您的代码包含y = T.ivector('y'),因此需要将标签显示为向量,而不是矩阵。
    • test_set_y.type TensorType(int32, vector)y.type TensorType(int32, vector) 属于同一类型。但是,这行代码会导致错误:y: test_set_y_vec[index * batch_size: (index + 1) * batch_size]。我解决不了。我已经尝试将 test_set_y 明确声明为 T.iscalarfill 来设置值。不能解决问题。
    • 您能否将真实数据替换为随机值,并将您的脚本减少到仍然存在问题的最小数量,以便在此处完整发布?
    【解决方案2】:

    扁平化你的数组

     y_out = theano.shared(np.asarray(y, dtype=theano.config.floatX), borrow=True)
     y_out = y_out.flatten()
     y_out = T.cast(y_out, 'int32')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 2016-08-05
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多