Search code examples
pythontheano

How to combine two tensor in theano


I am wondering how is it possible to combine two variables, similar to append in python? For example, we have two variables (after feeding with data):

x: with size 1*3 y: with size 1*3

now I want to have a variable, which combine x and y to a size of 1*3*2

Thanks,


Solution

  • One can use theano.tensor.stack to achieve this. Here's a working example:

    import theano
    import theano.tensor as tt
    
    x = tt.matrix()
    y = tt.matrix()
    z = tt.stack([x, y], axis=2)
    f = theano.function([x, y], z)
    print f([[1, 2, 3]], [[4, 5, 6]])
    

    which prints

    [[[ 1.  4.]
      [ 2.  5.]
      [ 3.  6.]]]