Petyr Baelish
Petyr Baelish

Reputation: 21

Getting value of a tensor variable in Theano

I am a noob at Theano - so please be patient.

I'm trying to implement gradient clipping for a project. A code snippet is as below :

grads = tensor.grad(cost, wrt=itemlist(tparams))

grad_norm = tensor.sqrt(sum(map(lambda x: tensor.sqr(x).sum(), grads)))
grad_norm = tensor.sqrt(grad_norm)
print grad_norm.eval()

The error I receive is as follows :

theano.gof.fg.MissingInputError: ("An input of the graph, used to compute Shape(x), was not provided and not given a value.Use the Theano flag exception_verbosity='high',for more information on this error.", x) Please advice.

Upvotes: 2

Views: 2324

Answers (1)

Daniel Renshaw
Daniel Renshaw

Reputation: 34177

The problem is that you're calling eval without providing the input values.

Your code is incomplete so I'm guessing a bit but cost is probably a function of at least one input tensor (e.g. something like x = T.matrix()). That being the case, a value needs to be provided for x when the computation is executed. You execute the computation by calling eval and can provide a value for x like this:

print grad_norm.eval({x: numpy.random.randn(10, 20).astype(theano.config.floatX})

You need to make sure you provide a value for every symbolic input that, in this case, grad_norm is a function of (just add additional entries to the dictionary passed to eval). You also need to make sure the shapes of the values passed in the dictionary are appropriate for the computation (e.g. if there's a dot product with a shared variable, the multiplied axes must be of the same size).

Also, be aware, the eval method is intended primarily for debug purposes. You shouldn't use this feature in 'real' code. Instead, you should compile the computation using theano.function.

Upvotes: 2

Related Questions