Skip to content

Instantly share code, notes, and snippets.

@eickenberg
Last active August 29, 2015 14:14
Show Gist options
  • Save eickenberg/8375cb16d3b742f85cb0 to your computer and use it in GitHub Desktop.
Save eickenberg/8375cb16d3b742f85cb0 to your computer and use it in GitHub Desktop.
checking theano l1 gradient == sign
import theano
import theano.tensor as T
import numpy as np
# 0 and some arbitrarily small positive and negative numbers
test_vector = np.array(
[0., 1., -1., .1, -.1, 10., -10., 1e-8, -1e-8,
1e-10, -1e-10, 1e-16, -1e-16]).astype(np.float32)
input_expr = T.fvector()
l1_norm = abs(input_expr).sum()
l1_norm_grad = T.grad(l1_norm, wrt=input_expr)
f_grad = theano.function([input_expr], l1_norm_grad)
from numpy.testing import assert_array_equal
# show that on all examples the gradient of the l1 norm is identical to the sign of the vector
assert_array_equal(f_grad(test_vector), np.sign(test_vector))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment