Skip to content

Instantly share code, notes, and snippets.

@srikarplus
Created October 9, 2018 01:48
Show Gist options
  • Save srikarplus/067a90e87c016b7d1f1cd6845231ae80 to your computer and use it in GitHub Desktop.
Save srikarplus/067a90e87c016b7d1f1cd6845231ae80 to your computer and use it in GitHub Desktop.
cost function for neural network
def nnCostFunc(nn_params, input_layer_size, hidden_layer_size, num_labels, X, y, lmbda):
theta1 = np.reshape(nn_params[:hidden_layer_size*(input_layer_size+1)], (hidden_layer_size, input_layer_size+1), 'F')
theta2 = np.reshape(nn_params[hidden_layer_size*(input_layer_size+1):], (num_labels, hidden_layer_size+1), 'F')
m = len(y)
ones = np.ones((m,1))
a1 = np.hstack((ones, X))
a2 = sigmoid(a1 @ theta1.T)
a2 = np.hstack((ones, a2))
h = sigmoid(a2 @ theta2.T)
y_d = pd.get_dummies(y.flatten())
temp1 = np.multiply(y_d, np.log(h))
temp2 = np.multiply(1-y_d, np.log(1-h))
temp3 = np.sum(temp1 + temp2)
sum1 = np.sum(np.sum(np.power(theta1[:,1:],2), axis = 1))
sum2 = np.sum(np.sum(np.power(theta2[:,1:],2), axis = 1))
return np.sum(temp3 / (-m)) + (sum1 + sum2) * lmbda / (2*m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment