0

Getting output classification with Lasagne/Theano

I am migrating my code from pure Theano to Lasagne. I had this certain code from a tutorial to get the result of a prediction with a certain data and I would generate a csv file to send to kaggle. But with lasagne, it doesn't work. I have tried several things but they all give errors.

I would love if anyone could help me figure what's wrong!

I pasted the whole code here : http://pastebin.com/e7ry3280

test_data  = np.loadtxt("../inputData/test.csv", dtype=np.uint8, delimiter=',', skiprows=1)

# The inputs are vectors now, we reshape them to monochrome 2D images,
# following the shape convention: (examples, channels, rows, columns)
data = data.reshape(-1, 1, 28, 28)
test_data = test_data.reshape(-1, 1, 28, 28)

index = T.lscalar()  # index to a [mini]batch
preds = []
for it in range(len(test_data)):
        test_data = test_data[it]
        N = len(test_data)
        # print "N : ", N
        test_data = theano.shared(np.asarray(test_data, dtype=theano.config.floatX))

        test_labels = T.cast(theano.shared(np.asarray(np.zeros(batch_size), dtype=theano.config.floatX)),'uint8')

        ###target_var
        #y = T.ivector('y')  # the labels are presented as 1D vector of [int] labels
        #index = T.lscalar()  # index to a [mini]batch

        ppm = theano.function([index],lasagne.layers.get_output(network, deterministic=True),
                              givens={
                                  input_var: test_data[index * batch_size: (index + 1) * batch_size],
                                  target_var: test_labels
                              }, on_unused_input='warn')

        p = [ppm(ii) for ii in range(N // batch_size)]

        p = np.array(p).reshape((N, 10))
        print (p)
        p = np.argmax(p, axis=1)
        p = p.astype(int)
        preds.append(p)

subm = np.empty((len(preds), 2))
subm[:, 0] = np.arange(1, len(preds) + 1)
subm[:, 1] = preds

np.savetxt('submission.csv', subm, fmt='%d', delimiter=',',header='ImageId,Label', comments='')

return preds

The code fails on the line that starts with ppm = theano.function...:

TypeError: Cannot convert Type TensorType(float32, 3D) (of Variable Subtensor{int64:int64:}.0) into Type TensorType(float32, 4D). You can try to manually convert Subtensor{int64:int64:}.0 into a TensorType(float32, 4D).

I'm just trying to input the test data to the CNN and get the results to a CSV file. How can I do it? I know I must use minibatches because the whole test data wont fit on the GPU.

Djizeus
  • 4,161
  • 1
  • 24
  • 42
KenobiBastila
  • 539
  • 4
  • 16
  • 52

1 Answers1

2

As pointed out by the error message and Daniel Renshaw in the comments, the problem is a mismatch of dimensions between test_data and input_var. On the first line on the loop, you write:

test_data = test_data[it]

Which turns the 4D array test_data into a 3D array with the same name (that is why using the same variable name for different types is never recommended :) ). After that you encapsulate it in a shared variable which doesn't change the dimension, and then you slice it to assign it to input_var, which again doesn't change the dimension.

If I understand your code, I think you should just remove that first line. That way test_data remains a list of examples, and you can slice it to make a batch.

Djizeus
  • 4,161
  • 1
  • 24
  • 42