#6801·Theano

theano.tensor.nnet.conv3d verify_grad fail when border_mode is "full"

Author: cheyenneeCreated Dec 4, 2023Updated Dec 4, 2023

problem: theano.tensor.nnet.conv3d verify_grad throws error when border_mode is "full". But when border_mode is "valid" \ "half" or integers, verify_grad succeeds.

repo code:

import theano
import theano.tensor as T
import numpy as np
def custom_activation(input_var, filters):
    return theano.tensor.nnet.conv3d(
        input_var, filters,
        input_shape=(1, 1, 5, 5, 5), filter_shape=(1, 1, 3, 3, 3),
        border_mode="full", subsample=(2, 2, 2)
    )
x = T.tensor5('x')
filters = T.tensor5('filters')
output = custom_activation(x, filters)
loss = T.sum(output ** 2)
grad_x = T.grad(loss, x)
input_data = np.random.random((1, 1, 5, 5, 5)).astype('float32')
filter_data = np.random.random((1, 1, 3, 3, 3)).astype('float32')
rng = np.random.RandomState(123)
print(theano.gradient.verify_grad(custom_activation, pt=[input_data, filter_data], rng=rng))

output:

theano.gradient.GradientError: GradientError: numeric gradient and analytic gradient exceed tolerance:
        At position 93 of argument 0 with shape (1, 1, 5, 5, 5),
            val1 = 0.272239      ,  val2 = 0.256039
            abs. error = 0.016199,  abs. tolerance = 0.010000
            rel. error = 0.030664,  rel. tolerance = 0.010000
Exception args: 
The error happened with the following inputs:, [array([[[[[0.09332228, 0.6421774 , 0.44572467, 0.70291895, 0.5098043 ],
          [0.683838  , 0.5080997 , 0.17283899, 0.4718204 , 0.64879394],
          [0.04148663, 0.46596617, 0.01547194, 0.68491524, 0.7285864 ],
          [0.06167305, 0.5222763 , 0.85140914, 0.37101352, 0.4930306 ],
          [0.21818937, 0.39695936, 0.8148833 , 0.01425331, 0.28849632]],

         [[0.14579165, 0.3190887 , 0.3956053 , 0.6647894 , 0.56203324],
          [0.81805575, 0.81356776, 0.09343356, 0.446239  , 0.6224381 ],
          [0.40550607, 0.8124617 , 0.3634869 , 0.7658372 , 0.6083649 ],
          [0.02053943, 0.5735918 , 0.05305126, 0.7468577 , 0.07271606],
          [0.1226428 , 0.3624604 , 0.9435363 , 0.92066526, 0.30806598]],

         [[0.35772562, 0.7519072 , 0.70943844, 0.68003255, 0.2684157 ],
          [0.3173718 , 0.3078216 , 0.6538214 , 0.8464836 , 0.33976585],
          [0.8910162 , 0.36507687, 0.9585025 , 0.20540749, 0.63861555],
          [0.46364146, 0.14153846, 0.24533378, 0.98415434, 0.99015224],
          [0.10620494, 0.17009372, 0.95523095, 0.5107561 , 0.7761644 ]],

         [[0.37265608, 0.7579192 , 0.4827517 , 0.13808495, 0.2129412 ],
          [0.9965981 , 0.09803493, 0.55493206, 0.47609097, 0.4549156 ],
          [0.01366896, 0.28229293, 0.3536284 , 0.82693   , 0.2318111 ],
          [0.05245899, 0.15890527, 0.9407436 , 0.5197881 , 0.6661276 ],
          [0.6424468 , 0.8960475 , 0.95943075, 0.1911599 , 0.3247764 ]],

         [[0.5513183 , 0.36243898, 0.64463586, 0.2977075 , 0.01973151],
          [0.46624586, 0.76396894, 0.2670252 , 0.4671752 , 0.2952615 ],
          [0.40112826, 0.28927532, 0.4659784 , 0.6025376 , 0.47399056],
          [0.48325536, 0.6336406 , 0.6286751 , 0.44942206, 0.3274733 ],
          [0.85661966, 0.20490149, 0.54953533, 0.92833453, 0.45493412]]]]],
      dtype=float32), array([[[[[0.6196886 , 0.35433555, 0.859738  ],
          [0.8134446 , 0.2639104 , 0.23035723],
          [0.8424116 , 0.7441677 , 0.8341569 ]],

         [[0.32340366, 0.78315777, 0.7468556 ],
          [0.08881853, 0.3331014 , 0.842906  ],
          [0.33051413, 0.46756816, 0.50611615]],

         [[0.01137997, 0.3695013 , 0.87763387],
          [0.8687561 , 0.39756495, 0.0249908 ],
          [0.30266598, 0.90555274, 0.22650315]]]]], dtype=float32)], 
The value of eps is:, None, 
The out_type is:, None

version: python 3.6 theano 1.0.4