Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vdurm Improving_DNN #35

Open
wants to merge 3 commits into
base: vdurm_master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 77 additions & 58 deletions Improving_DNN/GradientChecking/GradientChecking.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Packages\n",
Expand Down Expand Up @@ -72,10 +70,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# GRADED FUNCTION: forward_propagation\n",
Expand All @@ -93,17 +89,25 @@
" \"\"\"\n",
" \n",
" ### START CODE HERE ### (approx. 1 line)\n",
" J = None\n",
" J = x * theta\n",
" ### END CODE HERE ###\n",
" \n",
" return J"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"J = 8\n"
]
}
],
"source": [
"x, theta = 2, 4\n",
"J = forward_propagation(x, theta)\n",
Expand Down Expand Up @@ -133,10 +137,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# GRADED FUNCTION: backward_propagation\n",
Expand All @@ -154,19 +156,27 @@
" \"\"\"\n",
" \n",
" ### START CODE HERE ### (approx. 1 line)\n",
" dtheta = None\n",
" dtheta = x\n",
" ### END CODE HERE ###\n",
" \n",
" return dtheta"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {
"scrolled": true
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dtheta = 2\n"
]
}
],
"source": [
"x, theta = 2, 4\n",
"dtheta = backward_propagation(x, theta)\n",
Expand Down Expand Up @@ -212,10 +222,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# GRADED FUNCTION: gradient_check\n",
Expand All @@ -235,22 +243,22 @@
" \n",
" # Compute gradapprox using left side of formula (1). epsilon is small enough, you don't need to worry about the limit.\n",
" ### START CODE HERE ### (approx. 5 lines)\n",
" thetaplus = None # Step 1\n",
" thetaminus = None # Step 2\n",
" J_plus = None # Step 3\n",
" J_minus = None # Step 4\n",
" gradapprox = None # Step 5\n",
" thetaplus = theta+epsilon # Step 1\n",
" thetaminus = theta-epsilon # Step 2\n",
" J_plus = thetaplus*x # Step 3\n",
" J_minus = thetaminus*x # Step 4\n",
" gradapprox = (J_plus-J_minus) / (2*epsilon) # Step 5\n",
" ### END CODE HERE ###\n",
" \n",
" # Check if gradapprox is close enough to the output of backward_propagation()\n",
" ### START CODE HERE ### (approx. 1 line)\n",
" grad = None\n",
" grad = backward_propagation(x, theta)\n",
" ### END CODE HERE ###\n",
" \n",
" ### START CODE HERE ### (approx. 1 line)\n",
" numerator = None # Step 1'\n",
" denominator = None # Step 2'\n",
" difference = None # Step 3'\n",
" numerator = np.linalg.norm(grad-gradapprox) # Step 1'\n",
" denominator = np.linalg.norm(grad)+np.linalg.norm(gradapprox) # Step 2'\n",
" difference = numerator/denominator # Step 3'\n",
" ### END CODE HERE ###\n",
" \n",
" if difference < 1e-7:\n",
Expand All @@ -263,11 +271,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The gradient is correct!\n",
"difference = 2.919335883291695e-10\n"
]
}
],
"source": [
"x, theta = 2, 4\n",
"difference = gradient_check(x, theta)\n",
Expand Down Expand Up @@ -320,10 +337,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def forward_propagation_n(X, Y, parameters):\n",
Expand Down Expand Up @@ -380,10 +395,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def backward_propagation_n(X, Y, cache):\n",
Expand Down Expand Up @@ -469,10 +482,8 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# GRADED FUNCTION: gradient_check_n\n",
Expand Down Expand Up @@ -506,28 +517,28 @@
" # Compute J_plus[i]. Inputs: \"parameters_values, epsilon\". Output = \"J_plus[i]\".\n",
" # \"_\" is used because the function you have to outputs two parameters but we only care about the first one\n",
" ### START CODE HERE ### (approx. 3 lines)\n",
" thetaplus = None # Step 1\n",
" thetaplus[i][0] = None # Step 2\n",
" J_plus[i], _ = None # Step 3\n",
" thetaplus = np.copy(parameters_values) # Step 1\n",
" thetaplus[i][0] = thetaplus[i][0]+epsilon # Step 2\n",
" J_plus[i], _ = forward_propagation_n(X,Y, vector_to_dictionary(thetaplus)) # Step 3\n",
" ### END CODE HERE ###\n",
" \n",
" # Compute J_minus[i]. Inputs: \"parameters_values, epsilon\". Output = \"J_minus[i]\".\n",
" ### START CODE HERE ### (approx. 3 lines)\n",
" thetaminus = None # Step 1\n",
" thetaminus[i][0] = None # Step 2 \n",
" J_minus[i], _ = None # Step 3\n",
" thetaminus = np.copy(parameters_values) # Step 1\n",
" thetaminus[i][0] = thetaminus[i][0]-epsilon # Step 2 \n",
" J_minus[i], _ = forward_propagation_n(X,Y, vector_to_dictionary(thetaminus)) # Step 3\n",
" ### END CODE HERE ###\n",
" \n",
" # Compute gradapprox[i]\n",
" ### START CODE HERE ### (approx. 1 line)\n",
" gradapprox[i] = None\n",
" gradapprox[i] = (J_plus[i]-J_minus[i])/(2*epsilon)\n",
" ### END CODE HERE ###\n",
" \n",
" # Compare gradapprox to backward propagation gradients by computing difference.\n",
" ### START CODE HERE ### (approx. 1 line)\n",
" numerator = None # Step 1'\n",
" denominator = None # Step 2'\n",
" difference = None # Step 3'\n",
" numerator = np.linalg.norm(grad-gradapprox) # Step 1'\n",
" denominator = np.linalg.norm(grad)+np.linalg.norm(gradapprox) # Step 2'\n",
" difference = numerator/denominator # Step 3'\n",
" ### END CODE HERE ###\n",
"\n",
" if difference > 2e-7:\n",
Expand All @@ -540,11 +551,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {
"scrolled": false
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[93mThere is a mistake in the backward propagation! difference = 0.2850931567761624\u001b[0m\n"
]
}
],
"source": [
"X, Y, parameters = gradient_check_n_test_case()\n",
"\n",
Expand Down Expand Up @@ -618,7 +637,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
"version": "3.7.1"
}
},
"nbformat": 4,
Expand Down
406 changes: 326 additions & 80 deletions Improving_DNN/Initialization/Initialization.ipynb

Large diffs are not rendered by default.

349 changes: 257 additions & 92 deletions Improving_DNN/Regularization/Regularization.ipynb

Large diffs are not rendered by default.