import numpy as np
import theano.tensor as T
import theano
floatX='float32'
V=T.vector('V')
A=T.matrix('A')
y=T.tanh(T.dot(V,A))
results,updates=theano.scan(lambda i:T.grad(y[i],V),sequences=[T.arange(y.shape[0])])
compute_jac_v=theano.function([V,A],results,allow_input_downcast=True)
x=np.eye(5,dtype=theano.config.floatX)[0]
w=np.eye(5,3,dtype=theano.config.floatX)
w[2]=np.ones((3),dtype=theano.config.floatX)
print(compute_jac_v(x,w))
[[ 0.41997434 0. 0.41997434 0. 0. ]
[ 0. 1. 1. 0. 0. ]
[ 0. 0. 1. 0. 0. ]]