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.]]