This file contains hidden or 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
| def sigmoid_gradient(dA, Z): | |
| A, Z = sigmoid(Z) | |
| dZ = dA * A * (1 - A) | |
| return dZ | |
| def tanh_gradient(dA, Z): | |
| A, Z = tanh(Z) | |
| dZ = dA * (1 - np.square(A)) |
This file contains hidden or 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
| # Compute cross-entropy cost | |
| def compute_cost(AL, y): | |
| m = y.shape[1] | |
| cost = - (1 / m) * np.sum( | |
| np.multiply(y, np.log(AL)) + np.multiply(1 - y, np.log(1 - AL))) | |
| return cost |
This file contains hidden or 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
| # Define helper functions that will be used in L-model forward prop | |
| def linear_forward(A_prev, W, b): | |
| Z = np.dot(W, A_prev) + b | |
| cache = (A_prev, W, b) | |
| return Z, cache | |
| def linear_activation_forward(A_prev, W, b, activation_fn): | |
| assert activation_fn == "sigmoid" or activation_fn == "tanh" or \ | |
| activation_fn == "relu" |
This file contains hidden or 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
| # Plot the 4 activation functions | |
| z = np.linspace(-10, 10, 100) | |
| # Computes post-activation outputs | |
| A_sigmoid, z = sigmoid(z) | |
| A_tanh, z = tanh(z) | |
| A_relu, z = relu(z) | |
| A_leaky_relu, z = leaky_relu(z) | |
| # Plot sigmoid |
This file contains hidden or 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
| def sigmoid(Z): | |
| A = 1 / (1 + np.exp(-Z)) | |
| return A, Z | |
| def tanh(Z): | |
| A = np.tanh(Z) | |
| return A, Z | |
This file contains hidden or 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
| def initialize_parameters(layers_dims): | |
| np.random.seed(1) | |
| parameters = {} | |
| L = len(layers_dims) | |
| for l in range(1, L): | |
| parameters["W" + str(l)] = np.random.randn( | |
| layers_dims[l], layers_dims[l - 1]) * 0.01 | |
| parameters["b" + str(l)] = np.zeros((layers_dims[l], 1)) |
This file contains hidden or 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
| # Cricles | |
| X1 = make_circles(factor=0.5, noise=0.05, n_samples=1500) | |
| # Moons | |
| X2 = make_moons(n_samples=1500, noise=0.05) | |
| fig, ax = plt.subplots(1, 2) | |
| for i, X in enumerate([X1, X2]): | |
| fig.set_size_inches(18, 7) | |
| sp = SpectralClustering(n_clusters=2, affinity='nearest_neighbors') |
This file contains hidden or 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
| # Cricles | |
| X1 = make_circles(factor=0.5, noise=0.05, n_samples=1500) | |
| # Moons | |
| X2 = make_moons(n_samples=1500, noise=0.05) | |
| fig, ax = plt.subplots(1, 2) | |
| for i, X in enumerate([X1, X2]): | |
| fig.set_size_inches(18, 7) | |
| km = KMeans(n_clusters=2) |
This file contains hidden or 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
| # Create data from three different multivariate distributions | |
| X_1 = np.random.multivariate_normal(mean=[4, 0], cov=[[1, 0], [0, 1]], size=75) | |
| X_2 = np.random.multivariate_normal(mean=[6, 6], cov=[[2, 0], [0, 2]], size=250) | |
| X_3 = np.random.multivariate_normal(mean=[1, 5], cov=[[1, 0], [0, 2]], size=20) | |
| df = np.concatenate([X_1, X_2, X_3]) | |
| # Run kmeans | |
| km = KMeans(n_clusters=3) | |
| km.fit(df) | |
| labels = km.predict(df) |
This file contains hidden or 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
| # Create horizantal data | |
| X = np.tile(np.linspace(1, 5, 20), 2) | |
| y = np.repeat(np.array([2, 4]), 20) | |
| df = np.c_[X, y] | |
| km = KMeans(n_clusters=2) | |
| km.fit(df) | |
| labels = km.predict(df) | |
| centroids = km.cluster_centers_ |