def check_nn_gradients(lmd): input_layer_size = 3 hidden_layer_size = 5 num_labels = 3 m = 5 # We generatesome 'random' test data theta1 = diw.debug_initialize_weights(hidden_layer_size, input_layer_size) theta2 = diw.debug_initialize_weights(num_labels, hidden_layer_size) # Reusing debugInitializeWeights to genete X X = diw.debug_initialize_weights(m, input_layer_size - 1) y = 1 + np.mod(np.arange(1, m + 1), num_labels) # Unroll parameters nn_params = np.concatenate([theta1.flatten(), theta2.flatten()]) numgrad = np.zeros(nn_params.size) perturb = np.zeros(nn_params.size) e = 1e-4 for p in range(nn_params.size): perturb[p] = e loss1 = ncf.nn_cost_function(nn_params - perturb, input_layer_size, hidden_layer_size, num_labels, X, y, lmd) loss2 = ncf.nn_cost_function(nn_params + perturb, input_layer_size, hidden_layer_size, num_labels, X, y, lmd) numgrad[p] = (loss2 - loss1) / (2 * e) perturb[p] = 0 grad = ncf.nn_grad_function(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lmd) diff = np.linalg.norm(numgrad - grad) / np.linalg.norm(numgrad + grad) print( 'If your backpropagation implementation is correct,\n' 'the relative difference will be smaller than 10e-9 (assume epsilon=0.0001).\n' 'Relative Difference: {}\n'.format(diff))
def grad_func(p): return ncf.nn_cost_function(p, input_layer_size, hidden_layer_size, num_labels, X, y, lmd)[1]
# should complete the code in nncostfunction.py to return cost. After # implementing the feedforward to compute the cost, you can verify that # your implementation is correct by verifying that you get the same cost # as us for the fixed debugging parameters. # # We suggest implementing the feedforward cost *without* regularization # first so that it will be easier for you to debug. Later, in part 4, you # will get to implement the regularized cost. # print('Feedforward Using Neural Network ...') # Weight regularization parameter (we set this to 0 here). lmd = 0 cost, grad = ncf.nn_cost_function(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lmd) print( 'Cost at parameters (loaded from ex4weights): {:0.6f}\n(This value should be about 0.287629)' .format(cost)) input('Program paused. Press ENTER to continue') # ===================== Part 4: Implement Regularization ===================== # Once your cost function implementation is correct, you should now # continue to implement the regularization with the cost. # print('Checking Cost Function (w/ Regularization) ...') # Weight regularization parameter (we set this to 1 here).
hidden_layer_size = 25 # 25 hidden units num_labels = 10 # 10 labels, from 1 to 10 mat = scipy.io.loadmat("ex4data1.mat") X = mat["X"] y = mat["y"] mat = scipy.io.loadmat("ex4weights.mat") Theta1 = mat["Theta1"] Theta2 = mat["Theta2"] lamb = 0 nn_params = numpy.concatenate( (Theta1.reshape(Theta1.size, order="F"), Theta2.reshape(Theta2.size, order="F"))) cost = nn_cost_function(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lamb) print("Cost at parameters (loaded from ex4weights.mat):", cost) lamb = 1 cost = nn_cost_function(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lamb) print("Cost at parameters (loaded from ex4weights.mat):", cost) g = sigmoid_gradient(numpy.array([1, -0.5, 0, 0.5, 1])) print(g) initial_theta1 = rand_initialize_weights(input_layer_size, hidden_layer_size) initial_theta2 = rand_initialize_weights(hidden_layer_size, num_labels) initial_nn_params = numpy.concatenate( (initial_theta1.reshape(initial_theta1.size, order="F"), initial_theta2.reshape(initial_theta2.size, order="F")))
def costFunc(p): return nn_cost_function(p, input_layer_size, hidden_layer_size, num_labels, X, y, lamb)