1

Let's say, one vector is [1, 2, 3, 4] and the other one is [1, 2, 3, 4], so the result should be [1, 4, 9, 16]. if I want to write the dot product of these two vectors in theano, how can I achieve this using Scan ?

This is my code , however, the result is shown as the diagonal of the result matrix.

v1 = T.dvector('v1')
v2 = T.dvector('v2')
def myFunc(i, v1, v2, res):
    subtensor = res[i]
    return T.set_subtensor(subtensor, v1[i]*v2[i])

result, updates = theano.scan(fn=myFunc,
                              sequences=T.arange(v1.shape[0]),
                              non_sequences=[v1, v2],
                              outputs_info=v1
                              )
func = theano.function(inputs=[v1, v2], outputs=result, updates=updates)
vec1 = np.asarray([1,2,3,4])
vec2 = np.asarray([1,2,3,4])

vec3 = func(vec1, vec2)

print(vec3) 

This is the result:

[[  1.   2.   3.   4.]
 [  1.   4.   3.   4.]
 [  1.   2.   9.   4.]
 [  1.   2.   3.  16.]]
tqjustc
  • 3,624
  • 6
  • 27
  • 42

2 Answers2

0

Just figure it out by myself. See the answer:

import pdb
import theano
import theano.tensor as T
import numpy as np

vec1 = T.dvector('vec1')
vec2 = T.dvector('vec2')

def oneStep(i, vec1, vec2):
    return vec1[i] * vec2[i]

result, updates = theano.scan(fn=oneStep, sequences=T.arange(vec1.shape[0]), non_sequences=[vec1, vec2])

fn1 = theano.function(
    inputs=[vec1, vec2],
    outputs=result,
    updates=updates
)


v1 = np.random.random((1,3)).flatten()
v2 = np.random.random((1,3)).flatten()
print(fn1(v1,v2))
print(v1*v2)
tqjustc
  • 3,624
  • 6
  • 27
  • 42
0

You can use theano.tensor.dot for dot product of two vectors. If you still want to use theano.scan, the code below would be a simple approach specific to your question.

import theano
import theano.tensor as T
import numpy as np
v1 = T.dvector('v1')
v2 = T.dvector('v2')
result, updates = theano.scan(fn=lambda a,b:a*b,
                          sequences=[v1, v2]
                          )
 func = theano.function(inputs=[v1, v2], outputs=result)
 vec1 = np.asarray([1,2,3,4])
 vec2 = np.asarray([1,2,3,4])
 vec3 = func(vec1, vec2)
 print(vec3)