Qiongjie Tian
2017-05-23 04:14:55 UTC
I'm learning the scan in theano. It is somehow confusing to me.
Let's say, I want to use Scan to compute the dot multiplication of two
vectors. One is [1, 2, 3, 4] and the other is [1, 2, 3, 4]. The product
should be [1, 4, 9, 16]. I use the following codes. However, the result is
shown as the diagonal of the result matrix, instead of one vector.
Could anyone help me change the codes?
Thank you
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.]]
Let's say, I want to use Scan to compute the dot multiplication of two
vectors. One is [1, 2, 3, 4] and the other is [1, 2, 3, 4]. The product
should be [1, 4, 9, 16]. I use the following codes. However, the result is
shown as the diagonal of the result matrix, instead of one vector.
Could anyone help me change the codes?
Thank you
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.]]
--
---
You received this message because you are subscribed to the Google Groups "theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
---
You received this message because you are subscribed to the Google Groups "theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.