def test_neuralnet_grad(nn): f, grad = back_prop1(nn.data, nn.labels, nn.params, dd.dimensions) emp_grad = empirical_grad(lambda params: back_prop1(data, labels, params, dimensions), params) assert_close(grad, emp_grad)
# Set up fake data and parameters for the neural network N = 20 dimensions = [10, 5, 10] data = np.random.randn(N, dimensions[0]) # each row will be a datum labels = np.zeros((N, dimensions[2])) for i in xrange(N): labels[i,random.randint(0, dimensions[2]-1)] = 1 params = np.random.randn((dimensions[0] + 1) * dimensions[1] + (dimensions[1] + 1) * dimensions[2], ) print "Dimensionality of parameter vector", params.shape # Perform gradcheck on your neural network print "=== Neural network gradient check 1===" check_res = gradcheck_naive(lambda params: back_prop1(data, labels, params, dimensions), params) print "=== Neural network gradient check 2===" #check_res = gradcheck_naive(lambda params: back_prop2(data, labels, params, dimensions), params) print "=== normalize rows ===" print normalize_rows(np.array([[3.0, 4.0],[1, 2]])) # the result should be [[0.6, 0.8], [0.4472, 0.8944]] # Interface to the dataset for negative sampling dataset = type('dummy', (), {})() def dummySampleTokenIdx(): return random.randint(0, 4) def getRandomContext(C, parameters=None): tokens = ["a", "b", "c", "d", "e"] return tokens[random.randint(0,4)], [tokens[random.randint(0,4)] for i in xrange(2*C)]