theano.gradient.verify_grad throws error if the rng parameter is not provided
Author: cheyenneeCreated Dec 4, 2023Updated Dec 4, 2023
problem: Theano's verify_grad function raises an error if the rng parameter is not provided. While the documentation indicates that rng is optional, the source code sets it to None if a value is not explicitly passed, resulting in an error. To address this inconsistency, either the rng parameter should be made mandatory or a default value, such as rng = np.random.RandomState(123), should be provided in the source code.
repo code:
import theano
import theano.tensor as T
import numpy as np
def custom_activation(X, Y):
return theano.tensor.dot(X, Y)
X = T.tensor3('x')
Y = T.tensor3('y')
output = custom_activation(X, Y)
loss = T.sum(output ** 2)
grad_x = T.grad(loss, X)
x_data = np.random.random((5, 5, 5)).astype('float32')
y_data = np.random.random((5, 5, 5)).astype('float32')
print(theano.gradient.verify_grad(custom_activation, pt=[x_data, y_data]))output: TypeError: rng should be a valid instance of numpy.random.RandomState. You may want to use theano.tests.unittest_tools.verify_grad instead of theano.gradient.verify_grad.
version: python:3.6 theano:1.0.4
Source: Theano/Theano