1

I would like to know how to add each element in two vectors with Theano?

Assume that we have two vector vector_1 and vecotr_2, and we would like to construct a matrix A, where

A[i][j] = vector_1[i] + vecotr_2[j]

I know that in numpy we can use list comprehension. But i would like to use Theano obtain the result with less time. It seems that Theano.scan() can do this job, but i really don't know how to deal with it.

z.Lam
  • 11
  • 3
  • I implement this code with theano at [link](http://stackoverflow.com/users/6845486/z-lam). And numpy.add is much faster in my computer. – z.Lam Oct 23 '16 at 08:37

1 Answers1

0

You can make use of broadcasting. Here is an example in NumPy, you can do the same in Theano:

>>> import numpy as np
>>> x1 = np.array([1,1,9]).reshape((3,1))
>>> x2 = np.array([0,3,4]).reshape((1,3))
>>> np.add(x1, x2)
array([[ 1,  4,  5],
       [ 1,  4,  5],
       [ 9, 12, 13]])
Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
  • Thank you so much for answering. Your answer is very helpful. I decided to use numpy since it is fast enough :) – z.Lam Sep 19 '16 at 07:51