def tf_debug_gradient(x, y, verbose=True):
"""
Print the theoretical and numeric gradients, and the absolute difference between the two
Args:
x (tf.Variable): input variable
y (tf.Variable): output variable
verbose: switch display of information
Returns:
the theoretical and numeric gradient
"""
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
if verbose:
print(y.eval())
gt, gn = tf.test.compute_gradient(
x, [d.value for d in x.get_shape()], y, [d.value for d in y.get_shape()], delta=1e-2)
if verbose:
print(np.concatenate((gt, gn, np.round(np.abs(gt-gn),2)), len(gt.shape) - 1))
print(y.eval())
return gt, gn
评论列表
文章目录