Path: blob/master/Improving Deep Neural Networks Hyperparameter tuning, Regularization and Optimization/week5/Gradient Checking/gc_utils.py
14483 views
import numpy as np12def sigmoid(x):3"""4Compute the sigmoid of x56Arguments:7x -- A scalar or numpy array of any size.89Return:10s -- sigmoid(x)11"""12s = 1/(1+np.exp(-x))13return s1415def relu(x):16"""17Compute the relu of x1819Arguments:20x -- A scalar or numpy array of any size.2122Return:23s -- relu(x)24"""25s = np.maximum(0,x)2627return s2829def dictionary_to_vector(parameters):30"""31Roll all our parameters dictionary into a single vector satisfying our specific required shape.32"""33keys = []34count = 035for key in ["W1", "b1", "W2", "b2", "W3", "b3"]:3637# flatten parameter38new_vector = np.reshape(parameters[key], (-1,1))39keys = keys + [key]*new_vector.shape[0]4041if count == 0:42theta = new_vector43else:44theta = np.concatenate((theta, new_vector), axis=0)45count = count + 14647return theta, keys4849def vector_to_dictionary(theta):50"""51Unroll all our parameters dictionary from a single vector satisfying our specific required shape.52"""53parameters = {}54parameters["W1"] = theta[:20].reshape((5,4))55parameters["b1"] = theta[20:25].reshape((5,1))56parameters["W2"] = theta[25:40].reshape((3,5))57parameters["b2"] = theta[40:43].reshape((3,1))58parameters["W3"] = theta[43:46].reshape((1,3))59parameters["b3"] = theta[46:47].reshape((1,1))6061return parameters6263def gradients_to_vector(gradients):64"""65Roll all our gradients dictionary into a single vector satisfying our specific required shape.66"""6768count = 069for key in ["dW1", "db1", "dW2", "db2", "dW3", "db3"]:70# flatten parameter71new_vector = np.reshape(gradients[key], (-1,1))7273if count == 0:74theta = new_vector75else:76theta = np.concatenate((theta, new_vector), axis=0)77count = count + 17879return theta8081