Does the Anan stack matrix materialize?

I have the following code that adds 2 matrices to a three-dimensional tensor.

import theano import theano.tensor as T A = T.matrix("A") B = theano.tensor.stack(A, A) f = theano.function(inputs=[A], outputs=B) print f([range(10)]*2) 

However, I do not know how many times I need to add the matrix in advance. For example, the fourth line of code could be:

 B = theano.tensor.stack(A, A, A) B = theano.tensor.stack(A, A, A, A) etc... 

Is there an anano function to duplicate a matrix n times:

 theano.some_function(A, 3) = theano.tensor.stack(A, A, A) 

Then I can pass this 3 as an argument to the anano f function. Is it possible? I watched the broadcast, but the broadcast does not explicitly change the dimension / stack.

+5
source share
3 answers

After a long and hard processing of theano documentation, I found a solution:

 import theano import theano.tensor as T A = T.matrix("A") B = [A] C = theano.tensor.extra_ops.repeat(B, 3, axis=0) f = theano.function(inputs=[A], outputs=C) print f([range(10)]*2) 

is equivalent to:

 import theano import theano.tensor as T A = T.matrix("A") B = theano.tensor.stack(A, A, A) f = theano.function(inputs=[A], outputs=B) print f([range(10)]*2) 

except that we can now select the number of repetitions programmatically as the second argument: theano.tensor.extra_ops.repeat

+2
source

Here is an example of using broadcast

 import theano import theano.tensor as T import numpy as np A = T.fmatrix() n = T.iscalar() ones = T.ones((n, 1, 1)) stackedA = ones * A[np.newaxis, :, :] f = theano.function([A, n], stackedA) a = np.arange(30).reshape(5, 6).astype('float32') nn = 3 r = f(a, nn) print r.shape # outputs (3, 4, 5) print (r == a[np.newaxis]).all() # outputs True 

This approach can help the compiler avoid interleaving if it can optimize this.

+2
source

I don't know about theano, but you can accomplish this using a list list and an unpack list:

 n = 5 B = theano.tensor.stack(*[A for dummy in range(n)]) 

which is equivalent to:

 B = theano.tensor.stack(A, A, A, A, A) 

What it is, it first creates a list with n copies of A , and then unpacks the list into separate arguments (see https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists ).

+1
source

All Articles