I'm trying to perform elementwise gradient with
e.g.,
output-f(x): 5 by 1 vector,
with respect to input-X: 5 by 1 vector
I can do this like,
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 the result is
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's not efficient, i want to get 5 by 1 vector as a result
by 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 doesn't work.
Is there any way of do this? (sorry for bad format..I'm new here and learning)
(I added more clear example):
given input vector: x[1], x[2], ..., x[n]
and output vector: y[1], y[2], .., y[n],
where y[i] = f(x[i]).
I want the result of
df(x[i])/dx[i] only
and not the
df(x[i])/dx[j] for (i<>j)
, for computational efficiency (n is number of data > 10000)