-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
week-3 of neural network & deep learning
- Loading branch information
1 parent
9402d8b
commit 6068d81
Showing
10 changed files
with
3,707 additions
and
0 deletions.
There are no files selected for viewing
1,575 changes: 1,575 additions & 0 deletions
1,575
...ssification_with_a_hidden_layer/Planar+data+classification+with+one+hidden+layer+v4.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
1,575 changes: 1,575 additions & 0 deletions
1,575
...ssification_with_a_hidden_layer/Planar+data+classification+with+one+hidden+layer+v5.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file added
BIN
+136 KB
.../Planar_data_classification_with_a_hidden_layer/images/classification_kiank.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+666 KB
...signment/Planar_data_classification_with_a_hidden_layer/images/grad_summary.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+40.7 KB
...Week-3/Assignment/Planar_data_classification_with_a_hidden_layer/images/sgd.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+62.5 KB
...-3/Assignment/Planar_data_classification_with_a_hidden_layer/images/sgd_bad.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions
66
...Learning/Week-3/Assignment/Planar_data_classification_with_a_hidden_layer/planar_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import sklearn | ||
import sklearn.datasets | ||
import sklearn.linear_model | ||
|
||
def plot_decision_boundary(model, X, y): | ||
# Set min and max values and give it some padding | ||
x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1 | ||
y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1 | ||
h = 0.01 | ||
# Generate a grid of points with distance h between them | ||
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) | ||
# Predict the function value for the whole grid | ||
Z = model(np.c_[xx.ravel(), yy.ravel()]) | ||
Z = Z.reshape(xx.shape) | ||
# Plot the contour and training examples | ||
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral) | ||
plt.ylabel('x2') | ||
plt.xlabel('x1') | ||
plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral) | ||
|
||
|
||
def sigmoid(x): | ||
""" | ||
Compute the sigmoid of x | ||
Arguments: | ||
x -- A scalar or numpy array of any size. | ||
Return: | ||
s -- sigmoid(x) | ||
""" | ||
s = 1/(1+np.exp(-x)) | ||
return s | ||
|
||
def load_planar_dataset(): | ||
np.random.seed(1) | ||
m = 400 # number of examples | ||
N = int(m/2) # number of points per class | ||
D = 2 # dimensionality | ||
X = np.zeros((m,D)) # data matrix where each row is a single example | ||
Y = np.zeros((m,1), dtype='uint8') # labels vector (0 for red, 1 for blue) | ||
a = 4 # maximum ray of the flower | ||
|
||
for j in range(2): | ||
ix = range(N*j,N*(j+1)) | ||
t = np.linspace(j*3.12,(j+1)*3.12,N) + np.random.randn(N)*0.2 # theta | ||
r = a*np.sin(4*t) + np.random.randn(N)*0.2 # radius | ||
X[ix] = np.c_[r*np.sin(t), r*np.cos(t)] | ||
Y[ix] = j | ||
|
||
X = X.T | ||
Y = Y.T | ||
|
||
return X, Y | ||
|
||
def load_extra_datasets(): | ||
N = 200 | ||
noisy_circles = sklearn.datasets.make_circles(n_samples=N, factor=.5, noise=.3) | ||
noisy_moons = sklearn.datasets.make_moons(n_samples=N, noise=.2) | ||
blobs = sklearn.datasets.make_blobs(n_samples=N, random_state=5, n_features=2, centers=6) | ||
gaussian_quantiles = sklearn.datasets.make_gaussian_quantiles(mean=None, cov=0.5, n_samples=N, n_features=2, n_classes=2, shuffle=True, random_state=None) | ||
no_structure = np.random.rand(N, 2), np.random.rand(N, 2) | ||
|
||
return noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure |
119 changes: 119 additions & 0 deletions
119
...Learning/Week-3/Assignment/Planar_data_classification_with_a_hidden_layer/testCases_v2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import numpy as np | ||
|
||
def layer_sizes_test_case(): | ||
np.random.seed(1) | ||
X_assess = np.random.randn(5, 3) | ||
Y_assess = np.random.randn(2, 3) | ||
return X_assess, Y_assess | ||
|
||
def initialize_parameters_test_case(): | ||
n_x, n_h, n_y = 2, 4, 1 | ||
return n_x, n_h, n_y | ||
|
||
|
||
def forward_propagation_test_case(): | ||
np.random.seed(1) | ||
X_assess = np.random.randn(2, 3) | ||
b1 = np.random.randn(4,1) | ||
b2 = np.array([[ -1.3]]) | ||
|
||
parameters = {'W1': np.array([[-0.00416758, -0.00056267], | ||
[-0.02136196, 0.01640271], | ||
[-0.01793436, -0.00841747], | ||
[ 0.00502881, -0.01245288]]), | ||
'W2': np.array([[-0.01057952, -0.00909008, 0.00551454, 0.02292208]]), | ||
'b1': b1, | ||
'b2': b2} | ||
|
||
return X_assess, parameters | ||
|
||
def compute_cost_test_case(): | ||
np.random.seed(1) | ||
Y_assess = (np.random.randn(1, 3) > 0) | ||
parameters = {'W1': np.array([[-0.00416758, -0.00056267], | ||
[-0.02136196, 0.01640271], | ||
[-0.01793436, -0.00841747], | ||
[ 0.00502881, -0.01245288]]), | ||
'W2': np.array([[-0.01057952, -0.00909008, 0.00551454, 0.02292208]]), | ||
'b1': np.array([[ 0.], | ||
[ 0.], | ||
[ 0.], | ||
[ 0.]]), | ||
'b2': np.array([[ 0.]])} | ||
|
||
a2 = (np.array([[ 0.5002307 , 0.49985831, 0.50023963]])) | ||
|
||
return a2, Y_assess, parameters | ||
|
||
def backward_propagation_test_case(): | ||
np.random.seed(1) | ||
X_assess = np.random.randn(2, 3) | ||
Y_assess = (np.random.randn(1, 3) > 0) | ||
parameters = {'W1': np.array([[-0.00416758, -0.00056267], | ||
[-0.02136196, 0.01640271], | ||
[-0.01793436, -0.00841747], | ||
[ 0.00502881, -0.01245288]]), | ||
'W2': np.array([[-0.01057952, -0.00909008, 0.00551454, 0.02292208]]), | ||
'b1': np.array([[ 0.], | ||
[ 0.], | ||
[ 0.], | ||
[ 0.]]), | ||
'b2': np.array([[ 0.]])} | ||
|
||
cache = {'A1': np.array([[-0.00616578, 0.0020626 , 0.00349619], | ||
[-0.05225116, 0.02725659, -0.02646251], | ||
[-0.02009721, 0.0036869 , 0.02883756], | ||
[ 0.02152675, -0.01385234, 0.02599885]]), | ||
'A2': np.array([[ 0.5002307 , 0.49985831, 0.50023963]]), | ||
'Z1': np.array([[-0.00616586, 0.0020626 , 0.0034962 ], | ||
[-0.05229879, 0.02726335, -0.02646869], | ||
[-0.02009991, 0.00368692, 0.02884556], | ||
[ 0.02153007, -0.01385322, 0.02600471]]), | ||
'Z2': np.array([[ 0.00092281, -0.00056678, 0.00095853]])} | ||
return parameters, cache, X_assess, Y_assess | ||
|
||
def update_parameters_test_case(): | ||
parameters = {'W1': np.array([[-0.00615039, 0.0169021 ], | ||
[-0.02311792, 0.03137121], | ||
[-0.0169217 , -0.01752545], | ||
[ 0.00935436, -0.05018221]]), | ||
'W2': np.array([[-0.0104319 , -0.04019007, 0.01607211, 0.04440255]]), | ||
'b1': np.array([[ -8.97523455e-07], | ||
[ 8.15562092e-06], | ||
[ 6.04810633e-07], | ||
[ -2.54560700e-06]]), | ||
'b2': np.array([[ 9.14954378e-05]])} | ||
|
||
grads = {'dW1': np.array([[ 0.00023322, -0.00205423], | ||
[ 0.00082222, -0.00700776], | ||
[-0.00031831, 0.0028636 ], | ||
[-0.00092857, 0.00809933]]), | ||
'dW2': np.array([[ -1.75740039e-05, 3.70231337e-03, -1.25683095e-03, | ||
-2.55715317e-03]]), | ||
'db1': np.array([[ 1.05570087e-07], | ||
[ -3.81814487e-06], | ||
[ -1.90155145e-07], | ||
[ 5.46467802e-07]]), | ||
'db2': np.array([[ -1.08923140e-05]])} | ||
return parameters, grads | ||
|
||
def nn_model_test_case(): | ||
np.random.seed(1) | ||
X_assess = np.random.randn(2, 3) | ||
Y_assess = (np.random.randn(1, 3) > 0) | ||
return X_assess, Y_assess | ||
|
||
def predict_test_case(): | ||
np.random.seed(1) | ||
X_assess = np.random.randn(2, 3) | ||
parameters = {'W1': np.array([[-0.00615039, 0.0169021 ], | ||
[-0.02311792, 0.03137121], | ||
[-0.0169217 , -0.01752545], | ||
[ 0.00935436, -0.05018221]]), | ||
'W2': np.array([[-0.0104319 , -0.04019007, 0.01607211, 0.04440255]]), | ||
'b1': np.array([[ -8.97523455e-07], | ||
[ 8.15562092e-06], | ||
[ 6.04810633e-07], | ||
[ -2.54560700e-06]]), | ||
'b2': np.array([[ 9.14954378e-05]])} | ||
return parameters, X_assess |
186 changes: 186 additions & 0 deletions
186
...and_Deep_Learning/Week-3/Quiz/.ipynb_checkpoints/Shallow-Neural-Networks-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 1. Which of the following are true? (Check all that apply.)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"##### Ans: \n", | ||
"- X is a matrix in which each column is one training example.\n", | ||
"- a^{[2]}_4 is the activation output by the 4^{th} neuron of the 2^{nd} layer\n", | ||
"- a[2] denotes the activation vector of the 2^{nd} layer.\n", | ||
"- a^{[2](12)} denotes the activation vector of the 2^{nd} layer for the 12^{th} training example." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 2. The tanh activation usually works better than sigmoid activation function for hidden units because the mean of its output is closer to zero, and so it centers the data better for the next layer. True/False?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"##### Ans: True" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 3. Which of these is a correct vectorized implementation of forward propagation for layer lll, where 1 \\leq l \\leq L?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"##### Ans: \n", | ||
"- Z[l]=W[l]A[l−1]+b[l]\n", | ||
"- A[l] = g^{[l]}(Z^{[l]})A[l]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 4. You are building a binary classifier for recognizing cucumbers (y=1) vs. watermelons (y=0). Which one of these activation functions would you recommend using for the output layer? " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"##### Ans: sigmoid" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 5. Consider the following code:\n", | ||
"#### What will be B.shape? (If you’re not sure, feel free to run this in python to find out)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "raw", | ||
"metadata": {}, | ||
"source": [ | ||
"A = np.random.randn(4,3)\n", | ||
"B = np.sum(A, axis = 1, keepdims = True)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"##### Ans: (4, 1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 6. Suppose you have built a neural network. You decide to initialize the weights and biases to be zero. Which of the following statements is true?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"##### Ans: Each neuron in the first hidden layer will perform the same computation. So even after multiple iterations of gradient descent each neuron in the layer will be computing the same thing as other neurons. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 7. Logistic regression’s weights w should be initialized randomly rather than to all zeros, because if you initialize to all zeros, then logistic regression will fail to learn a useful decision boundary because it will fail to “break symmetry”, True/False?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"##### Ans: False" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 8. You have built a network using the tanh activation for all the hidden units. You initialize the weights to relative large values, using np.random.randn(..,..)*1000. What will happen? " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"##### Ans: This will cause the inputs of the tanh to also be very large, thus causing gradients to be close to zero. The optimization algorithm will thus become slow. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 9. Consider the following 1 hidden layer neural network:\n", | ||
"<img src=\"https://d3c33hcgiwev3.cloudfront.net/imageAssetProxy.v1/svogd3rcEeeA3RJRlG3Uqg_76207ca9fa3c209fbf43892870e5338f_1layer.png?expiry=1558137600000&hmac=9c8eqcnsxto62yQItQ2E3pCwPS3ISs08nQix4zwvoVI\" alt=\"\" >\n", | ||
"#### Which of the following statements are True? (Check all that apply)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"##### Ans: \n", | ||
"- b[1] will have shape (4, 1) \n", | ||
"- W[1] will have shape (4, 2)\n", | ||
"- W[2] will have shape (1, 4)\n", | ||
"- b[2] will have shape (1, 1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 10. In the same network as the previous question, what are the dimensions of Z^{[1]} and A^{[1]}?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"##### Ans: Z[1] and A^{[1]} are (4,m) " | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.6.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.