Search code examples
theano

Making a matrix from several vectors


Is it possible to make a Matrix out of several vectors in theano?

like:

vector1, vector2, vector3 = theano.tensor.vector()
Matrix = [vector1, vector2, vector3]

similar to the numpy operation:

Matrix = numpy.asarray([vector1, vector 2, vector3])

Solution

  • You can use theano.tensor.stack.

    Here's a working example:

    import theano
    import theano.tensor as tt
    
    vector1, vector2, vector3 = tt.vectors(3)
    matrix = tt.stack(vector1, vector2, vector3)
    f = theano.function([vector1, vector2, vector3], matrix)
    print f([1, 2, 3], [4, 5, 6], [7, 8, 9])
    

    where prints

    [[ 1.  2.  3.]
     [ 4.  5.  6.]
     [ 7.  8.  9.]]