我正在使用theano及其LeNet教程,使用街景Google图像数据集训练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]
错误本身说(afaiu)我正在尝试将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的尺寸一致。在我的代码中,它们都是一维向量。 为什么切片会导致错误?切片会创建矩阵吗?更重要的是,我该如何解决这个问题? 提前谢谢!
提前感谢您的帮助!
答案 0 :(得分:1)
展平你的阵列
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')
答案 1 :(得分:0)
您的输入数据看起来很奇怪,具体而言,标签显示为矩阵,而不是矢量。
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)
为什么所有这些在最后都有一个额外尺寸的尺寸?
我认为您需要更改数据加载代码,以便打印
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
是向量而不是矩阵。