Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Theano Reshaping

Tags:

reshape

theano

I am unable to clearly comprehend theano's reshape. I have an image matrix of shape:

    [batch_size, stack1_size, stack2_size, height, width]

, where there are stack2_size stacks of images, each having stack1_size of channels. I now want to convert them into the following shape:

    [batch_size, stack1_size*stack2_size, 1 , height, width]

such that all the stacks will be combined together into one stack of all channels. I am not sure if reshape will do this for me. I see that reshape seems to not lexicographically order the pixels if they are mixed in dimensions in the middle. I have been trying to achieve this with a combination of dimshuffle,reshape and concatenate, but to no avail. I would appreciate some help.

Thanks.

like image 304
Ragav Venkatesan Avatar asked Aug 04 '15 00:08

Ragav Venkatesan


1 Answers

Theano reshape works just like numpy reshape with its default order, i.e. 'C':

‘C’ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest.

Here's an example showing that the image pixels remain in the same order after a reshape via either numpy or Theano.

import numpy
import theano
import theano.tensor


def main():
    batch_size = 2
    stack1_size = 3
    stack2_size = 4
    height = 5
    width = 6
    data = numpy.arange(batch_size * stack1_size * stack2_size * height * width).reshape(
        (batch_size, stack1_size, stack2_size, height, width))
    reshaped_data = data.reshape([batch_size, stack1_size * stack2_size, 1, height, width])
    print data[0, 0, 0]
    print reshaped_data[0, 0, 0]

    x = theano.tensor.TensorType('int64', (False,) * 5)()
    reshaped_x = x.reshape((x.shape[0], x.shape[1] * x.shape[2], 1, x.shape[3], x.shape[4]))
    f = theano.function(inputs=[x], outputs=reshaped_x)
    print f(data)[0, 0, 0]


main()
like image 70
Daniel Renshaw Avatar answered Oct 10 '22 01:10

Daniel Renshaw