I am trying to perform an elemental gradient with
eg.
output-f (x): 5 on 1 vector,
relative to input-X: 5 on 1 vector
I can do it for example
import theano
import theano.tensor as T
X = T.vector('X')
f = X*3
[rfrx, []] = theano.scan(lambda j, f,X : T.grad(f[j], X), sequences=T.arange(X.shape[0]), non_sequences=[f,X])
fcn_rfrx = theano.function([X], rfrx)
fcn_rfrx(np.ones(5,).astype(float32))
and result
array([[ 3., 0., 0., 0., 0.],
[ 0., 3., 0., 0., 0.],
[ 0., 0., 3., 0., 0.],
[ 0., 0., 0., 3., 0.],
[ 0., 0., 0., 0., 3.]], dtype=float32)
but since it is inefficient, I want to get 5 on 1 vector as a result
doing something like ..
[rfrx, []] = theano.scan(lambda j, f,X : T.grad(f[j], X[j]), sequences=T.arange(X.shape[0]), non_sequences=[f,X])
which does not work.
Is there any way to do this? (sorry for the poor format .. I'm new here and studied)
(I added a clearer example):
given input vector: x [1], x [2], ..., x [n]
and the output vector: y [1], y [2], .., y [n],
where y [i] = f (x [i]).
I want to get the result
df (x [i]) / dx [i] only
but not
df (x [i]) / dx [j] for (i <> j)
for computational efficiency (n - amount of data> 10000)