So for the grad() you are also calling an outside program to compute it? Not sure if that’s allow in theano.
grad()