Uylenburgh
Uylenburgh

Reputation: 1307

TypeError: Cannot convert Type TensorType(float64, vector) (of Variable Subtensor{int64:int64:}.0) into Type TensorType(float64, matrix)

I am using theano and their LeNet tutorial to train CNN with the dataset of Street View Google Images.

I load the dataset:

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

I get the dimensions printed:

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)

Then I do as in tutorial:

# 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

Result printed:

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

I get the problem here (this is the first function defined, next is validation and traning, just so that the name does not confuse you, and you don't think that everything went OK with similar training and validation functions):

create a function to compute the mistakes that are made by the model

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

Precisely, I have an error in line:

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

The error itself says (afaiu) that I am trying to convert smth to matrix. Where is this happening, I wonder?

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, the dimensions of y and test_set_y agree. In my code, they are both 1D vectors. Why does slicing cause error? Does slicing create matrix? And more importantly, how do I solve this problem? Thanks in advance!

Thank you in advance for your help!

Upvotes: 2

Views: 6940

Answers (2)

Linda MacPhee-Cobb
Linda MacPhee-Cobb

Reputation: 7856

Flatten your array

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

Upvotes: 1

Daniel Renshaw
Daniel Renshaw

Reputation: 34177

Your input data looks strange and, specifically, the labels are presented as matrices, not vectors.

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)

Why do all of these have an extra dimension of size one at the end?

I think you need to change your data loading code so this prints

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

This ensure that, for example, test_labels is a vector instead of a matrix.

Upvotes: 0

Related Questions