BenJ
BenJ

Reputation: 456

Adding a row of ones to a tensor in Theano

I am new to theano and am trying to figure out how to add a row of ones to a matrix tensor where the first shape of the input tensor will vary slightly e.g. (50000, 784), (10000, 784) using MNIST. Taking a numpy example; this is what I would like to achieve in theano, where array would be a tensor:

array = np.zeros((2,2))
array = np.hstack((np.ones((2,1)), array))

I have looked at shape_padleft as well as pad_right and padaxis but have been unable to achieve the desired result.

Thanks very much for any help! Ben

Upvotes: 0

Views: 741

Answers (1)

BenJ
BenJ

Reputation: 456

So in answering my own question ;) I hope this helps anyone else!

A = T.matrix('A')
B = T.ones_like(A[:,0])
C = T.concatenate([T.shape_padright(B), A], axis=1)

C.eval({A:np.zeros((2,2))})

>> array([[1., 0., 0.],
          [1., 0., 0.]])

Upvotes: 3

Related Questions