Theano / Theano

Theano was a Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently. It is being continued as PyTensor: www.github.com/pymc-devs/pytensor

Home Page:https://www.github.com/pymc-devs/pytensor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

theano.gradient.verify_grad lack of checking the validity of inputs

cheyennee opened this issue · comments

problem:
theano.gradient.verify_grad doesnt check the validity of inputs. In following snippet code, input_1 and input_alpha should have same shape. When I input them into theano.tensor.nnet.relu, this API throws error. While in theano.gradient.verify_grad, it outputs None without error.

repo code:

import theano
import theano.tensor as T
import numpy as np
def custom_activation(x, alpha):
    return theano.tensor.nnet.relu(x, alpha)

x1 = T.tensor3('x1')
alpha = T.tensor4('alpha')

output = custom_activation(x1, alpha)
loss = T.sum(output ** 2)
grad_x1, grad_alpha = T.grad(loss, [x1, alpha])
input_1 = np.random.random((1, 2, 3)).astype('float32')
input_alpha = np.random.random((1, 2, 1, 1)).astype('float32')

rng = np.random.RandomState(123)
print(theano.gradient.verify_grad(custom_activation, pt=[input_1, input_alpha], rng=rng))

Same problem can be found in theano.tensor.nnet.nnet.binary_crossentropy.
repo code:

import theano
import theano.tensor as T
import numpy as np
def custom_activation(output, target):
    return theano.tensor.nnet.nnet.binary_crossentropy(output, target)
output = T.tensor4('output')
target = T.tensor4('target')
output = custom_activation(output, target)
loss = T.sum(output ** 2)
grad_inputs, grad_gamma = T.grad(loss, [output, target])
output_data = np.random.random((1, 1, 1, 1)).astype('float32')
target_data = np.random.random((1, 2, 3, 1)).astype('float32')
rng = np.random.RandomState(123)
print(theano.gradient.verify_grad(custom_activation, pt=[output_data, target_data], rng=rng))