├── Chapter01 ├── cross_validation.py ├── cross_validation_2.py └── cross_validation_3.py ├── Chapter02 ├── cple.py ├── generative_gaussian_mixtures.py ├── s3vm.py └── tsvm.py ├── Chapter03 ├── Old │ └── label_spreading.py ├── isomap.py ├── label_propagation.py ├── label_propagation_mrw.py ├── label_propagation_scikit_learn.py ├── label_spreading.py ├── laplacian_spectral_embedding.py ├── locally_linear_embedding.py └── tsne.py ├── Chapter04 ├── direct_sampling.py ├── hmm.py ├── metropolis-hastings-sampling.py └── pymc3_example.py ├── Chapter05 ├── factor_analysis.py ├── gaussian_mixture.py ├── ica.py ├── parameter_estimation.py └── pca.py ├── Chapter06 ├── SOM.py ├── covariance_rule.py ├── hebb_rule.py ├── rubner_tavan_network.py └── sanger_network.py ├── Chapter07 ├── evaluation_metrics.py ├── fuzzy_cmeans.py ├── k_nearest_neighbors.py ├── kmeans.py ├── metrics.py └── spectral_clustering.py ├── Chapter08 ├── adaboost.py ├── gradient_tree_boosting.py ├── random_forest.py └── voting_classifiers.py ├── Chapter09 ├── batch_normalization.py ├── dropout.py ├── mlp.py └── perceptron.py ├── Chapter10 ├── dcn.py ├── dcn_da.py └── recurrent.py ├── Chapter11 ├── dca.py ├── denoising_dca.py ├── sparse_dca.py └── variational_dca.py ├── Chapter12 ├── dcgan.py └── wgan.py ├── Chapter13 ├── supervised_dbn.py └── unsupervised_dbn.py ├── Chapter14 ├── policy_iteration.py ├── td0.py └── value_iteration.py ├── Chapter15 ├── actor_critic_td0.py ├── q_learning.py ├── q_learning_nn.py ├── sarsa.py └── td_lambda.py ├── LICENSE └── README.md /Chapter01/cross_validation.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import make_classification 5 | from sklearn.linear_model import LogisticRegression 6 | from sklearn.model_selection import cross_val_score 7 | 8 | 9 | # Set random seed for reproducibility 10 | np.random.seed(1000) 11 | 12 | 13 | if __name__ == '__main__': 14 | # Create the dataset 15 | X, Y = make_classification(n_samples=1000, random_state=1000) 16 | 17 | # Perform a CV with 15 folds and a Logistic Regression 18 | score = cross_val_score(LogisticRegression(), X, Y, cv=15) 19 | 20 | print('Average CV score: {}'.format(np.mean(score))) 21 | print('CV score variance: {}'.format(np.var(score))) 22 | 23 | # Plot the scores 24 | fig, ax = plt.subplots(figsize=(10, 6)) 25 | 26 | ax.plot(score) 27 | ax.set_xlabel('Cross-validation fold') 28 | ax.set_ylabel('Logistic Regression Accuracy') 29 | ax.grid() 30 | plt.show() -------------------------------------------------------------------------------- /Chapter01/cross_validation_2.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from sklearn.datasets import load_digits 4 | from sklearn.model_selection import cross_val_score, LeaveOneOut 5 | 6 | from sklearn.svm import SVC 7 | 8 | 9 | # Set random seed for reproducibility 10 | np.random.seed(1000) 11 | 12 | 13 | if __name__ == '__main__': 14 | # Load the dataset 15 | data = load_digits() 16 | 17 | # Create a polynomial SVM 18 | svm = SVC(kernel='poly') 19 | 20 | # Perform k-Fold Cross Validation 21 | skf_scores = cross_val_score(svm, data['data'], data['target'], cv=10) 22 | print('CV scores: {}'.format(skf_scores)) 23 | print('Average CV score: {}'.format(skf_scores.mean())) 24 | 25 | # Perform Leave-One-Out Cross Validation 26 | loo_scores = cross_val_score(svm, data['data'], data['target'], cv=LeaveOneOut()) 27 | print('LOO scores (100): {}'.format(loo_scores[0:100])) 28 | print('Average LOO score: {}'.format(loo_scores.mean())) -------------------------------------------------------------------------------- /Chapter01/cross_validation_3.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from sklearn.datasets import load_iris 4 | from sklearn.linear_model import LogisticRegression 5 | from sklearn.model_selection import cross_val_score, LeavePOut 6 | 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | 11 | 12 | if __name__ == '__main__': 13 | # Load the dataset 14 | data = load_iris() 15 | 16 | p = 3 17 | lr = LogisticRegression() 18 | 19 | # Perform Leave-P-Out Cross Validation 20 | lpo_scores = cross_val_score(lr, data['data'], data['target'], cv=LeavePOut(p)) 21 | print('LPO scores (100): {}'.format(lpo_scores[0:100])) 22 | print('Average LPO score: {}'.format(lpo_scores.mean())) 23 | -------------------------------------------------------------------------------- /Chapter02/cple.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from scipy.optimize import fmin_bfgs 4 | 5 | from sklearn.datasets import load_digits 6 | from sklearn.linear_model import LogisticRegression 7 | from sklearn.model_selection import cross_val_score 8 | 9 | # Set random seed for reproducibility 10 | np.random.seed(1000) 11 | 12 | nb_unlabeled = 150 13 | 14 | # Create a training Logistic Regression instance 15 | lr = LogisticRegression() 16 | 17 | # Initialize soft-variables 18 | q0 = np.random.uniform(0, 1, size=nb_unlabeled) 19 | 20 | # Vectorized threshold function 21 | trh = np.vectorize(lambda x: 0.0 if x < 0.5 else 1.0) 22 | 23 | 24 | def weighted_log_loss(yt, p, w=None, eps=1e-15): 25 | if w is None: 26 | w_t = np.ones((yt.shape[0], 2)) 27 | else: 28 | w_t = np.vstack((w, 1.0 - w)).T 29 | 30 | Y_t = np.vstack((1.0 - yt.squeeze(), yt.squeeze())).T 31 | L_t = np.sum(w_t * Y_t * np.log(np.clip(p, eps, 1.0 - eps)), axis=1) 32 | 33 | return np.mean(L_t) 34 | 35 | 36 | def build_dataset(q): 37 | Y_unlabeled = trh(q) 38 | 39 | X_n = np.zeros((nb_samples, nb_dimensions)) 40 | X_n[0:nb_samples - nb_unlabeled] = X[Y.squeeze() != -1] 41 | X_n[nb_samples - nb_unlabeled:] = X[Y.squeeze() == -1] 42 | 43 | Y_n = np.zeros((nb_samples, 1)) 44 | Y_n[0:nb_samples - nb_unlabeled] = Y[Y.squeeze() != -1] 45 | Y_n[nb_samples - nb_unlabeled:] = np.expand_dims(Y_unlabeled, axis=1) 46 | 47 | return X_n, Y_n 48 | 49 | 50 | def log_likelihood(q): 51 | X_n, Y_n = build_dataset(q) 52 | Y_soft = trh(q) 53 | 54 | lr.fit(X_n, Y_n.squeeze()) 55 | 56 | p_sup = lr.predict_proba(X[Y.squeeze() != -1]) 57 | p_semi = lr.predict_proba(X[Y.squeeze() == -1]) 58 | 59 | l_sup = weighted_log_loss(Y[Y.squeeze() != -1], p_sup) 60 | l_semi = weighted_log_loss(Y_soft, p_semi, q) 61 | 62 | return l_semi - l_sup 63 | 64 | 65 | if __name__ == '__main__': 66 | # Load dataset 67 | X_a, Y_a = load_digits(return_X_y=True) 68 | 69 | # Select the subset containing all 0s and 1s 70 | X = np.vstack((X_a[Y_a == 0], X_a[Y_a == 1])) 71 | Y = np.vstack((np.expand_dims(Y_a, axis=1)[Y_a == 0], np.expand_dims(Y_a, axis=1)[Y_a == 1])) 72 | 73 | nb_samples = X.shape[0] 74 | nb_dimensions = X.shape[1] 75 | Y_true = np.zeros((nb_unlabeled,)) 76 | 77 | # Select nb_unlabeled samples 78 | unlabeled_idx = np.random.choice(np.arange(0, nb_samples, 1), replace=False, size=nb_unlabeled) 79 | Y_true = Y[unlabeled_idx].copy() 80 | Y[unlabeled_idx] = -1 81 | 82 | # Check the CV scores using only a Logistic Regression 83 | total_cv_scores = cross_val_score(LogisticRegression(), X, Y.squeeze(), cv=10) 84 | 85 | print('CV scores (only Logistic Regression)') 86 | print(total_cv_scores) 87 | 88 | # Train CPLE 89 | print('Training CPLE...') 90 | q_end = fmin_bfgs(f=log_likelihood, x0=q0, maxiter=5000, disp=False) 91 | 92 | # Build the final dataset 93 | X_n, Y_n = build_dataset(q_end) 94 | 95 | # Check the CV scores using CPLE 96 | final_semi_cv_scores = cross_val_score(LogisticRegression(), X_n, Y_n.squeeze(), cv=10) 97 | 98 | print('CV scores (CPLE)') 99 | print(final_semi_cv_scores) 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /Chapter02/generative_gaussian_mixtures.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from matplotlib.patches import Ellipse 5 | from sklearn.datasets import make_blobs 6 | from scipy.stats import multivariate_normal 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | 11 | nb_samples = 1000 12 | nb_unlabeled = 750 13 | nb_iterations = 5 14 | 15 | # First Gaussian 16 | m1 = np.random.uniform(-7.5, 10.0, size=2) 17 | c1 = np.random.uniform(5.0, 15.0, size=(2, 2)) 18 | c1 = np.dot(c1, c1.T) 19 | q1 = 0.5 20 | 21 | # Second Gaussian 22 | m2 = np.random.uniform(-7.5, 10.0, size=2) 23 | c2 = np.random.uniform(5.0, 15.0, size=(2, 2)) 24 | c2 = np.dot(c2, c2.T) 25 | q2 = 0.5 26 | 27 | 28 | def show_dataset(): 29 | fig, ax = plt.subplots(figsize=(20, 15)) 30 | 31 | g1 = Ellipse(xy=m1, width=3 * np.sqrt(c1[0, 0]), height=3 * np.sqrt(c1[1, 1]), fill=False, linestyle='dashed', 32 | linewidth=1) 33 | g1_1 = Ellipse(xy=m1, width=2 * np.sqrt(c1[0, 0]), height=2 * np.sqrt(c1[1, 1]), fill=False, linestyle='dashed', 34 | linewidth=2) 35 | g1_2 = Ellipse(xy=m1, width=1.4 * np.sqrt(c1[0, 0]), height=1.4 * np.sqrt(c1[1, 1]), fill=False, linestyle='dashed', 36 | linewidth=3) 37 | 38 | g2 = Ellipse(xy=m2, width=3 * np.sqrt(c2[0, 0]), height=3 * np.sqrt(c2[1, 1]), fill=False, linestyle='dashed', 39 | linewidth=1) 40 | g2_1 = Ellipse(xy=m2, width=2 * np.sqrt(c2[0, 0]), height=2 * np.sqrt(c2[1, 1]), fill=False, linestyle='dashed', 41 | linewidth=2) 42 | g2_2 = Ellipse(xy=m2, width=1.4 * np.sqrt(c2[0, 0]), height=1.4 * np.sqrt(c2[1, 1]), fill=False, linestyle='dashed', 43 | linewidth=3) 44 | 45 | ax.scatter(X[Y == 0, 0], X[Y == 0, 1], color='#88d7f0', s=100) 46 | ax.scatter(X[Y == 1, 0], X[Y == 1, 1], color='#55ffec', s=100) 47 | ax.scatter(X[Y == -1, 0], X[Y == -1, 1], color='r', marker='d', s=25) 48 | 49 | ax.add_artist(g1) 50 | ax.add_artist(g1_1) 51 | ax.add_artist(g1_2) 52 | ax.add_artist(g2) 53 | ax.add_artist(g2_1) 54 | ax.add_artist(g2_2) 55 | 56 | ax.set_xlabel(r'$x_0$') 57 | ax.set_ylabel(r'$x_1$') 58 | ax.grid() 59 | 60 | plt.show() 61 | 62 | 63 | if __name__ == '__main__': 64 | # Generate dataset 65 | X, Y = make_blobs(n_samples=nb_samples, n_features=2, centers=2, cluster_std=2.5, random_state=100) 66 | 67 | unlabeled_idx = np.random.choice(np.arange(0, nb_samples, 1), replace=False, size=nb_unlabeled) 68 | Y[unlabeled_idx] = -1 69 | 70 | # Show the dataset with the initial Gaussians 71 | show_dataset() 72 | 73 | # Training process 74 | for i in range(nb_iterations): 75 | Pij = np.zeros((nb_samples, 2)) 76 | 77 | # E Step 78 | for i in range(nb_samples): 79 | if Y[i] == -1: 80 | p1 = multivariate_normal.pdf(X[i], m1, c1, allow_singular=True) * q1 81 | p2 = multivariate_normal.pdf(X[i], m2, c2, allow_singular=True) * q2 82 | Pij[i] = [p1, p2] / (p1 + p2) 83 | 84 | else: 85 | Pij[i, :] = [1.0, 0.0] if Y[i] == 0 else [0.0, 1.0] 86 | 87 | # M Step 88 | n = np.sum(Pij, axis=0) 89 | m = np.sum(np.dot(Pij.T, X), axis=0) 90 | 91 | m1 = np.dot(Pij[:, 0], X) / n[0] 92 | m2 = np.dot(Pij[:, 1], X) / n[1] 93 | 94 | q1 = n[0] / float(nb_samples) 95 | q2 = n[1] / float(nb_samples) 96 | 97 | c1 = np.zeros((2, 2)) 98 | c2 = np.zeros((2, 2)) 99 | 100 | for t in range(nb_samples): 101 | c1 += Pij[t, 0] * np.outer(X[t] - m1, X[t] - m1) 102 | c2 += Pij[t, 1] * np.outer(X[t] - m2, X[t] - m2) 103 | 104 | c1 /= n[0] 105 | c2 /= n[1] 106 | 107 | # Show the final Gaussians 108 | show_dataset() 109 | 110 | # Check some points 111 | print('The first 10 unlabeled samples:') 112 | print(np.round(X[Y == -1][0:10], 3)) 113 | 114 | print('\nCorresponding Gaussian assigments:') 115 | print(np.round(Pij[Y == -1][0:10], 3)) -------------------------------------------------------------------------------- /Chapter02/s3vm.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from scipy.optimize import minimize 5 | 6 | from sklearn.datasets import make_classification 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | 11 | nb_samples = 500 12 | nb_unlabeled = 200 13 | 14 | 15 | # Create dataset 16 | X, Y = make_classification(n_samples=nb_samples, n_features=2, n_redundant=0, random_state=1000) 17 | Y[Y == 0] = -1 18 | Y[nb_samples - nb_unlabeled:nb_samples] = 0 19 | 20 | 21 | # Initialize S3VM variables 22 | w = np.random.uniform(-0.1, 0.1, size=X.shape[1]) 23 | eta = np.random.uniform(0.0, 0.1, size=nb_samples - nb_unlabeled) 24 | xi = np.random.uniform(0.0, 0.1, size=nb_unlabeled) 25 | zi = np.random.uniform(0.0, 0.1, size=nb_unlabeled) 26 | b = np.random.uniform(-0.1, 0.1, size=1) 27 | C = 1.0 28 | 29 | 30 | # Stack all variables into a single vector 31 | theta0 = np.hstack((w, eta, xi, zi, b)) 32 | 33 | 34 | # Vectorize the min() function 35 | vmin = np.vectorize(lambda x1, x2: x1 if x1 <= x2 else x2) 36 | 37 | 38 | def svm_target(theta, Xd, Yd): 39 | wt = theta[0:2].reshape((Xd.shape[1], 1)) 40 | 41 | s_eta = np.sum(theta[2:2 + nb_samples - nb_unlabeled]) 42 | s_min_xi_zi = np.sum(vmin(theta[2 + nb_samples - nb_unlabeled:2 + nb_samples], 43 | theta[2 + nb_samples:2 + nb_samples + nb_unlabeled])) 44 | 45 | return C * (s_eta + s_min_xi_zi) + 0.5 * np.dot(wt.T, wt) 46 | 47 | 48 | def labeled_constraint(theta, Xd, Yd, idx): 49 | wt = theta[0:2].reshape((Xd.shape[1], 1)) 50 | 51 | c = Yd[idx] * (np.dot(Xd[idx], wt) + theta[-1]) + \ 52 | theta[2:2 + nb_samples - nb_unlabeled][idx] - 1.0 53 | 54 | return (c >= 0)[0] 55 | 56 | 57 | def unlabeled_constraint_1(theta, Xd, idx): 58 | wt = theta[0:2].reshape((Xd.shape[1], 1)) 59 | 60 | c = np.dot(Xd[idx], wt) - theta[-1] + \ 61 | theta[2 + nb_samples - nb_unlabeled:2 + nb_samples][idx - nb_samples + nb_unlabeled] - 1.0 62 | 63 | return (c >= 0)[0] 64 | 65 | 66 | def unlabeled_constraint_2(theta, Xd, idx): 67 | wt = theta[0:2].reshape((Xd.shape[1], 1)) 68 | 69 | c = -(np.dot(Xd[idx], wt) - theta[-1]) + \ 70 | theta[2 + nb_samples:2 + nb_samples + nb_unlabeled][idx - nb_samples + nb_unlabeled] - 1.0 71 | 72 | return (c >= 0)[0] 73 | 74 | 75 | def eta_constraint(theta, idx): 76 | return theta[2:2 + nb_samples - nb_unlabeled][idx] >= 0 77 | 78 | 79 | def xi_constraint(theta, idx): 80 | return theta[2 + nb_samples - nb_unlabeled:2 + nb_samples][idx - nb_samples + nb_unlabeled] >= 0 81 | 82 | 83 | def zi_constraint(theta, idx): 84 | return theta[2 + nb_samples:2 + nb_samples+nb_unlabeled ][idx - nb_samples + nb_unlabeled] >= 0 85 | 86 | 87 | if __name__ == '__main__': 88 | # Show the initial dataset 89 | fig, ax = plt.subplots(figsize=(12, 9)) 90 | 91 | ax.scatter(X[Y == -1, 0], X[Y == -1, 1], color='#88d7f0', s=100) 92 | ax.scatter(X[Y == 1, 0], X[Y == 1, 1], color='#55ffec', s=100) 93 | ax.scatter(X[Y == 0, 0], X[Y == 0, 1], color='r', marker='x', s=50) 94 | 95 | ax.set_xlabel(r'$x_0$') 96 | ax.set_ylabel(r'$x_1$') 97 | ax.grid() 98 | 99 | plt.show() 100 | 101 | # Setup all the constraints 102 | svm_constraints = [] 103 | 104 | for i in range(nb_samples - nb_unlabeled): 105 | svm_constraints.append({ 106 | 'type': 'ineq', 107 | 'fun': labeled_constraint, 108 | 'args': (X, Y, i) 109 | }) 110 | svm_constraints.append({ 111 | 'type': 'ineq', 112 | 'fun': eta_constraint, 113 | 'args': (i,) 114 | }) 115 | 116 | for i in range(nb_samples - nb_unlabeled, nb_samples): 117 | svm_constraints.append({ 118 | 'type': 'ineq', 119 | 'fun': unlabeled_constraint_1, 120 | 'args': (X, i) 121 | }) 122 | svm_constraints.append({ 123 | 'type': 'ineq', 124 | 'fun': unlabeled_constraint_2, 125 | 'args': (X, i) 126 | }) 127 | svm_constraints.append({ 128 | 'type': 'ineq', 129 | 'fun': xi_constraint, 130 | 'args': (i,) 131 | }) 132 | svm_constraints.append({ 133 | 'type': 'ineq', 134 | 'fun': zi_constraint, 135 | 'args': (i,) 136 | }) 137 | 138 | # Optimize the objective 139 | print('Optimizing...') 140 | result = minimize(fun=svm_target, 141 | x0=theta0, 142 | constraints=svm_constraints, 143 | args=(X, Y), 144 | method='SLSQP', 145 | tol=0.0001, 146 | options={'maxiter': 1000}) 147 | 148 | # Extract the last parameters 149 | theta_end = result['x'] 150 | w = theta_end[0:2] 151 | b = theta_end[-1] 152 | 153 | Xu = X[nb_samples - nb_unlabeled:nb_samples] 154 | yu = -np.sign(np.dot(Xu, w) + b) 155 | 156 | # Show the final plots 157 | fig, ax = plt.subplots(1, 2, figsize=(18, 8)) 158 | 159 | ax[0].scatter(X[Y == -1, 0], X[Y == -1, 1], color='#88d7f0', marker='s', s=400) 160 | ax[0].scatter(X[Y == 1, 0], X[Y == 1, 1], color='#55ffec', marker='o', s=400) 161 | ax[0].scatter(X[Y == 0, 0], X[Y == 0, 1], color='r', marker='x', s=15) 162 | 163 | ax[0].set_xlabel(r'$x_0$') 164 | ax[0].set_ylabel(r'$x_1$') 165 | ax[0].grid() 166 | 167 | ax[1].scatter(X[Y == -1, 0], X[Y == -1, 1], color='#88d7f0', marker='s', s=50) 168 | ax[1].scatter(X[Y == 1, 0], X[Y == 1, 1], color='#55ffec', marker='o', s=50) 169 | 170 | ax[1].scatter(Xu[yu == -1, 0], Xu[yu == -1, 1], color='#88d7f0', marker='x', s=150) 171 | ax[1].scatter(Xu[yu == 1, 0], Xu[yu == 1, 1], color='#55ffec', marker='x', s=150) 172 | 173 | ax[1].set_xlabel(r'$x_0$') 174 | ax[1].set_ylabel(r'$x_1$') 175 | ax[1].grid() 176 | 177 | plt.show() -------------------------------------------------------------------------------- /Chapter02/tsvm.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from scipy.optimize import minimize 5 | 6 | from sklearn.datasets import make_classification 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | 11 | nb_samples = 500 12 | nb_unlabeled = 400 13 | 14 | 15 | # Create dataset 16 | X, Y = make_classification(n_samples=nb_samples, n_features=2, n_redundant=0, random_state=1000) 17 | Y[Y==0] = -1 18 | Y[nb_samples - nb_unlabeled:nb_samples] = 0 19 | 20 | 21 | # Initialize TSVM variables 22 | w = np.random.uniform(-0.1, 0.1, size=X.shape[1]) 23 | eta_labeled = np.random.uniform(0.0, 0.1, size=nb_samples - nb_unlabeled) 24 | eta_unlabeled = np.random.uniform(0.0, 0.1, size=nb_unlabeled) 25 | y_unlabeled = np.random.uniform(-1.0, 1.0, size=nb_unlabeled) 26 | b = np.random.uniform(-0.1, 0.1, size=1) 27 | 28 | C_labeled = 1.0 29 | C_unlabeled = 10.0 30 | 31 | 32 | # Stack all variables into a single vector 33 | theta0 = np.hstack((w, eta_labeled, eta_unlabeled, y_unlabeled, b)) 34 | 35 | 36 | def svm_target(theta, Xd, Yd): 37 | wt = theta[0:2].reshape((Xd.shape[1], 1)) 38 | 39 | s_eta_labeled = np.sum(theta[2:2 + nb_samples - nb_unlabeled]) 40 | s_eta_unlabeled = np.sum(theta[2 + nb_samples - nb_unlabeled:2 + nb_samples]) 41 | 42 | return (C_labeled * s_eta_labeled) + (C_unlabeled * s_eta_unlabeled) + (0.5 * np.dot(wt.T, wt)) 43 | 44 | 45 | def labeled_constraint(theta, Xd, Yd, idx): 46 | wt = theta[0:2].reshape((Xd.shape[1], 1)) 47 | 48 | c = Yd[idx] * (np.dot(Xd[idx], wt) + theta[-1]) + \ 49 | theta[2:2 + nb_samples - nb_unlabeled][idx] - 1.0 50 | 51 | return (c >= 0)[0] 52 | 53 | 54 | def unlabeled_constraint(theta, Xd, idx): 55 | wt = theta[0:2].reshape((Xd.shape[1], 1)) 56 | 57 | c = theta[2 + nb_samples:2 + nb_samples + nb_unlabeled][idx - nb_samples + nb_unlabeled] * \ 58 | (np.dot(Xd[idx], wt) + theta[-1]) + \ 59 | theta[2 + nb_samples - nb_unlabeled:2 + nb_samples][idx - nb_samples + nb_unlabeled] - 1.0 60 | 61 | return (c >= 0)[0] 62 | 63 | 64 | def eta_labeled_constraint(theta, idx): 65 | return theta[2:2 + nb_samples - nb_unlabeled][idx] >= 0 66 | 67 | 68 | def eta_unlabeled_constraint(theta, idx): 69 | return theta[2 + nb_samples - nb_unlabeled:2 + nb_samples][idx - nb_samples + nb_unlabeled] >= 0 70 | 71 | 72 | def y_constraint(theta, idx): 73 | return np.power(theta[2 + nb_samples:2 + nb_samples + nb_unlabeled][idx], 2) == 1.0 74 | 75 | 76 | if __name__ == '__main__': 77 | # Show the initial dataset 78 | fig, ax = plt.subplots(figsize=(12, 9)) 79 | 80 | ax.scatter(X[Y == -1, 0], X[Y == -1, 1], color='#88d7f0', s=100) 81 | ax.scatter(X[Y == 1, 0], X[Y == 1, 1], color='#55ffec', s=100) 82 | ax.scatter(X[Y == 0, 0], X[Y == 0, 1], color='r', marker='x', s=50) 83 | 84 | ax.set_xlabel(r'$x_0$') 85 | ax.set_ylabel(r'$x_1$') 86 | ax.grid() 87 | 88 | plt.show() 89 | 90 | # Setup all the constraints 91 | svm_constraints = [] 92 | 93 | for i in range(nb_samples - nb_unlabeled): 94 | svm_constraints.append({ 95 | 'type': 'ineq', 96 | 'fun': labeled_constraint, 97 | 'args': (X, Y, i) 98 | }) 99 | svm_constraints.append({ 100 | 'type': 'ineq', 101 | 'fun': eta_labeled_constraint, 102 | 'args': (i,) 103 | }) 104 | 105 | for i in range(nb_samples - nb_unlabeled, nb_samples): 106 | svm_constraints.append({ 107 | 'type': 'ineq', 108 | 'fun': unlabeled_constraint, 109 | 'args': (X, i) 110 | }) 111 | svm_constraints.append({ 112 | 'type': 'ineq', 113 | 'fun': eta_unlabeled_constraint, 114 | 'args': (i,) 115 | }) 116 | 117 | for i in range(nb_unlabeled): 118 | svm_constraints.append({ 119 | 'type': 'eq', 120 | 'fun': y_constraint, 121 | 'args': (i,) 122 | }) 123 | 124 | # Optimize the objective 125 | print('Optimizing...') 126 | result = minimize(fun=svm_target, 127 | x0=theta0, 128 | constraints=svm_constraints, 129 | args=(X, Y), 130 | method='SLSQP', 131 | tol=0.0001, 132 | options={'maxiter': 1000}) 133 | 134 | # Extract the last parameters 135 | theta_end = result['x'] 136 | w = theta_end[0:2] 137 | b = theta_end[-1] 138 | 139 | Xu = X[nb_samples - nb_unlabeled:nb_samples] 140 | yu = -np.sign(np.dot(Xu, w) + b) 141 | 142 | # Show the final plots 143 | fig, ax = plt.subplots(1, 2, figsize=(18, 8)) 144 | 145 | ax[0].scatter(X[Y == -1, 0], X[Y == -1, 1], color='#88d7f0', marker='s', s=400) 146 | ax[0].scatter(X[Y == 1, 0], X[Y == 1, 1], color='#55ffec', marker='o', s=400) 147 | ax[0].scatter(X[Y == 0, 0], X[Y == 0, 1], color='r', marker='x', s=15) 148 | 149 | ax[0].set_xlabel(r'$x_0$') 150 | ax[0].set_ylabel(r'$x_1$') 151 | ax[0].grid() 152 | 153 | ax[1].scatter(X[Y == -1, 0], X[Y == -1, 1], color='#88d7f0', marker='s', s=50) 154 | ax[1].scatter(X[Y == 1, 0], X[Y == 1, 1], color='#55ffec', marker='o', s=50) 155 | 156 | ax[1].scatter(Xu[yu == -1, 0], Xu[yu == -1, 1], color='#88d7f0', marker='x', s=150) 157 | ax[1].scatter(Xu[yu == 1, 0], Xu[yu == 1, 1], color='#55ffec', marker='x', s=150) 158 | 159 | ax[1].set_xlabel(r'$x_0$') 160 | ax[1].set_ylabel(r'$x_1$') 161 | ax[1].grid() 162 | 163 | plt.show() -------------------------------------------------------------------------------- /Chapter03/Old/label_spreading.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import make_classification 5 | from sklearn.semi_supervised import LabelSpreading 6 | 7 | # Set random seed for reproducibility 8 | np.random.seed(1000) 9 | 10 | 11 | nb_samples = 5000 12 | nb_unlabeled = 1000 13 | 14 | 15 | if __name__ == '__main__': 16 | # Create the dataset 17 | X, Y = make_classification(n_samples=nb_samples, n_features=2, n_informative=2, n_redundant=0, random_state=100) 18 | Y[nb_samples - nb_unlabeled:nb_samples] = -1 19 | 20 | # Create and fit a LabelSpreading instance 21 | ls = LabelSpreading(kernel='rbf', gamma=10.0, alpha=0.2) 22 | ls.fit(X, Y) 23 | 24 | Y_final = ls.predict(X) 25 | 26 | # Show the final result 27 | fig, ax = plt.subplots(1, 2, figsize=(18, 8)) 28 | 29 | ax[0].scatter(X[Y == 0, 0], X[Y == 0, 1], color='#88d7f0', marker='s', s=100) 30 | ax[0].scatter(X[Y == 1, 0], X[Y == 1, 1], color='#55ffec', marker='o', s=100) 31 | ax[0].scatter(X[Y == -1, 0], X[Y == -1, 1], color='r', marker='x', s=20) 32 | 33 | ax[0].set_xlabel(r'$x_0$') 34 | ax[0].set_ylabel(r'$x_1$') 35 | ax[0].set_title('Dataset') 36 | ax[0].grid() 37 | 38 | ax[1].scatter(X[Y_final == 0, 0], X[Y_final == 0, 1], color='#88d7f0', marker='s', s=100) 39 | ax[1].scatter(X[Y_final == 1, 0], X[Y_final == 1, 1], color='#55ffec', marker='o', s=100) 40 | 41 | ax[1].set_xlabel(r'$x_0$') 42 | ax[1].set_ylabel(r'$x_1$') 43 | ax[1].set_title(r'Scikit-Learn Label Spreading ($\alpha$=0.2)') 44 | ax[1].grid() 45 | 46 | plt.show() -------------------------------------------------------------------------------- /Chapter03/isomap.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import fetch_olivetti_faces 5 | from sklearn.manifold import Isomap 6 | 7 | # Set random seed for reproducibility 8 | np.random.seed(1000) 9 | 10 | 11 | if __name__ == '__main__': 12 | # Create the dataset 13 | faces = fetch_olivetti_faces() 14 | 15 | # Train Isomap 16 | isomap = Isomap(n_neighbors=5, n_components=2) 17 | X_isomap = isomap.fit_transform(faces['data']) 18 | 19 | # Plot the result 20 | fig, ax = plt.subplots(figsize=(18, 10)) 21 | 22 | for i in range(100): 23 | ax.scatter(X_isomap[i, 0], X_isomap[i, 1], marker='o', s=100) 24 | ax.annotate('%d' % faces['target'][i], xy=(X_isomap[i, 0] + 0.5, X_isomap[i, 1] + 0.5)) 25 | 26 | ax.set_xlabel(r'$x_0$') 27 | ax.set_ylabel(r'$x_1$') 28 | ax.grid() 29 | 30 | plt.show() -------------------------------------------------------------------------------- /Chapter03/label_propagation.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import make_classification 5 | 6 | # Set random seed for reproducibility 7 | np.random.seed(1000) 8 | 9 | 10 | nb_samples=100 11 | nb_unlabeled = 75 12 | tolerance = 0.01 13 | 14 | 15 | def rbf(x1, x2, gamma=10.0): 16 | n = np.linalg.norm(x1 - x2, ord=1) 17 | return np.exp(-gamma * np.power(n, 2)) 18 | 19 | 20 | if __name__ == '__main__': 21 | # Create the dataset 22 | X, Y = make_classification(n_samples=nb_samples, n_features=2, n_informative=2, n_redundant=0, random_state=1000) 23 | Y[Y == 0] = -1 24 | Y[nb_samples - nb_unlabeled:nb_samples] = 0 25 | 26 | # Show the original dataset 27 | fig, ax = plt.subplots(figsize=(12, 9)) 28 | 29 | ax.scatter(X[Y == -1, 0], X[Y == -1, 1], color='#88d7f0', marker='s', s=100) 30 | ax.scatter(X[Y == 1, 0], X[Y == 1, 1], color='#55ffec', marker='o', s=100) 31 | ax.scatter(X[Y == 0, 0], X[Y == 0, 1], color='r', marker='x', s=50) 32 | 33 | ax.set_xlabel(r'$x_0$') 34 | ax.set_ylabel(r'$x_1$') 35 | ax.grid() 36 | 37 | plt.show() 38 | 39 | # Compute W 40 | W_rbf = np.zeros((nb_samples, nb_samples)) 41 | 42 | for i in range(nb_samples): 43 | for j in range(nb_samples): 44 | W_rbf[i, j] = rbf(X[i], X[j]) 45 | 46 | # Compute D and its inverse 47 | D_rbf = np.diag(np.sum(W_rbf, axis=1)) 48 | D_rbf_inv = np.linalg.inv(D_rbf) 49 | 50 | # Perform the label propagation 51 | Yt = Y.copy() 52 | Y_prev = np.zeros((nb_samples,)) 53 | iterations = 0 54 | 55 | while np.linalg.norm(Yt - Y_prev, ord=1) > tolerance: 56 | P = np.dot(D_rbf_inv, W_rbf) 57 | Yt = np.dot(P, Yt) 58 | Yt[0:nb_samples - nb_unlabeled] = Y[0:nb_samples - nb_unlabeled] 59 | Y_prev = Yt.copy() 60 | 61 | Y_final = np.sign(Yt) 62 | 63 | # Show the final result 64 | fig, ax = plt.subplots(1, 2, figsize=(18, 8)) 65 | 66 | ax[0].scatter(X[Y == -1, 0], X[Y == -1, 1], color='#88d7f0', marker='s', s=100) 67 | ax[0].scatter(X[Y == 1, 0], X[Y == 1, 1], color='#55ffec', marker='o', s=100) 68 | ax[0].scatter(X[Y == 0, 0], X[Y == 0, 1], color='r', marker='x', s=50) 69 | 70 | ax[0].set_xlabel(r'$x_0$') 71 | ax[0].set_ylabel(r'$x_1$') 72 | ax[0].set_title('Dataset') 73 | ax[0].grid() 74 | 75 | ax[1].scatter(X[Y_final == -1, 0], X[Y_final == -1, 1], color='#88d7f0', marker='s', s=100) 76 | ax[1].scatter(X[Y_final == 1, 0], X[Y_final == 1, 1], color='#55ffec', marker='o', s=100) 77 | 78 | ax[1].set_xlabel(r'$x_0$') 79 | ax[1].set_ylabel(r'$x_1$') 80 | ax[1].set_title('Label Propagation') 81 | ax[1].grid() 82 | 83 | plt.show() 84 | -------------------------------------------------------------------------------- /Chapter03/label_propagation_mrw.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import make_blobs 5 | from sklearn.neighbors import kneighbors_graph 6 | 7 | # Set random seed for reproducibility 8 | np.random.seed(1000) 9 | 10 | nb_samples = 2000 11 | nb_unlabeled = 1950 12 | nb_classes = 2 13 | 14 | 15 | def rbf(x1, x2, sigma=1.0): 16 | d = np.linalg.norm(x1 - x2, ord=1) 17 | return np.exp(-np.power(d, 2.0) / (2 * np.power(sigma, 2))) 18 | 19 | 20 | if __name__ == '__main__': 21 | # Create the dataset 22 | X, Y = make_blobs(n_samples=nb_samples, 23 | n_features=2, 24 | centers=nb_classes, 25 | cluster_std=2.5, 26 | random_state=500) 27 | 28 | Y[nb_samples - nb_unlabeled:] = -1 29 | 30 | # Show the original dataset 31 | fig, ax = plt.subplots(figsize=(12, 9)) 32 | 33 | ax.scatter(X[Y == -1, 0], X[Y == -1, 1], color='r', marker='x', s=50) 34 | ax.scatter(X[Y == 1, 0], X[Y == 1, 1], color='b', marker='o', s=100) 35 | ax.scatter(X[Y == 0, 0], X[Y == 0, 1], color='g', marker='s', s=100) 36 | 37 | ax.set_xlabel(r'$x_0$') 38 | ax.set_ylabel(r'$x_1$') 39 | ax.grid() 40 | 41 | plt.show() 42 | 43 | # Create the weight matrix 44 | W = kneighbors_graph(X, n_neighbors=15, mode='connectivity', include_self=True).toarray() 45 | 46 | for i in range(nb_samples): 47 | for j in range(nb_samples): 48 | if W[i, j] != 0.0: 49 | W[i, j] = rbf(X[i], X[j]) 50 | 51 | # Compute the Laplacian 52 | D = np.diag(np.sum(W, axis=1)) 53 | L = D - W 54 | Luu = L[nb_samples - nb_unlabeled:, nb_samples - nb_unlabeled:] 55 | Wul = W[nb_samples - nb_unlabeled:, 0:nb_samples - nb_unlabeled,] 56 | Yl = Y[0:nb_samples - nb_unlabeled] 57 | 58 | # Perform the random walk 59 | Yu = np.round(np.linalg.solve(Luu, np.dot(Wul, Yl))) 60 | Y[nb_samples - nb_unlabeled:] = Yu.copy() 61 | 62 | # Show the final dataset 63 | fig, ax = plt.subplots(figsize=(12, 9)) 64 | 65 | ax.scatter(X[Y == 1, 0], X[Y == 1, 1], color='b', marker='o', s=100) 66 | ax.scatter(X[Y == 0, 0], X[Y == 0, 1], color='g', marker='s', s=100) 67 | 68 | ax.set_xlabel(r'$x_0$') 69 | ax.set_ylabel(r'$x_1$') 70 | ax.grid() 71 | 72 | plt.show() 73 | -------------------------------------------------------------------------------- /Chapter03/label_propagation_scikit_learn.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import make_classification 5 | from sklearn.semi_supervised import LabelPropagation 6 | 7 | # Set random seed for reproducibility 8 | np.random.seed(1000) 9 | 10 | 11 | nb_samples = 1000 12 | nb_unlabeled = 750 13 | 14 | 15 | if __name__ == '__main__': 16 | # Create the dataset 17 | X, Y = make_classification(n_samples=nb_samples, n_features=2, n_informative=2, n_redundant=0, random_state=100) 18 | Y[nb_samples - nb_unlabeled:nb_samples] = -1 19 | 20 | # Create a LabelPropagation instance and fit it 21 | lp = LabelPropagation(kernel='rbf', gamma=10.0) 22 | lp.fit(X, Y) 23 | 24 | Y_final = lp.predict(X) 25 | 26 | # Show the final result 27 | fig, ax = plt.subplots(1, 2, figsize=(18, 8)) 28 | 29 | ax[0].scatter(X[Y == 0, 0], X[Y == 0, 1], color='#88d7f0', marker='s', s=100) 30 | ax[0].scatter(X[Y == 1, 0], X[Y == 1, 1], color='#55ffec', marker='o', s=100) 31 | ax[0].scatter(X[Y == -1, 0], X[Y == -1, 1], color='r', marker='x', s=50) 32 | 33 | ax[0].set_xlabel(r'$x_0$') 34 | ax[0].set_ylabel(r'$x_1$') 35 | ax[0].set_title('Dataset') 36 | ax[0].grid() 37 | 38 | ax[1].scatter(X[Y_final == 0, 0], X[Y_final == 0, 1], color='#88d7f0', marker='s', s=100) 39 | ax[1].scatter(X[Y_final == 1, 0], X[Y_final == 1, 1], color='#55ffec', marker='o', s=100) 40 | 41 | ax[1].set_xlabel(r'$x_0$') 42 | ax[1].set_ylabel(r'$x_1$') 43 | ax[1].set_title('Scikit-Learn Label Propagation') 44 | ax[1].grid() 45 | 46 | plt.show() 47 | 48 | -------------------------------------------------------------------------------- /Chapter03/label_spreading.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import scipy.sparse.csgraph 4 | 5 | from sklearn.datasets import make_classification 6 | from sklearn.semi_supervised import LabelSpreading 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | 11 | 12 | nb_samples = 5000 13 | nb_unlabeled = 1000 14 | 15 | 16 | if __name__ == '__main__': 17 | # Create the dataset 18 | X, Y = make_classification(n_samples=nb_samples, n_features=2, n_informative=2, n_redundant=0, random_state=100) 19 | Y[nb_samples - nb_unlabeled:nb_samples] = -1 20 | 21 | # Create and fit a LabelSpreading instance 22 | ls = LabelSpreading(kernel='rbf', gamma=10.0, alpha=0.2) 23 | ls.fit(X, Y) 24 | 25 | Y_final = ls.predict(X) 26 | 27 | # Show the final result 28 | fig, ax = plt.subplots(1, 2, figsize=(18, 8)) 29 | 30 | ax[0].scatter(X[Y == 0, 0], X[Y == 0, 1], color='#88d7f0', marker='s', s=100) 31 | ax[0].scatter(X[Y == 1, 0], X[Y == 1, 1], color='#55ffec', marker='o', s=100) 32 | ax[0].scatter(X[Y == -1, 0], X[Y == -1, 1], color='r', marker='x', s=20) 33 | 34 | ax[0].set_xlabel(r'$x_0$') 35 | ax[0].set_ylabel(r'$x_1$') 36 | ax[0].set_title('Dataset') 37 | ax[0].grid() 38 | 39 | ax[1].scatter(X[Y_final == 0, 0], X[Y_final == 0, 1], color='#88d7f0', marker='s', s=100) 40 | ax[1].scatter(X[Y_final == 1, 0], X[Y_final == 1, 1], color='#55ffec', marker='o', s=100) 41 | 42 | ax[1].set_xlabel(r'$x_0$') 43 | ax[1].set_ylabel(r'$x_1$') 44 | ax[1].set_title(r'Scikit-Learn Label Spreading ($\alpha$=0.2)') 45 | ax[1].grid() 46 | 47 | plt.show() 48 | -------------------------------------------------------------------------------- /Chapter03/laplacian_spectral_embedding.py: -------------------------------------------------------------------------------- 1 | import matplotlib.cm as cm 2 | import matplotlib.pyplot as plt 3 | import numpy as np 4 | 5 | from sklearn.datasets import fetch_olivetti_faces 6 | from sklearn.manifold import SpectralEmbedding 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | 11 | 12 | if __name__ == '__main__': 13 | # Create the dataset 14 | faces = fetch_olivetti_faces() 15 | 16 | # Train Laplacian Spectral Embedding 17 | se = SpectralEmbedding(n_components=2, n_neighbors=15) 18 | X_se = se.fit_transform(faces['data']) 19 | 20 | # Plot the result 21 | fig, ax = plt.subplots(figsize=(18, 10)) 22 | 23 | for i in range(400): 24 | ax.scatter(X_se[:, 0], X_se[:, 1], color=cm.rainbow(faces['target'] * 10), marker='o', s=30) 25 | ax.annotate('%d' % faces['target'][i], xy=(X_se[i, 0] + 0.001, X_se[i, 1] + 0.001)) 26 | 27 | ax.set_xlim([-0.15, 0.0]) 28 | ax.set_ylim([-0.2, 0.4]) 29 | ax.set_xlabel(r'$x_0$') 30 | ax.set_ylabel(r'$x_1$') 31 | ax.grid() 32 | 33 | plt.show() -------------------------------------------------------------------------------- /Chapter03/locally_linear_embedding.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import fetch_olivetti_faces 5 | from sklearn.manifold import LocallyLinearEmbedding 6 | 7 | # Set random seed for reproducibility 8 | np.random.seed(1000) 9 | 10 | 11 | if __name__ == '__main__': 12 | # Create the dataset 13 | faces = fetch_olivetti_faces() 14 | 15 | # Train LLE 16 | lle = LocallyLinearEmbedding(n_neighbors=15, n_components=2) 17 | X_lle = lle.fit_transform(faces['data']) 18 | 19 | # Plot the result 20 | fig, ax = plt.subplots(figsize=(18, 10)) 21 | 22 | for i in range(100): 23 | ax.scatter(X_lle[i, 0], X_lle[i, 1], marker='o', s=100) 24 | ax.annotate('%d' % faces['target'][i], xy=(X_lle[i, 0] + 0.0015, X_lle[i, 1] + 0.0015)) 25 | 26 | ax.set_xlabel(r'$x_0$') 27 | ax.set_ylabel(r'$x_1$') 28 | ax.grid() 29 | 30 | plt.show() -------------------------------------------------------------------------------- /Chapter03/tsne.py: -------------------------------------------------------------------------------- 1 | import matplotlib.cm as cm 2 | import matplotlib.pyplot as plt 3 | import numpy as np 4 | 5 | from sklearn.datasets import fetch_olivetti_faces 6 | from sklearn.manifold import TSNE 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | 11 | 12 | if __name__ == '__main__': 13 | # Create the dataset 14 | faces = fetch_olivetti_faces() 15 | 16 | # Train TSNE 17 | tsne = TSNE(n_components=2, perplexity=20) 18 | X_tsne = tsne.fit_transform(faces['data']) 19 | 20 | # Plot the result 21 | fig, ax = plt.subplots(figsize=(18, 10)) 22 | 23 | for i in range(400): 24 | ax.scatter(X_tsne[:, 0], X_tsne[:, 1], color=cm.rainbow(faces['target'] * 10), marker='o', s=20) 25 | ax.annotate('%d' % faces['target'][i], xy=(X_tsne[i, 0] + 1, X_tsne[i, 1] + 1)) 26 | 27 | ax.set_xlabel(r'$x_0$') 28 | ax.set_ylabel(r'$x_1$') 29 | ax.grid() 30 | 31 | plt.show() -------------------------------------------------------------------------------- /Chapter04/direct_sampling.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # Set random seed for reproducibility 4 | np.random.seed(1000) 5 | 6 | 7 | N = 4 8 | Nsamples = 50000 9 | 10 | 11 | def X1_sample(p=0.35): 12 | return np.random.binomial(1, p) 13 | 14 | 15 | def X2_sample(p=0.65): 16 | return np.random.binomial(1, p) 17 | 18 | 19 | def X3_sample(x1, x2, p1=0.75, p2=0.4): 20 | if x1 == 1 and x2 == 1: 21 | return np.random.binomial(1, p1) 22 | else: 23 | return np.random.binomial(1, p2) 24 | 25 | 26 | def X4_sample(x3, p1=0.65, p2=0.5): 27 | if x3 == 1: 28 | return np.random.binomial(1, p1) 29 | else: 30 | return np.random.binomial(1, p2) 31 | 32 | 33 | if __name__ == '__main__': 34 | # Initialize the sample frequency dictionary 35 | Fsamples = {} 36 | 37 | # Main loop 38 | for t in range(Nsamples): 39 | x1 = X1_sample() 40 | x2 = X2_sample() 41 | x3 = X3_sample(x1, x2) 42 | x4 = X4_sample(x3) 43 | 44 | sample = (x1, x2, x3, x4) 45 | 46 | if sample in Fsamples: 47 | Fsamples[sample] += 1 48 | else: 49 | Fsamples[sample] = 1 50 | 51 | # Compute the probabilities 52 | samples = np.array(list(Fsamples.keys()), dtype=np.bool_) 53 | probabilities = np.array(list(Fsamples.values()), dtype=np.float64) / Nsamples 54 | 55 | # Show the probabilities 56 | for i in range(len(samples)): 57 | print('P{} = {}'.format(samples[i], probabilities[i])) 58 | -------------------------------------------------------------------------------- /Chapter04/hmm.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from hmmlearn import hmm 4 | 5 | # Set random seed for reproducibility 6 | np.random.seed(1000) 7 | 8 | 9 | if __name__ == '__main__': 10 | # Create a Multinomial HMM 11 | hmm_model = hmm.MultinomialHMM(n_components=2, n_iter=100, random_state=1000) 12 | 13 | # Define a list of observations 14 | observations = np.array([[0], [1], [1], [0], [1], [1], [1], [0], [1], 15 | [0], [0], [0], [1], [0], [1], [1], [0], [1], 16 | [0], [0], [1], [0], [1], [0], [0], [0], [1], 17 | [0], [1], [0], [1], [0], [0], [0], [0], [0]], dtype=np.int32) 18 | 19 | # Fit the model using the Forward-Backward algorithm 20 | hmm_model.fit(observations) 21 | 22 | # Check the convergence 23 | print('Converged: {}'.format(hmm_model.monitor_.converged)) 24 | 25 | # Print the transition probability matrix 26 | print('\nTransition probability matrix:') 27 | print(hmm_model.transmat_) 28 | 29 | # Create a test sequence 30 | sequence = np.array([[1], [1], [1], [0], [1], [1], [1], [0], [1], 31 | [0], [1], [0], [1], [0], [1], [1], [0], [1], 32 | [1], [0], [1], [0], [1], [0], [1], [0], [1], 33 | [1], [1], [0], [0], [1], [1], [0], [1], [1]], dtype=np.int32) 34 | 35 | # Find the the most likely hidden states using the Viterbi algorithm 36 | lp, hs = hmm_model.decode(sequence) 37 | 38 | print('\nMost likely hidden state sequence:') 39 | print(hs) 40 | 41 | print('\nLog-propability:') 42 | print(lp) 43 | 44 | # Compute the posterior probabilities 45 | pp = hmm_model.predict_proba(sequence) 46 | 47 | print('\nPosterior probabilities:') 48 | print(pp) 49 | -------------------------------------------------------------------------------- /Chapter04/metropolis-hastings-sampling.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | # Set random seed for reproducibility 5 | np.random.seed(1000) 6 | 7 | 8 | nb_iterations = 100000 9 | x = 1.0 10 | samples = [] 11 | 12 | 13 | def prior(x): 14 | return 0.1 * np.exp(-0.1 * x) 15 | 16 | 17 | def likelihood(x): 18 | a = np.sqrt(0.2 / (2.0 * np.pi * np.power(x, 3))) 19 | b = - (0.2 * np.power(x - 1.0, 2)) / (2.0 * x) 20 | return a * np.exp(b) 21 | 22 | 23 | def g(x): 24 | return likelihood(x) * prior(x) 25 | 26 | 27 | def q(xp): 28 | return np.random.normal(xp) 29 | 30 | 31 | if __name__ == '__main__': 32 | # Main loop 33 | for i in range(nb_iterations): 34 | xc = q(x) 35 | 36 | alpha = g(xc) / g(x) 37 | if np.isnan(alpha): 38 | continue 39 | 40 | if alpha >= 1: 41 | samples.append(xc) 42 | x = xc 43 | else: 44 | if np.random.uniform(0.0, 1.0) < alpha: 45 | samples.append(xc) 46 | x = xc 47 | 48 | # Generate the histogram 49 | hist, _ = np.histogram(samples, bins=100) 50 | hist_p = hist / len(samples) 51 | 52 | # Show the histogram 53 | fig, ax = plt.subplots(figsize=(15, 8)) 54 | 55 | ax.plot(hist_p) 56 | ax.grid() 57 | ax.set_xlabel('x') 58 | ax.set_ylabel('p(x)') 59 | 60 | plt.show() -------------------------------------------------------------------------------- /Chapter04/pymc3_example.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import pymc3 as pm 4 | import pymc3.distributions.continuous as pmc 5 | import pymc3.distributions.discrete as pmd 6 | import pymc3.math as pmm 7 | 8 | # PyMC 3 Installation instructions (https://github.com/pymc-devs/pymc3) 9 | # Pip: pip install pymc3 10 | # Conda: conda install -c conda-forge pymc3 11 | # 12 | # In case of issues with h5py with an Anaconda distribution, please update the package: conda install h5py 13 | 14 | # Set random seed for reproducibility 15 | np.random.seed(1000) 16 | 17 | 18 | nb_samples = 500 19 | 20 | 21 | if __name__ == '__main__': 22 | # Create a PyMC3 model 23 | model = pm.Model() 24 | 25 | # Define the model structure 26 | with model: 27 | passenger_onboarding = pmc.Wald('Passenger Onboarding', mu=0.5, lam=0.2) 28 | refueling = pmc.Wald('Refueling', mu=0.25, lam=0.5) 29 | departure_traffic_delay = pmc.Wald('Departure Traffic Delay', mu=0.1, lam=0.2) 30 | 31 | departure_time = pm.Deterministic('Departure Time', 32 | 12.0 + departure_traffic_delay + 33 | pmm.switch(passenger_onboarding >= refueling, 34 | passenger_onboarding, 35 | refueling)) 36 | 37 | rough_weather = pmd.Bernoulli('Rough Weather', p=0.35) 38 | 39 | flight_time = pmc.Exponential('Flight Time', lam=0.5 - (0.1 * rough_weather)) 40 | arrival_traffic_delay = pmc.Wald('Arrival Traffic Delay', mu=0.1, lam=0.2) 41 | 42 | arrival_time = pm.Deterministic('Arrival time', 43 | departure_time + 44 | flight_time + 45 | arrival_traffic_delay) 46 | 47 | # Sample from the model 48 | # On Windows with Anaconda 3.5 there can be an issue with joblib, therefore I recommend to set n_jobs=1 49 | with model: 50 | samples = pm.sample(draws=nb_samples, njobs=1, random_seed=1000) 51 | 52 | # Plot the summary 53 | pm.summary(samples) 54 | 55 | # Show the diagrams 56 | fig, ax = plt.subplots(8, 2, figsize=(14, 18)) 57 | 58 | pm.traceplot(samples, ax=ax) 59 | 60 | for i in range(8): 61 | for j in range(2): 62 | ax[i, j].grid() 63 | 64 | ax[2, 0].set_xlim([0.05, 1.0]) 65 | ax[3, 0].set_xlim([0.05, 0.4]) 66 | ax[4, 0].set_xlim([12, 16]) 67 | ax[5, 0].set_xlim([0, 10]) 68 | ax[6, 0].set_xlim([0.05, 0.4]) 69 | ax[7, 0].set_xlim([14, 20]) 70 | 71 | plt.show() 72 | 73 | -------------------------------------------------------------------------------- /Chapter05/factor_analysis.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.covariance import LedoitWolf 5 | from sklearn.datasets import fetch_mldata 6 | from sklearn.decomposition import FactorAnalysis 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | 11 | 12 | def zero_center(Xd): 13 | return Xd - np.mean(Xd, axis=0) 14 | 15 | 16 | if __name__ == '__main__': 17 | # Load the dataset 18 | digits = fetch_mldata('MNIST original') 19 | X = zero_center(digits['data'].astype(np.float64)) 20 | np.random.shuffle(X) 21 | 22 | # Create dataset + heteroscedastic noise 23 | Omega = np.random.uniform(0.0, 0.75, size=X.shape[1]) 24 | Xh = X + np.random.normal(0.0, Omega, size=X.shape) 25 | 26 | # Show dataset + heteroscedastic noise plot 27 | fig, ax = plt.subplots(10, 10, figsize=(10, 10)) 28 | 29 | for i in range(10): 30 | for j in range(10): 31 | ax[i, j].imshow(Xh[(i * 10) + j].reshape((28, 28)), cmap='gray') 32 | ax[i, j].axis('off') 33 | 34 | plt.show() 35 | 36 | # Perform Factor analysis 37 | fa = FactorAnalysis(n_components=64, random_state=1000) 38 | fah = FactorAnalysis(n_components=64, random_state=1000) 39 | 40 | Xfa = fa.fit_transform(X) 41 | Xfah = fah.fit_transform(Xh) 42 | 43 | print('Factor analysis score X: {}'.format(fa.score(X))) 44 | print('Factor analysis score Xh: {}'.format(fah.score(Xh))) 45 | 46 | # Perform Lodoit-Wolf shrinkage 47 | ldw = LedoitWolf() 48 | ldwh = LedoitWolf() 49 | 50 | ldw.fit(X) 51 | ldwh.fit(Xh) 52 | 53 | print('Ledoit-Wolf score X: {}'.format(ldw.score(X))) 54 | print('Ledoit-Wolf score Xh: {}'.format(ldwh.score(Xh))) 55 | 56 | # Show the components 57 | fig, ax = plt.subplots(8, 8, figsize=(10, 10)) 58 | 59 | for i in range(8): 60 | for j in range(8): 61 | ax[i, j].imshow(fah.components_[(i * 8) + j].reshape((28, 28)), cmap='gray') 62 | ax[i, j].axis('off') 63 | 64 | plt.show() 65 | -------------------------------------------------------------------------------- /Chapter05/gaussian_mixture.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import make_blobs 5 | from sklearn.mixture import GaussianMixture 6 | 7 | # Set random seed for reproducibility 8 | np.random.seed(1000) 9 | 10 | 11 | nb_samples = 1000 12 | 13 | 14 | if __name__ == '__main__': 15 | # Create the dataset 16 | X, Y = make_blobs(n_samples=nb_samples, n_features=2, centers=3, cluster_std=1.5, random_state=1000) 17 | 18 | # Show the unclustered dataset 19 | fig, ax = plt.subplots(figsize=(15, 8)) 20 | 21 | ax.scatter(X[:, 0], X[:, 1], s=40) 22 | ax.grid() 23 | ax.set_xlabel(r'$x_0$') 24 | ax.set_ylabel(r'$x_1$') 25 | 26 | plt.show() 27 | 28 | # Create and fit a Gaussian Mixture 29 | gm = GaussianMixture(n_components=3) 30 | gm.fit(X) 31 | 32 | # Show the Gaussian parameters 33 | print('Weights:\n') 34 | print(gm.weights_) 35 | 36 | print('\nMeans:\n') 37 | print(gm.means_) 38 | 39 | print('\nCovariances:\n') 40 | print(gm.covariances_) 41 | 42 | # Show the clustered dataset 43 | Yp = gm.predict(X) 44 | 45 | fig, ax = plt.subplots(figsize=(15, 8)) 46 | 47 | ax.scatter(X[Yp == 0, 0], X[Yp == 0, 1], c='red', marker='o', s=50) 48 | ax.scatter(X[Yp == 1, 0], X[Yp == 1, 1], c='blue', marker='x', s=50) 49 | ax.scatter(X[Yp == 2, 0], X[Yp == 2, 1], c='green', marker='s', s=50) 50 | ax.grid() 51 | ax.set_xlabel(r'$x_0$') 52 | ax.set_ylabel(r'$x_1$') 53 | 54 | plt.show() -------------------------------------------------------------------------------- /Chapter05/ica.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import fetch_mldata 5 | from sklearn.decomposition import FastICA 6 | 7 | # Set random seed for reproducibility 8 | np.random.seed(1000) 9 | 10 | 11 | def zero_center(Xd): 12 | return Xd - np.mean(Xd, axis=0) 13 | 14 | 15 | if __name__ == '__main__': 16 | # Load the dataset 17 | digits = fetch_mldata('MNIST original') 18 | X = zero_center(digits['data'].astype(np.float64)) 19 | np.random.shuffle(X) 20 | 21 | # Peform Fast ICA with 64 components 22 | fastica = FastICA(n_components=64, max_iter=5000, random_state=1000) 23 | fastica.fit(X) 24 | 25 | # Plot the indipendent components 26 | fig, ax = plt.subplots(8, 8, figsize=(11, 11)) 27 | 28 | for i in range(8): 29 | for j in range(8): 30 | ax[i, j].imshow(fastica.components_[(i * 8) + j].reshape((28, 28)), cmap='gray') 31 | ax[i, j].axis('off') 32 | 33 | plt.show() 34 | 35 | # Peform Fast ICA with 640 components 36 | fastica = FastICA(n_components=640, max_iter=5000, random_state=1000) 37 | fastica.fit(X) 38 | 39 | # Plot the indipendent components 40 | fig, ax = plt.subplots(10, 10, figsize=(11, 11)) 41 | 42 | for i in range(10): 43 | for j in range(10): 44 | ax[i, j].imshow(fastica.components_[(i * 10) + j].reshape((28, 28)), cmap='gray') 45 | ax[i, j].axis('off') 46 | 47 | plt.show() 48 | 49 | -------------------------------------------------------------------------------- /Chapter05/parameter_estimation.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # Set random seed for reproducibility 4 | np.random.seed(1000) 5 | 6 | 7 | def theta(theta_prev, z1=50.0, x3=10.0): 8 | num = (8.0 * z1 * theta_prev) + (4.0 * x3 * (12.0 - theta_prev)) 9 | den = (z1 + x3) * (12.0 - theta_prev) 10 | return num / den 11 | 12 | 13 | if __name__ == '__main__': 14 | theta_v = 0.01 15 | 16 | for i in range(1000): 17 | theta_v = theta(theta_v) 18 | 19 | # Final theta 20 | print(theta_v) 21 | 22 | # Probability vector 23 | p = [theta_v / 6.0, (1 - (theta_v / 4.0)), theta_v / 12.0] 24 | 25 | print(p) -------------------------------------------------------------------------------- /Chapter05/pca.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import fetch_mldata 5 | from sklearn.decomposition import PCA 6 | 7 | # Set random seed for reproducibility 8 | np.random.seed(1000) 9 | 10 | 11 | def zero_center(Xd): 12 | return Xd - np.mean(Xd, axis=0) 13 | 14 | 15 | if __name__ == '__main__': 16 | # Load the dataset 17 | digits = fetch_mldata('MNIST original') 18 | X = zero_center(digits['data'].astype(np.float64)) 19 | np.random.shuffle(X) 20 | 21 | # Create dataset + heteroscedastic noise 22 | Omega = np.random.uniform(0.0, 0.75, size=X.shape[1]) 23 | Xh = X + np.random.normal(0.0, Omega, size=X.shape) 24 | 25 | # Show dataset + heteroscedastic noise plot 26 | fig, ax = plt.subplots(10, 10, figsize=(10, 10)) 27 | 28 | for i in range(10): 29 | for j in range(10): 30 | ax[i, j].imshow(Xh[(i * 10) + j].reshape((28, 28)), cmap='gray') 31 | ax[i, j].axis('off') 32 | 33 | plt.show() 34 | 35 | # Perform a PCA on the dataset + heteroscedastic noise 36 | pca = PCA(n_components=64, svd_solver='full', random_state=1000) 37 | Xpca = pca.fit_transform(Xh) 38 | 39 | print('PCA score: {}'.format(pca.score(Xh))) 40 | print('Explained variance ratio: {}'.format(np.sum(pca.explained_variance_ratio_))) 41 | 42 | # Show the explained variance plot 43 | ev = pca.explained_variance_ratio_ 44 | 45 | fig, ax = plt.subplots(figsize=(10, 6)) 46 | 47 | ax.bar(np.arange(0, len(ev), 1), ev) 48 | ax.set_xlabel('Components') 49 | ax.set_ylabel('Explained variance ratio') 50 | 51 | plt.show() 52 | 53 | 54 | -------------------------------------------------------------------------------- /Chapter06/SOM.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import fetch_olivetti_faces 5 | from sklearn.preprocessing import StandardScaler 6 | 7 | # Set random seed for reproducibility 8 | np.random.seed(1000) 9 | 10 | 11 | nb_iterations = 1000 12 | nb_startup_iterations = 500 13 | pattern_length = 64 * 64 14 | pattern_width = pattern_height = 64 15 | eta0 = 1.0 16 | sigma0 = 3.0 17 | tau = 100.0 18 | matrix_side = 5 19 | 20 | W = np.random.normal(0, 0.1, size=(matrix_side, matrix_side, pattern_length)) 21 | precomputed_distances = np.zeros((matrix_side, matrix_side, matrix_side, matrix_side)) 22 | 23 | 24 | def winning_unit(xt): 25 | global W 26 | distances = np.linalg.norm(W - xt, ord=2, axis=2) 27 | max_activation_unit = np.argmax(distances) 28 | return int(np.floor(max_activation_unit / matrix_side)), max_activation_unit % matrix_side 29 | 30 | 31 | def eta(t): 32 | return eta0 * np.exp(-float(t) / tau) 33 | 34 | 35 | def sigma(t): 36 | return float(sigma0) * np.exp(-float(t) / tau) 37 | 38 | 39 | def distance_matrix(xt, yt, sigmat): 40 | global precomputed_distances 41 | dm = precomputed_distances[xt, yt, :, :] 42 | de = 2.0 * np.power(sigmat, 2) 43 | return np.exp(-dm / de) 44 | 45 | 46 | if __name__ == '__main__': 47 | # Load the dataset 48 | faces = fetch_olivetti_faces(shuffle=True) 49 | Xcomplete = faces['data'].astype(np.float64) / np.max(faces['data']) 50 | np.random.shuffle(Xcomplete) 51 | X = Xcomplete[0:100] 52 | 53 | # Pre-compute distances 54 | for i in range(matrix_side): 55 | for j in range(matrix_side): 56 | for k in range(matrix_side): 57 | for t in range(matrix_side): 58 | precomputed_distances[i, j, k, t] = np.power(float(i) - float(k), 2) + np.power(float(j) - float(t), 2) 59 | 60 | # Perform training cycle 61 | sequence = np.arange(0, X.shape[0]) 62 | t = 0 63 | 64 | for e in range(nb_iterations): 65 | np.random.shuffle(sequence) 66 | t += 1 67 | 68 | if e < nb_startup_iterations: 69 | etat = eta(t) 70 | sigmat = sigma(t) 71 | else: 72 | etat = 0.2 73 | sigmat = 1.0 74 | 75 | for n in sequence: 76 | x_sample = X[n] 77 | 78 | xw, yw = winning_unit(x_sample) 79 | dm = distance_matrix(xw, yw, sigmat) 80 | 81 | dW = etat * np.expand_dims(dm, axis=2) * (x_sample - W) 82 | W += dW 83 | 84 | W /= np.linalg.norm(W, axis=2).reshape((matrix_side, matrix_side, 1)) 85 | 86 | if e > 0 and e % 100 == 0: 87 | print(t) 88 | 89 | # Show the final W matrix 90 | sc = StandardScaler(with_std=False) 91 | Ws = sc.fit_transform(W.reshape((matrix_side * matrix_side, pattern_length))) 92 | 93 | matrix_w = np.zeros((matrix_side * pattern_height, matrix_side * pattern_width)) 94 | 95 | Ws = Ws.reshape((matrix_side, matrix_side, pattern_length)) 96 | 97 | for i in range(matrix_side): 98 | for j in range(matrix_side): 99 | matrix_w[i * pattern_height:i * pattern_height + pattern_height, 100 | j * pattern_height:j * pattern_height + pattern_width] = W[i, j].reshape((pattern_height, pattern_width)) * 255.0 101 | 102 | fig, ax = plt.subplots(figsize=(5, 5)) 103 | 104 | ax.matshow(matrix_w.tolist(), cmap='gray') 105 | ax.set_xticks([]) 106 | ax.set_yticks([]) 107 | plt.show() -------------------------------------------------------------------------------- /Chapter06/covariance_rule.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | # Set random seed for reproducibility 5 | np.random.seed(1000) 6 | 7 | 8 | if __name__ == '__main__': 9 | # Create the dataset 10 | rs = np.random.RandomState(1000) 11 | X = rs.normal(loc=1.0, scale=(20.0, 1.0), size=(1000, 2)) 12 | 13 | w = np.array([30.0, 3.0]) 14 | w0 = w.copy() 15 | 16 | # Plot the initial w position 17 | fig, ax = plt.subplots(figsize=(15, 10)) 18 | 19 | ax.scatter(X[:, 0], X[:, 1], s=20, color='#cccccc') 20 | ax.set_xlabel(r'$x_0$') 21 | ax.set_ylabel(r'$x_1$') 22 | plt.ylim([-10, 10]) 23 | ax.grid() 24 | ax.arrow(0, 0, 30.0, 3.0, head_width=1.0, head_length=2.0, fc='k', ec='k') 25 | ax.annotate(r'$w_0$', xy=(10.0, 10.0), xycoords='data', xytext=(25.0, 3.5), textcoords='data', size=18) 26 | plt.show() 27 | 28 | S = np.cov(X.T) 29 | 30 | for i in range(10): 31 | w += np.dot(S, w) 32 | w /= np.linalg.norm(w) 33 | 34 | w *= 50.0 35 | 36 | print('Final w: {}'.format(np.round(w, 1))) 37 | 38 | # Plot the final configuration 39 | fig, ax = plt.subplots(figsize=(15, 8)) 40 | 41 | ax.scatter(X[:, 0], X[:, 1], s=20, color='#cccccc') 42 | ax.set_xlabel(r'$x_0$') 43 | ax.set_ylabel(r'$x_1$') 44 | ax.grid() 45 | 46 | ax.arrow(0, 0, w0[0], w0[1], head_width=1.0, head_length=2.0, fc='k', ec='k') 47 | ax.annotate(r'$w_0$', xy=(1.0, 1.0), xycoords='data', xytext=(w0[0] - 6.0, w0[1] + 0.5), textcoords='data', size=20) 48 | 49 | ax.arrow(0, 0, w[0], w[1], head_width=1.0, head_length=2.0, fc='k', ec='k') 50 | ax.annotate(r'$w_\infty$', xy=(1.0, 1.0), xycoords='data', xytext=(w[0] - 6.0, w[1] + 0.5), textcoords='data', 51 | size=20) 52 | 53 | plt.ylim([-10, 10]) 54 | plt.show() 55 | 56 | -------------------------------------------------------------------------------- /Chapter06/hebb_rule.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import numpy as np 3 | 4 | # Set random seed for reproducibility 5 | np.random.seed(1000) 6 | 7 | 8 | if __name__ == '__main__': 9 | # Define initial vectors 10 | w = np.array([1.0, 0.2]) 11 | x = np.array([0.1, 0.5]) 12 | alpha = 0.0 13 | 14 | print('Initial w: {}'.format(w)) 15 | 16 | # Perform the iterations 17 | for i in range(50): 18 | y = np.dot(w, x.T) 19 | w += x * y 20 | alpha = np.arccos(np.dot(w, x.T) / (np.linalg.norm(w) * np.linalg.norm(x))) 21 | 22 | print('Final w: {}'.format(w)) 23 | print('Final alpha: {}'.format(alpha * 180.0 / np.pi)) 24 | 25 | # Repeat the test with alpha greater than 90° 26 | w = np.array([1.0, -1.0]) 27 | 28 | print('Initial w: {}'.format(w)) 29 | 30 | # Perform the iterations 31 | for i in range(50): 32 | y = np.dot(w, x.T) 33 | w += x * y 34 | alpha = np.arccos(np.dot(w, x.T) / (np.linalg.norm(w) * np.linalg.norm(x))) 35 | 36 | print('Final w: {}'.format(w)) 37 | print('Final alpha: {}'.format(alpha * 180.0 / np.pi)) -------------------------------------------------------------------------------- /Chapter06/rubner_tavan_network.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from sklearn.datasets import make_blobs 4 | 5 | # Set random seed for reproducibility 6 | np.random.seed(1000) 7 | 8 | 9 | n_components = 2 10 | learning_rate = 0.0001 11 | max_iterations = 1000 12 | stabilization_cycles = 5 13 | threshold = 0.00001 14 | 15 | 16 | def zero_center(Xd): 17 | return Xd - np.mean(Xd, axis=0) 18 | 19 | 20 | if __name__ == '__main__': 21 | # Create the dataset 22 | X, _ = make_blobs(n_samples=500, centers=2, cluster_std=5.0, random_state=1000) 23 | Xs = zero_center(X) 24 | 25 | Q = np.cov(Xs.T) 26 | eigu, eigv = np.linalg.eig(Q) 27 | 28 | print('Eigenvalues: {}'.format(eigu)) 29 | print('Eigenvectors: {}'.format(eigv)) 30 | 31 | # Initialize the variables 32 | W = np.random.normal(0.0, 0.5, size=(Xs.shape[1], n_components)) 33 | V = np.tril(np.random.normal(0.0, 0.01, size=(n_components, n_components))) 34 | np.fill_diagonal(V, 0.0) 35 | 36 | prev_W = np.zeros((Xs.shape[1], n_components)) 37 | t = 0 38 | 39 | # Perform the training cycle 40 | while (np.linalg.norm(W - prev_W, ord='fro') > threshold and t < max_iterations): 41 | prev_W = W.copy() 42 | t += 1 43 | 44 | for i in range(Xs.shape[0]): 45 | y_p = np.zeros((n_components, 1)) 46 | xi = np.expand_dims(Xs[i], 1) 47 | y = None 48 | 49 | for _ in range(stabilization_cycles): 50 | y = np.dot(W.T, xi) + np.dot(V, y_p) 51 | y_p = y.copy() 52 | 53 | dW = np.zeros((Xs.shape[1], n_components)) 54 | dV = np.zeros((n_components, n_components)) 55 | 56 | for t in range(n_components): 57 | y2 = np.power(y[t], 2) 58 | dW[:, t] = np.squeeze((y[t] * xi) + (y2 * np.expand_dims(W[:, t], 1))) 59 | dV[t, :] = -np.squeeze((y[t] * y) + (y2 * np.expand_dims(V[t, :], 1))) 60 | 61 | W += (learning_rate * dW) 62 | V += (learning_rate * dV) 63 | 64 | V = np.tril(V) 65 | np.fill_diagonal(V, 0.0) 66 | 67 | W /= np.linalg.norm(W, axis=0).reshape((1, n_components)) 68 | 69 | print('Final w: {}'.format(W)) 70 | 71 | # Compute the covariance matrix 72 | Y_comp = np.zeros((Xs.shape[0], n_components)) 73 | 74 | for i in range(Xs.shape[0]): 75 | y_p = np.zeros((n_components, 1)) 76 | xi = np.expand_dims(Xs[i], 1) 77 | 78 | for _ in range(stabilization_cycles): 79 | Y_comp[i] = np.squeeze(np.dot(W.T, xi) + np.dot(V.T, y_p)) 80 | y_p = y.copy() 81 | 82 | print('Final covariance matrix: {}'.format(np.cov(Y_comp.T))) -------------------------------------------------------------------------------- /Chapter06/sanger_network.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import make_blobs 5 | 6 | # Set random seed for reproducibility 7 | np.random.seed(1000) 8 | 9 | 10 | n_components = 2 11 | learning_rate = 0.01 12 | nb_iterations = 5000 13 | t = 0.0 14 | 15 | 16 | def zero_center(Xd): 17 | return Xd - np.mean(Xd, axis=0) 18 | 19 | 20 | if __name__ == '__main__': 21 | # Create the dataset 22 | X, _ = make_blobs(n_samples=500, centers=2, cluster_std=5.0, random_state=1000) 23 | Xs = zero_center(X) 24 | 25 | Q = np.cov(Xs.T) 26 | eigu, eigv = np.linalg.eig(Q) 27 | 28 | print('Eigenvalues: {}'.format(eigu)) 29 | print('Eigenvectors: {}'.format(eigv)) 30 | 31 | # Initialize the weights 32 | W_sanger = np.random.normal(scale=0.5, size=(n_components, Xs.shape[1])) 33 | W_sanger /= np.linalg.norm(W_sanger, axis=1).reshape((n_components, 1)) 34 | 35 | # Perform the training cycle 36 | for i in range(nb_iterations): 37 | dw = np.zeros((n_components, Xs.shape[1])) 38 | t += 1.0 39 | 40 | for j in range(Xs.shape[0]): 41 | Ysj = np.dot(W_sanger, Xs[j]).reshape((n_components, 1)) 42 | QYd = np.tril(np.dot(Ysj, Ysj.T)) 43 | dw += np.dot(Ysj, Xs[j].reshape((1, X.shape[1]))) - np.dot(QYd, W_sanger) 44 | 45 | W_sanger += (learning_rate / t) * dw 46 | W_sanger /= np.linalg.norm(W_sanger, axis=1).reshape((n_components, 1)) 47 | 48 | print('Final weights: {}'.format(W_sanger.T)) 49 | 50 | # Plot the final configuration 51 | fig, ax = plt.subplots(figsize=(10, 10)) 52 | 53 | ax.scatter(Xs[:, 0], Xs[:, 1], c='blue') 54 | ax.set_xlabel(r'$x_0$') 55 | ax.set_xlabel(r'$x_1$') 56 | W = W_sanger * 15 57 | 58 | ax.arrow(0, 0, W[0, 0], W[0, 1], head_width=1.0, head_length=2.0, fc='k', ec='k') 59 | ax.annotate(r'$w_0$', xy=(1.0, 1.0), xycoords='data', xytext=(W[0, 0] + 0.5, W[0, 1] + 0.5), textcoords='data', 60 | size=20) 61 | 62 | ax.arrow(0, 0, W[1, 0], W[1, 1], head_width=1.0, head_length=2.0, fc='k', ec='k') 63 | ax.annotate(r'$w_1$', xy=(1.0, 1.0), xycoords='data', xytext=(W[1, 0] + 0.5, W[1, 1] + 0.5), textcoords='data', 64 | size=20) 65 | 66 | ax.grid() 67 | plt.show() -------------------------------------------------------------------------------- /Chapter07/evaluation_metrics.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import matplotlib.cm as cm 3 | import numpy as np 4 | 5 | from sklearn.datasets import load_digits 6 | from sklearn.cluster import KMeans 7 | from sklearn.metrics import homogeneity_score, completeness_score, adjusted_rand_score, silhouette_score, silhouette_samples 8 | 9 | 10 | # Set random seed for reproducibility 11 | np.random.seed(1000) 12 | 13 | 14 | if __name__ == '__main__': 15 | # Load the dataset 16 | digits = load_digits() 17 | X_train = digits['data'] / np.max(digits['data']) 18 | 19 | # Perform K-Means with 10 clusters 20 | km = KMeans(n_clusters=10, random_state=1000) 21 | Y = km.fit_predict(X_train) 22 | 23 | print('Homogeneity score: {}'.format(homogeneity_score(digits['target'], Y))) 24 | print('Completeness score: {}'.format(completeness_score(digits['target'], Y))) 25 | print('Adjusted Rand score: {}'.format(adjusted_rand_score(digits['target'], Y))) 26 | print('Silhouette score: {}'.format(silhouette_score(X_train, Y, metric='euclidean'))) 27 | 28 | # Plot silhouette plots 29 | fig, ax = plt.subplots(2, 2, figsize=(15, 10)) 30 | 31 | nb_clusters = [3, 5, 10, 12] 32 | mapping = [(0, 0), (0, 1), (1, 0), (1, 1)] 33 | 34 | for i, n in enumerate(nb_clusters): 35 | km = KMeans(n_clusters=n, random_state=1000) 36 | Y = km.fit_predict(X_train) 37 | 38 | silhouette_values = silhouette_samples(X_train, Y) 39 | 40 | ax[mapping[i]].set_xticks([-0.15, 0.0, 0.25, 0.5, 0.75, 1.0]) 41 | ax[mapping[i]].set_yticks([]) 42 | ax[mapping[i]].set_title('%d clusters' % n) 43 | ax[mapping[i]].set_xlim([-0.15, 1]) 44 | ax[mapping[i]].grid() 45 | y_lower = 20 46 | 47 | for t in range(n): 48 | ct_values = silhouette_values[Y == t] 49 | ct_values.sort() 50 | 51 | y_upper = y_lower + ct_values.shape[0] 52 | 53 | color = cm.Accent(float(t) / n) 54 | ax[mapping[i]].fill_betweenx(np.arange(y_lower, y_upper), 0, ct_values, facecolor=color, edgecolor=color) 55 | 56 | y_lower = y_upper + 20 57 | 58 | plt.show() -------------------------------------------------------------------------------- /Chapter07/fuzzy_cmeans.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | # To install Scikit-Fuzzy (if not already installed): pip install -U scikit-fuzzy 5 | # Further instructions: https://pythonhosted.org/scikit-fuzzy/ 6 | from skfuzzy.cluster import cmeans, cmeans_predict 7 | 8 | from sklearn.datasets import load_digits 9 | 10 | 11 | # Set random seed for reproducibility 12 | np.random.seed(1000) 13 | 14 | 15 | if __name__ == '__main__': 16 | # Load the dataset 17 | digits = load_digits() 18 | X_train = digits['data'] / np.max(digits['data']) 19 | 20 | # Perform Fuzzy C-Means 21 | fc, W, _, _, _, _, pc = cmeans(X_train.T, c=10, m=1.25, error=1e-6, maxiter=10000, seed=1000) 22 | 23 | print('Partition coeffiecient: {}'.format(pc)) 24 | 25 | # Plot the centroids 26 | fig, ax = plt.subplots(1, 10, figsize=(10, 10)) 27 | 28 | for i in range(10): 29 | c = fc[i] 30 | ax[i].matshow(c.reshape(8, 8) * 255.0, cmap='gray') 31 | ax[i].set_xticks([]) 32 | ax[i].set_yticks([]) 33 | 34 | plt.show() 35 | 36 | # Membership degrees of a sample representing the digit '7' 37 | print('Membership degrees: {}'.format(W[:, 7])) 38 | 39 | fig, ax = plt.subplots(figsize=(15, 8)) 40 | 41 | ax.plot(np.arange(10), W[:, 7]) 42 | ax.set_xlabel('Cluster index') 43 | ax.set_ylabel('Fuzzy membership') 44 | ax.grid() 45 | 46 | plt.show() 47 | 48 | # Perform a prediction 49 | new_sample = np.expand_dims(X_train[7], axis=1) 50 | Wn, _, _, _, _, _ = cmeans_predict(new_sample, cntr_trained=fc, m=1.25, error=1e-6, maxiter=10000, seed=1000) 51 | 52 | print('Membership degrees: {}'.format(Wn.T)) 53 | 54 | -------------------------------------------------------------------------------- /Chapter07/k_nearest_neighbors.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import load_digits 5 | from sklearn.neighbors import NearestNeighbors 6 | 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | 11 | 12 | if __name__ == '__main__': 13 | # Load the dataset 14 | digits = load_digits() 15 | X_train = digits['data'] / np.max(digits['data']) 16 | 17 | # Perform kNN 18 | knn = NearestNeighbors(n_neighbors=50, algorithm='ball_tree') 19 | knn.fit(X_train) 20 | 21 | # Query the model 22 | distances, neighbors = knn.kneighbors(X_train[100].reshape(1, -1), return_distance=True) 23 | 24 | print('Distances: {}'.format(distances[0])) 25 | 26 | # Plot the neighbors 27 | fig, ax = plt.subplots(5, 10, figsize=(8, 8)) 28 | 29 | for y in range(5): 30 | for x in range(10): 31 | idx = neighbors[0][(x + (y * 10))] 32 | ax[y, x].matshow(digits['images'][idx], cmap='gray') 33 | ax[y, x].set_xticks([]) 34 | ax[y, x].set_yticks([]) 35 | 36 | plt.show() 37 | 38 | -------------------------------------------------------------------------------- /Chapter07/kmeans.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import matplotlib.cm as cm 3 | import numpy as np 4 | 5 | from sklearn.datasets import load_digits 6 | from sklearn.cluster import KMeans 7 | from sklearn.manifold import TSNE 8 | 9 | 10 | # Set random seed for reproducibility 11 | np.random.seed(1000) 12 | 13 | 14 | min_nb_clusters = 2 15 | max_nb_clusters = 20 16 | 17 | 18 | if __name__ == '__main__': 19 | # Load the dataset 20 | digits = load_digits() 21 | X_train = digits['data'] / np.max(digits['data']) 22 | 23 | # Compute the inertias 24 | inertias = np.zeros(shape=(max_nb_clusters - min_nb_clusters + 1,)) 25 | 26 | for i in range(min_nb_clusters, max_nb_clusters + 1): 27 | km = KMeans(n_clusters=i, random_state=1000) 28 | km.fit(X_train) 29 | inertias[i - min_nb_clusters] = km.inertia_ 30 | 31 | # Plot the inertias 32 | fig, ax = plt.subplots(figsize=(12, 7)) 33 | 34 | ax.plot(np.arange(2, max_nb_clusters + 1), inertias) 35 | ax.set_xlabel('Number of clusters') 36 | ax.set_ylabel('Inertia') 37 | ax.set_xticks(np.arange(2, max_nb_clusters + 1)) 38 | ax.grid() 39 | plt.show() 40 | 41 | # Perform K-Means with 10 clusters 42 | km = KMeans(n_clusters=10, random_state=1000) 43 | Y = km.fit_predict(X_train) 44 | 45 | # Show the centroids 46 | fig, ax = plt.subplots(1, 10, figsize=(10, 10)) 47 | 48 | for i in range(10): 49 | c = km.cluster_centers_[i] 50 | ax[i].matshow(c.reshape(8, 8) * 255.0, cmap='gray') 51 | ax[i].set_xticks([]) 52 | ax[i].set_yticks([]) 53 | 54 | plt.show() 55 | 56 | # Perform t-SNE on the clustered dataset 57 | tsne = TSNE(n_components=2, perplexity=20.0, random_state=1000) 58 | X_tsne = tsne.fit_transform(X_train) 59 | 60 | fig, ax = plt.subplots(figsize=(18, 10)) 61 | 62 | # Show the t-SNE clustered dataset 63 | for i in range(X_tsne.shape[0]): 64 | ax.scatter(X_tsne[i, 0], X_tsne[i, 1], marker='o', color=cm.Pastel1(Y[i]), s=150) 65 | ax.annotate('%d' % Y[i], xy=(X_tsne[i, 0] - 0.5, X_tsne[i, 1] - 0.5)) 66 | 67 | ax.set_xlabel(r'$x_0$') 68 | ax.set_ylabel(r'$x_1$') 69 | ax.grid() 70 | 71 | plt.show() 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /Chapter07/metrics.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from scipy.spatial.distance import pdist 4 | 5 | # Set random seed for reproducibility 6 | np.random.seed(1000) 7 | 8 | nb_samples = 100 9 | nb_bins = 100 10 | 11 | 12 | def max_min_mean(p=1.0, d=2): 13 | Xs = np.random.uniform(0.0, 1.0, size=(nb_bins, nb_samples, d)) 14 | 15 | pd_max = np.zeros(shape=(nb_bins,)) 16 | pd_min = np.zeros(shape=(nb_bins,)) 17 | 18 | for i in range(nb_bins): 19 | pd = pdist(Xs[i], metric='minkowski', p=p) 20 | pd_max[i] = np.max(pd) 21 | pd_min[i] = np.min(pd) 22 | 23 | return np.mean(pd_max - pd_min) 24 | 25 | 26 | if __name__ == '__main__': 27 | print('P=1 -> {}'.format(max_min_mean(p=1.0))) 28 | print('P=2 -> {}'.format(max_min_mean(p=2.0))) 29 | print('P=10 -> {}'.format(max_min_mean(p=10.0))) 30 | print('P=100 -> {}'.format(max_min_mean(p=100.0))) 31 | 32 | 33 | print('P=1 -> {}'.format(max_min_mean(p=1.0, d=100))) 34 | print('P=2 -> {}'.format(max_min_mean(p=2.0, d=100))) 35 | print('P=10 -> {}'.format(max_min_mean(p=10.0, d=100))) 36 | print('P=100 -> {}'.format(max_min_mean(p=100.0, d=100))) 37 | 38 | 39 | print('P=1 -> {}'.format(max_min_mean(p=1.0, d=1000))) 40 | print('P=2 -> {}'.format(max_min_mean(p=2.0, d=1000))) 41 | print('P=10 -> {}'.format(max_min_mean(p=10.0, d=1000))) 42 | print('P=100 -> {}'.format(max_min_mean(p=100.0, d=1000))) 43 | -------------------------------------------------------------------------------- /Chapter07/spectral_clustering.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.cluster import KMeans, SpectralClustering 5 | from sklearn.preprocessing import StandardScaler 6 | 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | 11 | 12 | nb_samples = 1000 13 | 14 | 15 | if __name__ == '__main__': 16 | # Create the dataset 17 | X = np.zeros(shape=(nb_samples, 2)) 18 | 19 | for i in range(nb_samples): 20 | X[i, 0] = float(i) 21 | 22 | if i % 2 == 0: 23 | X[i, 1] = 1.0 + (np.random.uniform(0.65, 1.0) * np.sin(float(i) / 100.0)) 24 | else: 25 | X[i, 1] = 0.1 + (np.random.uniform(0.5, 0.85) * np.sin(float(i) / 100.0)) 26 | 27 | ss = StandardScaler() 28 | Xs = ss.fit_transform(X) 29 | 30 | # Test K-Means 31 | km = KMeans(n_clusters=2, random_state=1000) 32 | Y_km = km.fit_predict(Xs) 33 | 34 | # Plot the result 35 | fig, ax = plt.subplots(figsize=(16, 8)) 36 | 37 | ax.scatter(Xs[Y_km == 0, 0], Xs[Y_km == 0, 1]) 38 | ax.scatter(Xs[Y_km == 1, 0], Xs[Y_km == 1, 1]) 39 | 40 | ax.set_xlabel(r'$x_0$') 41 | ax.set_ylabel(r'$x_1$') 42 | ax.grid() 43 | plt.show() 44 | 45 | # Apply Spectral clustering 46 | sc = SpectralClustering(n_clusters=2, affinity='nearest_neighbors', n_neighbors=20, random_state=1000) 47 | Y_sc = sc.fit_predict(Xs) 48 | 49 | # Plot the result 50 | fig, ax = plt.subplots(figsize=(16, 8)) 51 | 52 | ax.scatter(Xs[Y_sc == 0, 0], Xs[Y_sc == 0, 1]) 53 | ax.scatter(Xs[Y_sc == 1, 0], Xs[Y_sc == 1, 1]) 54 | 55 | ax.set_xlabel(r'$x_0$') 56 | ax.set_ylabel(r'$x_1$') 57 | ax.grid() 58 | plt.show() 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /Chapter08/adaboost.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import load_wine 5 | from sklearn.decomposition import PCA, FactorAnalysis 6 | from sklearn.ensemble import AdaBoostClassifier 7 | from sklearn.model_selection import cross_val_score 8 | 9 | 10 | # Set random seed for reproducibility 11 | np.random.seed(1000) 12 | 13 | 14 | if __name__ == '__main__': 15 | # Load the dataset 16 | X, Y = load_wine(return_X_y=True) 17 | 18 | # Compute the CV scores for different number of estimators 19 | scores_ne = [] 20 | 21 | for ne in range(10, 201, 10): 22 | adc = AdaBoostClassifier(n_estimators=ne, learning_rate=0.8, random_state=1000) 23 | scores_ne.append(np.mean(cross_val_score(adc, X, Y, cv=10))) 24 | 25 | # Plot CV scores 26 | fig, ax = plt.subplots(figsize=(15, 7)) 27 | 28 | ax.plot(list(range(10, 201, 10)), scores_ne) 29 | ax.set_xlabel('Number of estimators') 30 | ax.set_ylabel('10-fold Cross-Validation Accuracy') 31 | ax.grid() 32 | plt.show() 33 | 34 | # Compute the CV scores for different learning rates 35 | scores_eta_adc = [] 36 | 37 | for eta in np.linspace(0.01, 1.0, 100): 38 | adc = AdaBoostClassifier(n_estimators=50, learning_rate=eta, random_state=1000) 39 | scores_eta_adc.append(np.mean(cross_val_score(adc, X, Y, cv=10))) 40 | 41 | # Plot CV scores 42 | fig, ax = plt.subplots(figsize=(15, 8)) 43 | 44 | ax.plot(list(np.linspace(0.01, 1.0, 100)), scores_eta_adc) 45 | ax.set_xlabel('Learning rate') 46 | ax.set_ylabel('10-fold Cross-Validation Accuracy') 47 | ax.grid() 48 | plt.show() 49 | 50 | # Perform PCA and Factor Analysis 51 | scores_pca = [] 52 | 53 | for i in range(13, 1, -1): 54 | if i < 12: 55 | pca = PCA(n_components=i, random_state=1000) 56 | X_pca = pca.fit_transform(X) 57 | else: 58 | X_pca = X 59 | 60 | adc = AdaBoostClassifier(n_estimators=50, learning_rate=0.8, random_state=1000) 61 | scores_pca.append(np.mean(cross_val_score(adc, X_pca, Y, cv=10))) 62 | 63 | scores_fa = [] 64 | 65 | for i in range(13, 1, -1): 66 | if i < 12: 67 | fa = FactorAnalysis(n_components=i, random_state=1000) 68 | X_fa = fa.fit_transform(X) 69 | else: 70 | X_fa = X 71 | 72 | adc = AdaBoostClassifier(n_estimators=50, learning_rate=0.8, random_state=1000) 73 | scores_fa.append(np.mean(cross_val_score(adc, X_fa, Y, cv=10))) 74 | 75 | # Plot the results 76 | fig, ax = plt.subplots(figsize=(15, 8)) 77 | 78 | ax.plot([i for i in range(2, X.shape[1] + 1)], scores_fa[::-1], label='Factor Analysis') 79 | ax.plot([i for i in range(2, X.shape[1] + 1)], scores_pca[::-1], label='PCA') 80 | ax.set_xlabel('Number of components') 81 | ax.set_ylabel('10-fold Cross-Validation Accuracy') 82 | ax.legend() 83 | ax.grid() 84 | plt.show() -------------------------------------------------------------------------------- /Chapter08/gradient_tree_boosting.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from sklearn.datasets import load_wine 5 | from sklearn.ensemble import GradientBoostingClassifier 6 | from sklearn.model_selection import cross_val_score 7 | 8 | 9 | # Set random seed for reproducibility 10 | np.random.seed(1000) 11 | 12 | 13 | if __name__ == '__main__': 14 | # Load the dataset 15 | X, Y = load_wine(return_X_y=True) 16 | 17 | # Perform Gradient Tree Boosting with different tree depths 18 | scores_md = [] 19 | eta = 0.8 20 | 21 | for md in range(2, 13): 22 | gbc = GradientBoostingClassifier(n_estimators=50, learning_rate=eta, max_depth=md, random_state=1000) 23 | scores_md.append(np.mean(cross_val_score(gbc, X, Y, cv=10))) 24 | 25 | # Plot the results 26 | fig, ax = plt.subplots(figsize=(15, 8)) 27 | 28 | ax.plot(list(range(2, 13)), scores_md) 29 | ax.set_xlabel('Maximum Tree Depth') 30 | ax.set_ylabel('10-fold Cross-Validation Accuracy') 31 | ax.grid() 32 | plt.show() 33 | 34 | # Perform Gradient Tree Boosting with different learning rates 35 | scores_eta = [] 36 | 37 | for eta in np.linspace(0.01, 1.0, 100): 38 | gbr = GradientBoostingClassifier(n_estimators=50, learning_rate=eta, max_depth=2, random_state=1000) 39 | scores_eta.append(np.mean(cross_val_score(gbr, X, Y, cv=10))) 40 | 41 | # Plot the results 42 | fig, ax = plt.subplots(figsize=(15, 8)) 43 | 44 | ax.plot(np.linspace(0.01, 1.0, 100), scores_eta) 45 | ax.set_xlabel('Learning rate') 46 | ax.set_ylabel('10-fold Cross-Validation Accuracy') 47 | ax.grid() 48 | plt.show() -------------------------------------------------------------------------------- /Chapter08/random_forest.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from multiprocessing import cpu_count 5 | 6 | from sklearn.datasets import load_wine 7 | from sklearn.ensemble import RandomForestClassifier 8 | from sklearn.feature_selection import SelectFromModel 9 | from sklearn.model_selection import cross_val_score 10 | from sklearn.linear_model import LogisticRegression 11 | from sklearn.tree import DecisionTreeClassifier 12 | from sklearn.svm import SVC 13 | 14 | # Set random seed for reproducibility 15 | np.random.seed(1000) 16 | 17 | 18 | if __name__ == '__main__': 19 | # Load the dataset 20 | X, Y = load_wine(return_X_y=True) 21 | 22 | # Test Logistic regression 23 | lr = LogisticRegression(max_iter=1000, random_state=1000) 24 | print('Logistic Regression CV score: {}'.format(np.mean(cross_val_score(lr, X, Y, cv=10)))) 25 | 26 | # Test Decision Tree 27 | dt = DecisionTreeClassifier(criterion='entropy', random_state=1000) 28 | print('Decistion Tree CV score: {}'.format(np.mean(cross_val_score(dt, X, Y, cv=10)))) 29 | 30 | # Test Polynomial SVM 31 | svm = SVC(kernel='poly', random_state=1000) 32 | print('Polynomial SVM CV score: {}'.format(np.mean(cross_val_score(svm, X, Y, cv=10)))) 33 | 34 | # Test Random Forest 35 | rf = RandomForestClassifier(n_estimators=50, n_jobs=cpu_count(), random_state=1000) 36 | scores = cross_val_score(rf, X, Y, cv=10) 37 | print('Random Forest CV score: {}'.format(np.mean(scores))) 38 | 39 | # Plot CV scores 40 | fig, ax = plt.subplots(figsize=(15, 7)) 41 | 42 | ax.plot(scores) 43 | ax.set_xlabel('Number of Trees (x10)') 44 | ax.set_ylabel('10-fold Cross-Validation Accuracy') 45 | ax.grid() 46 | plt.show() 47 | 48 | # Show feature importances 49 | rf.fit(X, Y) 50 | 51 | wine = load_wine() 52 | features = [wine['feature_names'][x] for x in np.argsort(rf.feature_importances_)][::-1] 53 | 54 | fig, ax = plt.subplots(figsize=(15, 8)) 55 | 56 | ax.bar([i for i in range(13)], np.sort(rf.feature_importances_)[::-1], align='center') 57 | ax.set_ylabel('Feature Importance') 58 | plt.xticks([i for i in range(13)], features, rotation=60) 59 | plt.show() 60 | 61 | # Select the most important features 62 | sfm = SelectFromModel(estimator=rf, prefit=True, threshold=0.02) 63 | X_sfm = sfm.transform(X) 64 | 65 | print('Feature selection shape: {}'.format(X_sfm.shape)) 66 | 67 | -------------------------------------------------------------------------------- /Chapter08/voting_classifiers.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from sklearn.datasets import load_digits 4 | from sklearn.ensemble import VotingClassifier 5 | from sklearn.tree import DecisionTreeClassifier 6 | from sklearn.linear_model import LogisticRegression 7 | from sklearn.model_selection import cross_val_score 8 | 9 | 10 | # Set random seed for reproducibility 11 | np.random.seed(1000) 12 | 13 | 14 | if __name__ == '__main__': 15 | X, Y = load_digits(return_X_y=True) 16 | X /= np.max(X) 17 | 18 | # Test Decision Tree 19 | dt = DecisionTreeClassifier(criterion='entropy', random_state=1000) 20 | print('Decision Tree score: {}'.format(np.mean(cross_val_score(dt, X, Y, cv=10)))) 21 | 22 | # Test Logistic Regression 23 | lr = LogisticRegression(C=2.0, random_state=1000) 24 | print('Logistic Regression score: {}'.format(np.mean(cross_val_score(lr, X, Y, cv=10)))) 25 | 26 | # Create a soft voting classifier 27 | vc = VotingClassifier(estimators=[ 28 | ('LR', LogisticRegression(C=2.0, random_state=1000)), 29 | ('DT', DecisionTreeClassifier(criterion='entropy', random_state=1000))], 30 | voting='soft', weights=(0.9, 0.1)) 31 | 32 | print('Voting classifier score: {}'.format(np.mean(cross_val_score(vc, X, Y, cv=10)))) -------------------------------------------------------------------------------- /Chapter09/batch_normalization.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from keras.datasets import mnist 5 | from keras.models import Sequential 6 | from keras.layers import Dense, Activation, BatchNormalization 7 | from keras.optimizers import Adam 8 | from keras.utils import to_categorical 9 | 10 | 11 | # Set random seed for reproducibility 12 | np.random.seed(1000) 13 | 14 | 15 | if __name__ == '__main__': 16 | # Load and normalize the dataset 17 | (X_train, Y_train), (X_test, Y_test) = mnist.load_data() 18 | 19 | width = height = X_train.shape[1] 20 | 21 | X_train = X_train.reshape((X_train.shape[0], width * height)).astype(np.float32) / 255.0 22 | X_test = X_test.reshape((X_test.shape[0], width * height)).astype(np.float32) / 255.0 23 | 24 | Y_train = to_categorical(Y_train, num_classes=10) 25 | Y_test = to_categorical(Y_test, num_classes=10) 26 | 27 | # Create model 28 | model = Sequential() 29 | 30 | model.add(Dense(2048, input_shape=(width * height,))) 31 | model.add(BatchNormalization()) 32 | model.add(Activation('relu')) 33 | 34 | model.add(Dense(1024)) 35 | model.add(BatchNormalization()) 36 | model.add(Activation('relu')) 37 | 38 | model.add(Dense(1024)) 39 | model.add(BatchNormalization()) 40 | model.add(Activation('relu')) 41 | 42 | model.add(Dense(10)) 43 | model.add(BatchNormalization()) 44 | model.add(Activation('softmax')) 45 | 46 | # Compile model 47 | model.compile(optimizer=Adam(lr=0.001, decay=1e-6), 48 | loss='categorical_crossentropy', 49 | metrics=['accuracy']) 50 | 51 | # Train the model 52 | history_bn = model.fit(X_train, Y_train, 53 | epochs=200, 54 | batch_size=256, 55 | validation_data=(X_test, Y_test)) 56 | 57 | # Show the results 58 | fig, ax = plt.subplots(1, 2, figsize=(18, 6)) 59 | 60 | fig.suptitle('MLP with Batch Normalization. Adam with learning rate equal to 0.001') 61 | 62 | ax[0].plot(history_bn.history['acc'], label='Training accuracy') 63 | ax[0].plot(history_bn.history['val_acc'], label='Validation accuracy') 64 | ax[0].set_xlabel('Epoch') 65 | ax[0].set_ylabel('Accuracy') 66 | ax[0].legend() 67 | ax[0].grid() 68 | 69 | ax[1].plot(history_bn.history['loss'], label='Training loss') 70 | ax[1].plot(history_bn.history['val_loss'], label='Validation loss') 71 | ax[1].set_xlabel('Epoch') 72 | ax[1].set_ylabel('Loss') 73 | ax[1].set_yticks(np.linspace(0.0, 1.0, 10)) 74 | ax[1].legend() 75 | ax[1].grid() 76 | plt.show() -------------------------------------------------------------------------------- /Chapter09/dropout.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from keras.constraints import maxnorm 5 | from keras.datasets import mnist 6 | from keras.models import Sequential 7 | from keras.layers import Dense, Activation, Dropout 8 | from keras.optimizers import Adam, SGD 9 | from keras.utils import to_categorical 10 | 11 | 12 | # Set random seed for reproducibility 13 | np.random.seed(1000) 14 | 15 | 16 | if __name__ == '__main__': 17 | # Load and normalize the dataset 18 | (X_train, Y_train), (X_test, Y_test) = mnist.load_data() 19 | 20 | width = height = X_train.shape[1] 21 | 22 | X_train = X_train.reshape((X_train.shape[0], width * height)).astype(np.float32) / 255.0 23 | X_test = X_test.reshape((X_test.shape[0], width * height)).astype(np.float32) / 255.0 24 | 25 | Y_train = to_categorical(Y_train, num_classes=10) 26 | Y_test = to_categorical(Y_test, num_classes=10) 27 | 28 | # Create the model without Dropout 29 | model = Sequential() 30 | 31 | model.add(Dense(2048, input_shape=(width * height,))) 32 | model.add(Activation('relu')) 33 | 34 | model.add(Dense(1024)) 35 | model.add(Activation('relu')) 36 | 37 | model.add(Dense(1024)) 38 | model.add(Activation('relu')) 39 | 40 | model.add(Dense(10)) 41 | model.add(Activation('softmax')) 42 | 43 | # Compile the model 44 | model.compile(optimizer=Adam(lr=0.0001, decay=1e-6), 45 | loss='categorical_crossentropy', 46 | metrics=['accuracy']) 47 | 48 | # Train the model 49 | history_nd = model.fit(X_train, Y_train, 50 | epochs=200, 51 | batch_size=256, 52 | validation_data=(X_test, Y_test)) 53 | 54 | # Show the results 55 | fig, ax = plt.subplots(1, 2, figsize=(18, 6)) 56 | 57 | fig.suptitle('MLP without Dropout. Adam with learning rate equal to 0.0001') 58 | 59 | ax[0].plot(history_nd.history['acc'], label='Training accuracy') 60 | ax[0].plot(history_nd.history['val_acc'], label='Validation accuracy') 61 | ax[0].set_xlabel('Epoch') 62 | ax[0].set_ylabel('Accuracy') 63 | ax[0].legend() 64 | ax[0].grid() 65 | 66 | ax[1].plot(history_nd.history['loss'], label='Training loss') 67 | ax[1].plot(history_nd.history['val_loss'], label='Validation loss') 68 | ax[1].set_xlabel('Epoch') 69 | ax[1].set_ylabel('Loss') 70 | ax[1].set_yticks(np.linspace(0.0, 1.0, 10)) 71 | ax[1].legend() 72 | ax[1].grid() 73 | plt.show() 74 | 75 | # Create model with Dropout 76 | model = Sequential() 77 | 78 | model.add(Dropout(0.25, input_shape=(width * height,), seed=1000)) 79 | 80 | model.add(Dense(2048, kernel_initializer='uniform', kernel_constraint=maxnorm(2.0))) 81 | model.add(Activation('relu')) 82 | model.add(Dropout(0.5, seed=1000)) 83 | 84 | model.add(Dense(1024, kernel_initializer='uniform', kernel_constraint=maxnorm(2.0))) 85 | model.add(Activation('relu')) 86 | model.add(Dropout(0.5, seed=1000)) 87 | 88 | model.add(Dense(1024, kernel_initializer='uniform', kernel_constraint=maxnorm(2.0))) 89 | model.add(Activation('relu')) 90 | model.add(Dropout(0.5, seed=1000)) 91 | 92 | model.add(Dense(10)) 93 | model.add(Activation('softmax')) 94 | 95 | # Compile the model 96 | model.compile(optimizer=SGD(lr=0.1, momentum=0.9), 97 | loss='categorical_crossentropy', 98 | metrics=['accuracy']) 99 | 100 | # Train the model 101 | history = model.fit(X_train, Y_train, 102 | epochs=200, 103 | batch_size=256, 104 | validation_data=(X_test, Y_test)) 105 | 106 | # Show the results 107 | fig, ax = plt.subplots(1, 2, figsize=(18, 6)) 108 | 109 | fig.suptitle('MLP with Dropout. SGD with learning rate equal to 0.1 and Momentum equal to 0.9') 110 | 111 | ax[0].plot(history.history['acc'], label='Training accuracy') 112 | ax[0].plot(history.history['val_acc'], label='Validation accuracy') 113 | ax[0].set_xlabel('Epoch') 114 | ax[0].set_ylabel('Accuracy') 115 | ax[0].legend() 116 | ax[0].grid() 117 | 118 | ax[1].plot(history.history['loss'], label='Training loss') 119 | ax[1].plot(history.history['val_loss'], label='Validation loss') 120 | ax[1].set_xlabel('Epoch') 121 | ax[1].set_ylabel('Loss') 122 | ax[1].set_yticks(np.linspace(0.0, 1.0, 10)) 123 | ax[1].legend() 124 | ax[1].grid() 125 | plt.show() 126 | 127 | -------------------------------------------------------------------------------- /Chapter09/mlp.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | # To install Keras: pip install -U keras 5 | # Further information: https://keras.io 6 | from keras.models import Sequential 7 | from keras.layers import Dense, Activation 8 | from keras.utils import to_categorical 9 | 10 | from sklearn.model_selection import train_test_split 11 | from sklearn.preprocessing import StandardScaler 12 | from sklearn.utils import shuffle 13 | 14 | 15 | # Set random seed for reproducibility 16 | np.random.seed(1000) 17 | 18 | 19 | nb_samples = 1000 20 | nsb = int(nb_samples / 4) 21 | 22 | 23 | if __name__ == '__main__': 24 | # Create dataset 25 | X = np.zeros((nb_samples, 2)) 26 | Y = np.zeros((nb_samples,)) 27 | 28 | X[0:nsb, :] = np.random.multivariate_normal([1.0, -1.0], np.diag([0.1, 0.1]), size=nsb) 29 | Y[0:nsb] = 0.0 30 | 31 | X[nsb:(2 * nsb), :] = np.random.multivariate_normal([1.0, 1.0], np.diag([0.1, 0.1]), size=nsb) 32 | Y[nsb:(2 * nsb)] = 1.0 33 | 34 | X[(2 * nsb):(3 * nsb), :] = np.random.multivariate_normal([-1.0, 1.0], np.diag([0.1, 0.1]), size=nsb) 35 | Y[(2 * nsb):(3 * nsb)] = 0.0 36 | 37 | X[(3 * nsb):, :] = np.random.multivariate_normal([-1.0, -1.0], np.diag([0.1, 0.1]), size=nsb) 38 | Y[(3 * nsb):] = 1.0 39 | 40 | ss = StandardScaler() 41 | X = ss.fit_transform(X) 42 | 43 | X, Y = shuffle(X, Y, random_state=1000) 44 | 45 | # Create an MLP 46 | model = Sequential() 47 | 48 | model.add(Dense(4, input_dim=2)) 49 | model.add(Activation('tanh')) 50 | 51 | model.add(Dense(2)) 52 | model.add(Activation('softmax')) 53 | 54 | # Compile the model 55 | model.compile(optimizer='adam', 56 | loss='categorical_crossentropy', 57 | metrics=['accuracy']) 58 | 59 | # Create train and test sets 60 | X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=1000) 61 | 62 | # Train the model 63 | model.fit(X_train, 64 | to_categorical(Y_train, num_classes=2), 65 | epochs=100, 66 | batch_size=32, 67 | validation_data=(X_test, to_categorical(Y_test, num_classes=2))) 68 | 69 | # Plot the classification result 70 | Y_pred = model.predict(X) 71 | Y_pred_mlp = np.argmax(Y_pred, axis=1) 72 | 73 | fig, ax = plt.subplots(figsize=(8, 8)) 74 | 75 | ax.scatter(X[Y_pred_mlp == 0, 0], X[Y_pred_mlp == 0, 1]) 76 | ax.scatter(X[Y_pred_mlp == 1, 0], X[Y_pred_mlp == 1, 1]) 77 | ax.set_xlabel(r'$x_0$') 78 | ax.set_ylabel(r'$x_1$') 79 | ax.grid() 80 | plt.show() 81 | 82 | -------------------------------------------------------------------------------- /Chapter09/perceptron.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from multiprocessing import cpu_count 5 | 6 | from sklearn.linear_model import Perceptron 7 | from sklearn.model_selection import cross_val_score 8 | from sklearn.preprocessing import StandardScaler 9 | from sklearn.utils import shuffle 10 | 11 | 12 | # Set random seed for reproducibility 13 | np.random.seed(1000) 14 | 15 | 16 | nb_samples = 1000 17 | nsb = int(nb_samples / 4) 18 | 19 | 20 | if __name__ == '__main__': 21 | # Create dataset 22 | X = np.zeros((nb_samples, 2)) 23 | Y = np.zeros((nb_samples,)) 24 | 25 | X[0:nsb, :] = np.random.multivariate_normal([1.0, -1.0], np.diag([0.1, 0.1]), size=nsb) 26 | Y[0:nsb] = 0.0 27 | 28 | X[nsb:(2 * nsb), :] = np.random.multivariate_normal([1.0, 1.0], np.diag([0.1, 0.1]), size=nsb) 29 | Y[nsb:(2 * nsb)] = 1.0 30 | 31 | X[(2 * nsb):(3 * nsb), :] = np.random.multivariate_normal([-1.0, 1.0], np.diag([0.1, 0.1]), size=nsb) 32 | Y[(2 * nsb):(3 * nsb)] = 0.0 33 | 34 | X[(3 * nsb):, :] = np.random.multivariate_normal([-1.0, -1.0], np.diag([0.1, 0.1]), size=nsb) 35 | Y[(3 * nsb):] = 1.0 36 | 37 | ss = StandardScaler() 38 | X = ss.fit_transform(X) 39 | 40 | X, Y = shuffle(X, Y, random_state=1000) 41 | 42 | # Show the dataset 43 | fig, ax = plt.subplots(figsize=(8, 8)) 44 | 45 | ax.scatter(X[Y == 0, 0], X[Y == 0, 1]) 46 | ax.scatter(X[Y == 1, 0], X[Y == 1, 1]) 47 | ax.set_xlabel(r'$x_0$') 48 | ax.set_ylabel(r'$x_1$') 49 | ax.grid() 50 | plt.show() 51 | 52 | # Perform a classification based on a Perceptron 53 | # Starting from Scikit-Learn 0.19 it's helpful to include a max_iter or tol parameter to avoid a warning 54 | pc = Perceptron(penalty='l2', alpha=0.1, n_jobs=cpu_count(), random_state=1000) 55 | print('Perceptron CV score: {}'.format(np.mean(cross_val_score(pc, X, Y, cv=10)))) 56 | 57 | # Show a classification result 58 | pc.fit(X, Y) 59 | Y_pred_perceptron = pc.predict(X) 60 | 61 | fig, ax = plt.subplots(figsize=(8, 8)) 62 | 63 | ax.scatter(X[Y_pred_perceptron == 0, 0], X[Y_pred_perceptron == 0, 1]) 64 | ax.scatter(X[Y_pred_perceptron == 1, 0], X[Y_pred_perceptron == 1, 1]) 65 | ax.set_xlabel(r'$x_0$') 66 | ax.set_ylabel(r'$x_1$') 67 | ax.grid() 68 | plt.show() 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /Chapter10/dcn.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from keras.datasets import mnist 5 | from keras.models import Sequential 6 | from keras.layers import Dense, Activation, Dropout, Conv2D, AveragePooling2D, Flatten 7 | from keras.optimizers import Adam 8 | from keras.utils import to_categorical 9 | 10 | 11 | # Set random seed for reproducibility 12 | np.random.seed(1000) 13 | 14 | 15 | if __name__ == '__main__': 16 | # Load the dataset 17 | (X_train, Y_train), (X_test, Y_test) = mnist.load_data() 18 | 19 | width = height = X_train.shape[1] 20 | 21 | X_train = X_train.reshape((X_train.shape[0], width, height, 1)).astype(np.float32) / 255.0 22 | X_test = X_test.reshape((X_test.shape[0], width, height, 1)).astype(np.float32) / 255.0 23 | 24 | Y_train = to_categorical(Y_train, num_classes=10) 25 | Y_test = to_categorical(Y_test, num_classes=10) 26 | 27 | # Create the model 28 | model = Sequential() 29 | 30 | model.add(Dropout(0.25, input_shape=(width, height, 1), seed=1000)) 31 | 32 | model.add(Conv2D(16, kernel_size=(3, 3), padding='same')) 33 | model.add(Activation('relu')) 34 | model.add(Dropout(0.5, seed=1000)) 35 | 36 | model.add(Conv2D(32, kernel_size=(3, 3), padding='same')) 37 | model.add(Activation('relu')) 38 | model.add(Dropout(0.5, seed=1000)) 39 | 40 | model.add(AveragePooling2D(pool_size=(2, 2), padding='same')) 41 | 42 | model.add(Conv2D(64, kernel_size=(3, 3), padding='same')) 43 | model.add(Activation('relu')) 44 | 45 | model.add(AveragePooling2D(pool_size=(2, 2), padding='same')) 46 | 47 | model.add(Conv2D(64, kernel_size=(3, 3), padding='same')) 48 | model.add(Activation('relu')) 49 | model.add(Dropout(0.5, seed=1000)) 50 | 51 | model.add(AveragePooling2D(pool_size=(2, 2), padding='same')) 52 | 53 | model.add(Flatten()) 54 | 55 | model.add(Dense(1024)) 56 | model.add(Activation('relu')) 57 | model.add(Dropout(0.5, seed=1000)) 58 | 59 | model.add(Dense(10)) 60 | model.add(Activation('softmax')) 61 | 62 | # Compile the model 63 | model.compile(optimizer=Adam(lr=0.001, decay=1e-5), 64 | loss='categorical_crossentropy', 65 | metrics=['accuracy']) 66 | 67 | history = model.fit(X_train, Y_train, 68 | epochs=200, 69 | batch_size=256, 70 | validation_data=(X_test, Y_test)) 71 | 72 | # Show the results 73 | fig, ax = plt.subplots(1, 2, figsize=(18, 6)) 74 | 75 | ax[0].plot(history.history['acc'], label='Training accuracy') 76 | ax[0].plot(history.history['val_acc'], label='Validation accuracy') 77 | ax[0].set_xlabel('Epoch') 78 | ax[0].set_ylabel('Accuracy') 79 | ax[0].legend() 80 | ax[0].grid() 81 | 82 | ax[1].plot(history.history['loss'], label='Training loss') 83 | ax[1].plot(history.history['val_loss'], label='Validation loss') 84 | ax[1].set_xlabel('Epoch') 85 | ax[1].set_ylabel('Loss') 86 | ax[1].set_yticks(np.linspace(0.0, 1.0, 10)) 87 | ax[1].legend() 88 | ax[1].grid() 89 | plt.show() -------------------------------------------------------------------------------- /Chapter10/dcn_da.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from keras.callbacks import ReduceLROnPlateau 5 | from keras.datasets import fashion_mnist 6 | from keras.models import Sequential 7 | from keras.layers import Activation, Dense, Flatten, LeakyReLU, Conv2D, MaxPooling2D, BatchNormalization 8 | from keras.optimizers import Adam 9 | from keras.preprocessing.image import ImageDataGenerator 10 | from keras.utils import to_categorical 11 | 12 | 13 | # Set random seed for reproducibility 14 | np.random.seed(1000) 15 | 16 | 17 | nb_classes = 10 18 | train_batch_size = 256 19 | test_batch_size = 100 20 | nb_epochs = 100 21 | steps_per_epoch = 1500 22 | 23 | 24 | if __name__ == '__main__': 25 | # Load the dataset 26 | (X_train, Y_train), (X_test, Y_test) = fashion_mnist.load_data() 27 | 28 | # Create the augmented data generators 29 | train_idg = ImageDataGenerator(rescale=1.0 / 255.0, 30 | samplewise_center=True, 31 | samplewise_std_normalization=True, 32 | horizontal_flip=True, 33 | rotation_range=10.0, 34 | shear_range=np.pi / 12.0, 35 | zoom_range=0.25) 36 | 37 | train_dg = train_idg.flow(x=np.expand_dims(X_train, axis=3), 38 | y=to_categorical(Y_train, num_classes=nb_classes), 39 | batch_size=train_batch_size, 40 | shuffle=True, 41 | seed=1000) 42 | 43 | test_idg = ImageDataGenerator(rescale=1.0 / 255.0, 44 | samplewise_center=True, 45 | samplewise_std_normalization=True) 46 | 47 | test_dg = train_idg.flow(x=np.expand_dims(X_test, axis=3), 48 | y=to_categorical(Y_test, num_classes=nb_classes), 49 | shuffle=False, 50 | batch_size=test_batch_size, 51 | seed=1000) 52 | 53 | # Create the model 54 | model = Sequential() 55 | 56 | model.add(Conv2D(filters=32, 57 | kernel_size=(3, 3), 58 | padding='same', 59 | input_shape=(X_train.shape[1], X_train.shape[2], 1))) 60 | 61 | model.add(BatchNormalization()) 62 | model.add(LeakyReLU(alpha=0.1)) 63 | 64 | model.add(Conv2D(filters=64, 65 | kernel_size=(3, 3), 66 | padding='same')) 67 | 68 | model.add(BatchNormalization()) 69 | model.add(LeakyReLU(alpha=0.1)) 70 | 71 | model.add(MaxPooling2D(pool_size=(2, 2))) 72 | 73 | model.add(Conv2D(filters=64, 74 | kernel_size=(3, 3), 75 | padding='same')) 76 | 77 | model.add(BatchNormalization()) 78 | model.add(LeakyReLU(alpha=0.1)) 79 | 80 | model.add(Conv2D(filters=128, 81 | kernel_size=(3, 3), 82 | padding='same')) 83 | 84 | model.add(BatchNormalization()) 85 | model.add(LeakyReLU(alpha=0.1)) 86 | 87 | model.add(Conv2D(filters=128, 88 | kernel_size=(3, 3), 89 | padding='same')) 90 | 91 | model.add(BatchNormalization()) 92 | model.add(LeakyReLU(alpha=0.1)) 93 | 94 | model.add(MaxPooling2D(pool_size=(2, 2))) 95 | 96 | model.add(Flatten()) 97 | 98 | model.add(Dense(units=1024)) 99 | model.add(BatchNormalization()) 100 | model.add(LeakyReLU(alpha=0.1)) 101 | 102 | model.add(Dense(units=1024)) 103 | model.add(BatchNormalization()) 104 | model.add(LeakyReLU(alpha=0.1)) 105 | 106 | model.add(Dense(units=nb_classes)) 107 | model.add(Activation('softmax')) 108 | 109 | # Compile the model 110 | model.compile(loss='categorical_crossentropy', 111 | optimizer=Adam(lr=0.0001, decay=1e-5), 112 | metrics=['accuracy']) 113 | 114 | # Train the model 115 | history = model.fit_generator(generator=train_dg, 116 | epochs=nb_epochs, 117 | steps_per_epoch=steps_per_epoch, 118 | validation_data=test_dg, 119 | validation_steps=int(X_test.shape[0] / test_batch_size), 120 | callbacks=[ 121 | ReduceLROnPlateau(factor=0.1, patience=1, cooldown=1, min_lr=1e-6) 122 | ]) 123 | 124 | # Show the results 125 | fig, ax = plt.subplots(1, 2, figsize=(18, 6)) 126 | 127 | ax[0].plot(history.history['acc'], label='Training accuracy') 128 | ax[0].plot(history.history['val_acc'], label='Validation accuracy') 129 | ax[0].set_xlabel('Epoch') 130 | ax[0].set_ylabel('Accuracy') 131 | ax[0].legend() 132 | ax[0].grid() 133 | 134 | ax[1].plot(history.history['loss'], label='Training loss') 135 | ax[1].plot(history.history['val_loss'], label='Validation loss') 136 | ax[1].set_xlabel('Epoch') 137 | ax[1].set_ylabel('Loss') 138 | ax[1].set_yticks(np.linspace(0.0, 1.0, 10)) 139 | ax[1].legend() 140 | ax[1].grid() 141 | plt.show() 142 | 143 | -------------------------------------------------------------------------------- /Chapter10/recurrent.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | 4 | from keras.models import Sequential 5 | from keras.layers import LSTM, Dense, Activation 6 | from keras.optimizers import Adam 7 | 8 | from sklearn.preprocessing import MinMaxScaler 9 | 10 | 11 | # Set random seed for reproducibility 12 | np.random.seed(1000) 13 | 14 | # Download the dataset from: https://datamarket.com/data/set/22ti/zuerich-monthly-sunspot-numbers-1749-1983#!ds=22ti&display=line 15 | dataset_filename = '\dataset.csv' 16 | 17 | n_samples = 2820 18 | data = np.zeros(shape=(n_samples, ), dtype=np.float32) 19 | 20 | sequence_length = 15 21 | 22 | 23 | if __name__ == '__main__': 24 | # Load the dataset 25 | with open(dataset_filename, 'r') as f: 26 | lines = f.readlines() 27 | 28 | for i, line in enumerate(lines): 29 | if i == 0: 30 | continue 31 | 32 | if i == n_samples + 1: 33 | break 34 | 35 | _, value = line.split(',') 36 | data[i - 1] = float(value) 37 | 38 | # Scale the dataset between -1 and 1 39 | mmscaler = MinMaxScaler((-1.0, 1.0)) 40 | data = mmscaler.fit_transform(data.reshape(-1, 1)) 41 | 42 | # Show the dataset 43 | fig, ax = plt.subplots(figsize=(20, 10)) 44 | 45 | ax.plot(data) 46 | ax.grid() 47 | ax.set_xlabel('Time steps') 48 | ax.set_ylabel('Monthly sunspots numbers') 49 | plt.show() 50 | 51 | # Create the train and test sets (rounding to 2800 samples) 52 | X_ts = np.zeros(shape=(n_samples - sequence_length, sequence_length, 1), dtype=np.float32) 53 | Y_ts = np.zeros(shape=(n_samples - sequence_length, 1), dtype=np.float32) 54 | 55 | for i in range(0, data.shape[0] - sequence_length): 56 | X_ts[i] = data[i:i + sequence_length] 57 | Y_ts[i] = data[i + sequence_length] 58 | 59 | X_ts_train = X_ts[0:2300, :] 60 | Y_ts_train = Y_ts[0:2300] 61 | 62 | X_ts_test = X_ts[2300:2800, :] 63 | Y_ts_test = Y_ts[2300:2800] 64 | 65 | # Create the model 66 | model = Sequential() 67 | 68 | model.add(LSTM(4, stateful=True, batch_input_shape=(20, sequence_length, 1))) 69 | 70 | model.add(Dense(1)) 71 | model.add(Activation('tanh')) 72 | 73 | # Compile the model 74 | model.compile(optimizer=Adam(lr=0.001, decay=0.0001), 75 | loss='mse', 76 | metrics=['mse']) 77 | 78 | # Train the model 79 | model.fit(X_ts_train, Y_ts_train, 80 | batch_size=20, 81 | epochs=100, 82 | shuffle=False, 83 | validation_data=(X_ts_test, Y_ts_test)) 84 | 85 | # Show the result 86 | fig, ax = plt.subplots(figsize=(20, 10)) 87 | 88 | ax.plot(Y_ts_test, label='True values') 89 | ax.plot(model.predict(X_ts_test, batch_size=20), label='Predicted values') 90 | ax.grid() 91 | ax.set_xlabel('Time steps') 92 | ax.set_ylabel('Monthly sunspots numbers') 93 | ax.legend() 94 | plt.show() 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /Chapter11/dca.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import tensorflow as tf 4 | 5 | from keras.datasets import fashion_mnist 6 | 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | 11 | 12 | nb_samples = 1000 13 | nb_epochs = 400 14 | batch_size = 200 15 | code_length = 256 16 | 17 | 18 | if __name__ == '__main__': 19 | # Load the dataset 20 | (X_train, _), (_, _) = fashion_mnist.load_data() 21 | X_train = X_train.astype(np.float32)[0:nb_samples] / 255.0 22 | 23 | width = X_train.shape[1] 24 | height = X_train.shape[2] 25 | 26 | # Create graph 27 | graph = tf.Graph() 28 | 29 | with graph.as_default(): 30 | input_images = tf.placeholder(tf.float32, shape=(None, width, height, 1)) 31 | 32 | r_input_images = tf.image.resize_images(input_images, (32, 32)) 33 | 34 | # Encoder 35 | conv_0 = tf.layers.conv2d(inputs=r_input_images, 36 | filters=32, 37 | kernel_size=(3, 3), 38 | strides=(2, 2), 39 | activation=tf.nn.relu, 40 | padding='same') 41 | 42 | conv_1 = tf.layers.conv2d(inputs=conv_0, 43 | filters=64, 44 | kernel_size=(3, 3), 45 | activation=tf.nn.relu, 46 | padding='same') 47 | 48 | conv_2 = tf.layers.conv2d(inputs=conv_1, 49 | filters=128, 50 | kernel_size=(3, 3), 51 | activation=tf.nn.relu, 52 | padding='same') 53 | 54 | # Code layer 55 | code_input = tf.layers.flatten(inputs=conv_2) 56 | 57 | code_layer = tf.layers.dense(inputs=code_input, 58 | units=code_length, 59 | activation=tf.nn.sigmoid) 60 | 61 | # Decoder 62 | decoder_input = tf.reshape(code_layer, (-1, 16, 16, 1)) 63 | 64 | convt_0 = tf.layers.conv2d_transpose(inputs=decoder_input, 65 | filters=128, 66 | kernel_size=(3, 3), 67 | strides=(2, 2), 68 | activation=tf.nn.relu, 69 | padding='same') 70 | 71 | convt_1 = tf.layers.conv2d_transpose(inputs=convt_0, 72 | filters=64, 73 | kernel_size=(3, 3), 74 | activation=tf.nn.relu, 75 | padding='same') 76 | 77 | convt_2 = tf.layers.conv2d_transpose(inputs=convt_1, 78 | filters=32, 79 | kernel_size=(3, 3), 80 | activation=tf.nn.relu, 81 | padding='same') 82 | 83 | convt_3 = tf.layers.conv2d_transpose(inputs=convt_2, 84 | filters=1, 85 | kernel_size=(3, 3), 86 | activation=tf.sigmoid, 87 | padding='same') 88 | 89 | # Loss 90 | loss = tf.nn.l2_loss(convt_3 - r_input_images) 91 | 92 | # Training step 93 | training_step = tf.train.AdamOptimizer(0.001).minimize(loss) 94 | 95 | # Train the model 96 | session = tf.InteractiveSession(graph=graph) 97 | tf.global_variables_initializer().run() 98 | 99 | for e in range(nb_epochs): 100 | np.random.shuffle(X_train) 101 | 102 | total_loss = 0.0 103 | 104 | for i in range(0, nb_samples - batch_size, batch_size): 105 | X = np.zeros((batch_size, width, height, 1), dtype=np.float32) 106 | X[:, :, :, 0] = X_train[i:i + batch_size, :, :] 107 | 108 | _, n_loss = session.run([training_step, loss], 109 | feed_dict={ 110 | input_images: X 111 | }) 112 | total_loss += n_loss 113 | 114 | print('Epoch {}) Total loss: {}'.format(e + 1, total_loss)) 115 | 116 | codes = session.run([code_layer], 117 | feed_dict={ 118 | input_images: np.expand_dims(X_train, axis=3), 119 | })[0] 120 | 121 | print('Code mean: {}'.format(np.mean(codes))) 122 | 123 | # Show some examples 124 | Xs = np.reshape(X_train[0:batch_size], (batch_size, width, height, 1)) 125 | 126 | Ys = session.run([convt_3], 127 | feed_dict={ 128 | input_images: Xs 129 | }) 130 | 131 | Ys = np.squeeze(Ys[0] * 255.0) 132 | 133 | # Show the results 134 | fig, ax = plt.subplots(2, 10, figsize=(18, 4)) 135 | 136 | for i in range(10): 137 | ax[0, i].imshow(Ys[i], cmap='gray') 138 | ax[0, i].set_xticks([]) 139 | ax[0, i].set_yticks([]) 140 | 141 | ax[1, i].imshow(Ys[i + 10], cmap='gray') 142 | ax[1, i].set_xticks([]) 143 | ax[1, i].set_yticks([]) 144 | 145 | plt.show() 146 | 147 | session.close() 148 | 149 | -------------------------------------------------------------------------------- /Chapter11/denoising_dca.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import tensorflow as tf 4 | 5 | from keras.datasets import fashion_mnist 6 | 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | 11 | 12 | nb_samples = 1000 13 | nb_epochs = 400 14 | batch_size = 200 15 | code_length = 256 16 | 17 | 18 | if __name__ == '__main__': 19 | # Load the dataset 20 | (X_train, _), (_, _) = fashion_mnist.load_data() 21 | X_train = X_train.astype(np.float32)[0:nb_samples] / 255.0 22 | 23 | width = X_train.shape[1] 24 | height = X_train.shape[2] 25 | 26 | graph = tf.Graph() 27 | 28 | with graph.as_default(): 29 | input_noisy_images = tf.placeholder(tf.float32, shape=(None, width, height, 1)) 30 | input_images = tf.placeholder(tf.float32, shape=(None, width, height, 1)) 31 | 32 | # Encoder 33 | conv_0 = tf.layers.conv2d(inputs=input_noisy_images, 34 | filters=32, 35 | kernel_size=(3, 3), 36 | activation=tf.nn.relu, 37 | padding='same') 38 | 39 | conv_1 = tf.layers.conv2d(inputs=conv_0, 40 | filters=64, 41 | kernel_size=(3, 3), 42 | activation=tf.nn.relu, 43 | padding='same') 44 | 45 | conv_2 = tf.layers.conv2d(inputs=conv_1, 46 | filters=128, 47 | kernel_size=(3, 3), 48 | activation=tf.nn.relu, 49 | padding='same') 50 | 51 | # Code layer 52 | code_input = tf.layers.flatten(inputs=conv_2) 53 | 54 | code_layer = tf.layers.dense(inputs=code_input, 55 | units=width * height, 56 | activation=tf.nn.sigmoid) 57 | 58 | # Decoder 59 | decoder_input = tf.reshape(code_layer, (-1, width, height, 1)) 60 | 61 | convt_0 = tf.layers.conv2d_transpose(inputs=decoder_input, 62 | filters=128, 63 | kernel_size=(3, 3), 64 | activation=tf.nn.relu, 65 | padding='same') 66 | 67 | convt_1 = tf.layers.conv2d_transpose(inputs=convt_0, 68 | filters=64, 69 | kernel_size=(3, 3), 70 | activation=tf.nn.relu, 71 | padding='same') 72 | 73 | convt_2 = tf.layers.conv2d_transpose(inputs=convt_1, 74 | filters=32, 75 | kernel_size=(3, 3), 76 | activation=tf.nn.relu, 77 | padding='same') 78 | 79 | convt_3 = tf.layers.conv2d_transpose(inputs=convt_2, 80 | filters=1, 81 | kernel_size=(3, 3), 82 | activation=tf.sigmoid, 83 | padding='same') 84 | 85 | # Loss 86 | loss = tf.nn.l2_loss(convt_3 - input_images) 87 | 88 | # Training step 89 | training_step = tf.train.AdamOptimizer(0.001).minimize(loss) 90 | 91 | # Train the model 92 | session = tf.InteractiveSession(graph=graph) 93 | tf.global_variables_initializer().run() 94 | 95 | for e in range(nb_epochs): 96 | total_loss = 0.0 97 | 98 | for i in range(0, nb_samples - batch_size, batch_size): 99 | X = np.zeros((batch_size, width, height, 1), dtype=np.float32) 100 | X[:, :, :, 0] = X_train[i:i + batch_size, :, :] 101 | Xn = np.clip(X + np.random.normal(0.0, 0.2, size=(batch_size, width, height, 1)), 0.0, 1.0) 102 | 103 | _, n_loss = session.run([training_step, loss], 104 | feed_dict={ 105 | input_images: X, 106 | input_noisy_images: Xn 107 | }) 108 | total_loss += n_loss 109 | 110 | print('Epoch {}) Total loss: {}'.format(e + 1, total_loss)) 111 | 112 | # Show some results 113 | Xs = np.reshape(X_train[0:10], (10, width, height, 1)) 114 | Xn = np.clip(Xs + np.random.normal(0.0, 0.2, size=(10, width, height, 1)), 0.0, 1.0) 115 | 116 | Ys = session.run([convt_3], 117 | feed_dict={ 118 | input_noisy_images: Xn 119 | }) 120 | 121 | Ys = np.squeeze(Ys[0] * 255.0) 122 | 123 | # Show the results 124 | fig, ax = plt.subplots(2, 10, figsize=(18, 4)) 125 | 126 | for i in range(10): 127 | ax[0, i].imshow(np.squeeze(Xn[i]), cmap='gray') 128 | ax[0, i].set_xticks([]) 129 | ax[0, i].set_yticks([]) 130 | 131 | ax[1, i].imshow(Ys[i], cmap='gray') 132 | ax[1, i].set_xticks([]) 133 | ax[1, i].set_yticks([]) 134 | 135 | plt.show() 136 | 137 | session.close() -------------------------------------------------------------------------------- /Chapter11/sparse_dca.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import tensorflow as tf 4 | 5 | from keras.datasets import fashion_mnist 6 | 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | 11 | 12 | nb_samples = 1000 13 | nb_epochs = 400 14 | batch_size = 200 15 | code_length = 256 16 | 17 | 18 | if __name__ == '__main__': 19 | # Load the dataset 20 | (X_train, _), (_, _) = fashion_mnist.load_data() 21 | X_train = X_train.astype(np.float32)[0:nb_samples] / 255.0 22 | 23 | width = X_train.shape[1] 24 | height = X_train.shape[2] 25 | 26 | # Create graph 27 | graph = tf.Graph() 28 | 29 | with graph.as_default(): 30 | input_images = tf.placeholder(tf.float32, shape=(None, width, height, 1)) 31 | 32 | r_input_images = tf.image.resize_images(input_images, (32, 32)) 33 | 34 | # Encoder 35 | conv_0 = tf.layers.conv2d(inputs=r_input_images, 36 | filters=32, 37 | kernel_size=(3, 3), 38 | strides=(2, 2), 39 | activation=tf.nn.relu, 40 | padding='same') 41 | 42 | conv_1 = tf.layers.conv2d(inputs=conv_0, 43 | filters=64, 44 | kernel_size=(3, 3), 45 | activation=tf.nn.relu, 46 | padding='same') 47 | 48 | conv_2 = tf.layers.conv2d(inputs=conv_1, 49 | filters=128, 50 | kernel_size=(3, 3), 51 | activation=tf.nn.relu, 52 | padding='same') 53 | 54 | # Code layer 55 | code_input = tf.layers.flatten(inputs=conv_2) 56 | 57 | code_layer = tf.layers.dense(inputs=code_input, 58 | units=code_length, 59 | activation=tf.nn.sigmoid) 60 | 61 | # Decoder 62 | decoder_input = tf.reshape(code_layer, (-1, 16, 16, 1)) 63 | 64 | convt_0 = tf.layers.conv2d_transpose(inputs=decoder_input, 65 | filters=128, 66 | kernel_size=(3, 3), 67 | strides=(2, 2), 68 | activation=tf.nn.relu, 69 | padding='same') 70 | 71 | convt_1 = tf.layers.conv2d_transpose(inputs=convt_0, 72 | filters=64, 73 | kernel_size=(3, 3), 74 | activation=tf.nn.relu, 75 | padding='same') 76 | 77 | convt_2 = tf.layers.conv2d_transpose(inputs=convt_1, 78 | filters=32, 79 | kernel_size=(3, 3), 80 | activation=tf.nn.relu, 81 | padding='same') 82 | 83 | convt_3 = tf.layers.conv2d_transpose(inputs=convt_2, 84 | filters=1, 85 | kernel_size=(3, 3), 86 | activation=tf.sigmoid, 87 | padding='same') 88 | 89 | # Loss 90 | sparsity_constraint = tf.reduce_sum(0.001 * tf.norm(code_layer, ord=1, axis=1)) 91 | loss = tf.nn.l2_loss(convt_3 - r_input_images) + sparsity_constraint 92 | 93 | # Training step 94 | training_step = tf.train.AdamOptimizer(0.001).minimize(loss) 95 | 96 | # Train the model 97 | session = tf.InteractiveSession(graph=graph) 98 | tf.global_variables_initializer().run() 99 | 100 | for e in range(nb_epochs): 101 | np.random.shuffle(X_train) 102 | 103 | total_loss = 0.0 104 | 105 | for i in range(0, nb_samples - batch_size, batch_size): 106 | X = np.zeros((batch_size, width, height, 1), dtype=np.float32) 107 | X[:, :, :, 0] = X_train[i:i + batch_size, :, :] 108 | 109 | _, n_loss = session.run([training_step, loss], 110 | feed_dict={ 111 | input_images: X 112 | }) 113 | total_loss += n_loss 114 | 115 | print('Epoch {}) Total loss: {}'.format(e + 1, total_loss)) 116 | 117 | codes = session.run([code_layer], 118 | feed_dict={ 119 | input_images: np.expand_dims(X_train, axis=3), 120 | })[0] 121 | 122 | print('Code mean: {}'.format(np.mean(codes))) 123 | 124 | # Show some examples 125 | Xs = np.reshape(X_train[0:batch_size], (batch_size, width, height, 1)) 126 | 127 | Ys = session.run([convt_3], 128 | feed_dict={ 129 | input_images: Xs 130 | }) 131 | 132 | Ys = np.squeeze(Ys[0] * 255.0) 133 | 134 | # Show the results 135 | fig, ax = plt.subplots(2, 10, figsize=(18, 4)) 136 | 137 | for i in range(10): 138 | ax[0, i].imshow(Ys[i], cmap='gray') 139 | ax[0, i].set_xticks([]) 140 | ax[0, i].set_yticks([]) 141 | 142 | ax[1, i].imshow(Ys[i + 10], cmap='gray') 143 | ax[1, i].set_xticks([]) 144 | ax[1, i].set_yticks([]) 145 | 146 | plt.show() 147 | 148 | session.close() -------------------------------------------------------------------------------- /Chapter11/variational_dca.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import tensorflow as tf 4 | 5 | from keras.datasets import fashion_mnist 6 | 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | tf.set_random_seed(1000) 11 | 12 | 13 | nb_samples = 1000 14 | nb_epochs = 400 15 | batch_size = 200 16 | code_length = 256 17 | 18 | 19 | if __name__ == '__main__': 20 | # Load the dataset 21 | (X_train, _), (_, _) = fashion_mnist.load_data() 22 | X_train = X_train.astype(np.float32)[0:nb_samples] / 255.0 23 | 24 | width = X_train.shape[1] 25 | height = X_train.shape[2] 26 | 27 | graph = tf.Graph() 28 | 29 | with graph.as_default(): 30 | input_images = tf.placeholder(tf.float32, shape=(batch_size, width, height, 1)) 31 | 32 | # Encoder 33 | conv_0 = tf.layers.conv2d(inputs=input_images, 34 | filters=32, 35 | kernel_size=(3, 3), 36 | strides=(2, 2), 37 | activation=tf.nn.relu, 38 | padding='same') 39 | 40 | conv_1 = tf.layers.conv2d(inputs=conv_0, 41 | filters=64, 42 | kernel_size=(3, 3), 43 | strides=(2, 2), 44 | activation=tf.nn.relu, 45 | padding='same') 46 | 47 | conv_2 = tf.layers.conv2d(inputs=conv_1, 48 | filters=128, 49 | kernel_size=(3, 3), 50 | activation=tf.nn.relu, 51 | padding='same') 52 | 53 | # Code layer 54 | code_input = tf.layers.flatten(inputs=conv_2) 55 | 56 | code_mean = tf.layers.dense(inputs=code_input, 57 | units=width * height) 58 | 59 | code_log_variance = tf.layers.dense(inputs=code_input, 60 | units=width * height) 61 | 62 | code_std = tf.sqrt(tf.exp(code_log_variance)) 63 | 64 | # Normal samples 65 | normal_samples = tf.random_normal(mean=0.0, stddev=1.0, shape=(batch_size, width * height)) 66 | 67 | # Sampled code 68 | sampled_code = (normal_samples * code_std) + code_mean 69 | 70 | # Decoder 71 | decoder_input = tf.reshape(sampled_code, (-1, 7, 7, 16)) 72 | 73 | convt_0 = tf.layers.conv2d_transpose(inputs=decoder_input, 74 | filters=64, 75 | kernel_size=(3, 3), 76 | strides=(2, 2), 77 | activation=tf.nn.relu, 78 | padding='same') 79 | 80 | convt_1 = tf.layers.conv2d_transpose(inputs=convt_0, 81 | filters=32, 82 | kernel_size=(3, 3), 83 | strides=(2, 2), 84 | activation=tf.nn.relu, 85 | padding='same') 86 | 87 | convt_2 = tf.layers.conv2d_transpose(inputs=convt_1, 88 | filters=1, 89 | kernel_size=(3, 3), 90 | padding='same') 91 | 92 | convt_output = tf.nn.sigmoid(convt_2) 93 | 94 | # Loss 95 | reconstruction = tf.nn.sigmoid_cross_entropy_with_logits(logits=convt_2, labels=input_images) 96 | kl_divergence = 0.5 * tf.reduce_sum( 97 | tf.square(code_mean) + tf.square(code_std) - tf.log(1e-8 + tf.square(code_std)) - 1, axis=1) 98 | 99 | loss = tf.reduce_sum(reconstruction) + kl_divergence 100 | 101 | # Training step 102 | training_step = tf.train.AdamOptimizer(0.001).minimize(loss) 103 | 104 | # Train the model 105 | session = tf.InteractiveSession(graph=graph) 106 | tf.global_variables_initializer().run() 107 | 108 | for e in range(nb_epochs): 109 | np.random.shuffle(X_train) 110 | 111 | total_loss = 0.0 112 | 113 | for i in range(0, nb_samples - batch_size, batch_size): 114 | X = np.zeros((batch_size, width, height, 1), dtype=np.float32) 115 | X[:, :, :, 0] = X_train[i:i + batch_size, :, :] 116 | 117 | _, n_loss = session.run([training_step, loss], 118 | feed_dict={ 119 | input_images: X 120 | }) 121 | total_loss += n_loss 122 | 123 | print('Epoch {}) Total loss: {}'.format(e + 1, total_loss)) 124 | 125 | # Show some results 126 | Xs = np.reshape(X_train[0:batch_size], (batch_size, width, height, 1)) 127 | 128 | Ys = session.run([convt_output], 129 | feed_dict={ 130 | input_images: Xs 131 | }) 132 | 133 | Ys = np.squeeze(Ys[0] * 255.0) 134 | 135 | fig, ax = plt.subplots(2, 10, figsize=(18, 4)) 136 | 137 | for i in range(10): 138 | ax[0, i].imshow(Ys[i], cmap='gray') 139 | ax[0, i].set_xticks([]) 140 | ax[0, i].set_yticks([]) 141 | 142 | ax[1, i].imshow(Ys[i + 10], cmap='gray') 143 | ax[1, i].set_xticks([]) 144 | ax[1, i].set_yticks([]) 145 | 146 | plt.show() 147 | 148 | session.close() 149 | -------------------------------------------------------------------------------- /Chapter12/dcgan.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import tensorflow as tf 4 | 5 | from keras.datasets import fashion_mnist 6 | 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | tf.set_random_seed(1000) 11 | 12 | 13 | nb_samples = 5000 14 | code_length = 100 15 | nb_epochs = 200 16 | batch_size = 128 17 | nb_iterations = int(nb_samples / batch_size) 18 | 19 | 20 | def generator(z, is_training=True): 21 | with tf.variable_scope('generator'): 22 | conv_0 = tf.layers.conv2d_transpose(inputs=z, 23 | filters=1024, 24 | kernel_size=(4, 4), 25 | padding='valid') 26 | 27 | b_conv_0 = tf.layers.batch_normalization(inputs=conv_0, training=is_training) 28 | 29 | conv_1 = tf.layers.conv2d_transpose(inputs=tf.nn.leaky_relu(b_conv_0), 30 | filters=512, 31 | kernel_size=(4, 4), 32 | strides=(2, 2), 33 | padding='same') 34 | 35 | b_conv_1 = tf.layers.batch_normalization(inputs=conv_1, training=is_training) 36 | 37 | conv_2 = tf.layers.conv2d_transpose(inputs=tf.nn.leaky_relu(b_conv_1), 38 | filters=256, 39 | kernel_size=(4, 4), 40 | strides=(2, 2), 41 | padding='same') 42 | 43 | b_conv_2 = tf.layers.batch_normalization(inputs=conv_2, training=is_training) 44 | 45 | conv_3 = tf.layers.conv2d_transpose(inputs=tf.nn.leaky_relu(b_conv_2), 46 | filters=128, 47 | kernel_size=(4, 4), 48 | strides=(2, 2), 49 | padding='same') 50 | 51 | b_conv_3 = tf.layers.batch_normalization(inputs=conv_3, training=is_training) 52 | 53 | conv_4 = tf.layers.conv2d_transpose(inputs=tf.nn.leaky_relu(b_conv_3), 54 | filters=1, 55 | kernel_size=(4, 4), 56 | strides=(2, 2), 57 | padding='same') 58 | 59 | return tf.nn.tanh(conv_4) 60 | 61 | 62 | def discriminator(x, is_training=True, reuse_variables=True): 63 | with tf.variable_scope('discriminator', reuse=reuse_variables): 64 | conv_0 = tf.layers.conv2d(inputs=x, 65 | filters=128, 66 | kernel_size=(4, 4), 67 | strides=(2, 2), 68 | padding='same') 69 | 70 | conv_1 = tf.layers.conv2d(inputs=tf.nn.leaky_relu(conv_0), 71 | filters=256, 72 | kernel_size=(4, 4), 73 | strides=(2, 2), 74 | padding='same') 75 | 76 | b_conv_1 = tf.layers.batch_normalization(inputs=conv_1, training=is_training) 77 | 78 | conv_2 = tf.layers.conv2d(inputs=tf.nn.leaky_relu(b_conv_1), 79 | filters=512, 80 | kernel_size=(4, 4), 81 | strides=(2, 2), 82 | padding='same') 83 | 84 | b_conv_2 = tf.layers.batch_normalization(inputs=conv_2, training=is_training) 85 | 86 | conv_3 = tf.layers.conv2d(inputs=tf.nn.leaky_relu(b_conv_2), 87 | filters=1024, 88 | kernel_size=(4, 4), 89 | strides=(2, 2), 90 | padding='same') 91 | 92 | b_conv_3 = tf.layers.batch_normalization(inputs=conv_3, training=is_training) 93 | 94 | conv_4 = tf.layers.conv2d(inputs=tf.nn.leaky_relu(b_conv_3), 95 | filters=1, 96 | kernel_size=(4, 4), 97 | padding='valid') 98 | 99 | return conv_4 100 | 101 | 102 | if __name__ == '__main__': 103 | # Load the dataset 104 | (X_train, _), (_, _) = fashion_mnist.load_data() 105 | X_train = X_train.astype(np.float32)[0:nb_samples] / 255.0 106 | X_train = (2.0 * X_train) - 1.0 107 | 108 | width = X_train.shape[1] 109 | height = X_train.shape[2] 110 | 111 | # Create the graph 112 | graph = tf.Graph() 113 | 114 | with graph.as_default(): 115 | input_x = tf.placeholder(tf.float32, shape=(None, width, height, 1)) 116 | input_z = tf.placeholder(tf.float32, shape=(None, code_length)) 117 | is_training = tf.placeholder(tf.bool) 118 | 119 | gen = generator(z=tf.reshape(input_z, (-1, 1, 1, code_length)), is_training=is_training) 120 | 121 | r_input_x = tf.image.resize_images(images=input_x, size=(64, 64)) 122 | 123 | discr_1_l = discriminator(x=r_input_x, is_training=is_training, reuse_variables=False) 124 | discr_2_l = discriminator(x=gen, is_training=is_training, reuse_variables=True) 125 | 126 | loss_d_1 = tf.reduce_mean( 127 | tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(discr_1_l), logits=discr_1_l)) 128 | loss_d_2 = tf.reduce_mean( 129 | tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.zeros_like(discr_2_l), logits=discr_2_l)) 130 | loss_d = loss_d_1 + loss_d_2 131 | 132 | loss_g = tf.reduce_mean( 133 | tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(discr_2_l), logits=discr_2_l)) 134 | 135 | variables_g = [variable for variable in tf.trainable_variables() if variable.name.startswith('generator')] 136 | variables_d = [variable for variable in tf.trainable_variables() if variable.name.startswith('discriminator')] 137 | 138 | with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)): 139 | training_step_d = tf.train.AdamOptimizer(0.0002, beta1=0.5).minimize(loss=loss_d, var_list=variables_d) 140 | training_step_g = tf.train.AdamOptimizer(0.0002, beta1=0.5).minimize(loss=loss_g, var_list=variables_g) 141 | 142 | # Train the model 143 | session = tf.InteractiveSession(graph=graph) 144 | tf.global_variables_initializer().run() 145 | 146 | samples_range = np.arange(nb_samples) 147 | 148 | for e in range(nb_epochs * 5): 149 | d_losses = [] 150 | g_losses = [] 151 | 152 | for i in range(nb_iterations): 153 | Xi = np.random.choice(samples_range, size=batch_size) 154 | X = np.expand_dims(X_train[Xi], axis=3) 155 | Z = np.random.uniform(-1.0, 1.0, size=(batch_size, code_length)).astype(np.float32) 156 | 157 | _, d_loss = session.run([training_step_d, loss_d], 158 | feed_dict={ 159 | input_x: X, 160 | input_z: Z, 161 | is_training: True 162 | }) 163 | d_losses.append(d_loss) 164 | 165 | Z = np.random.uniform(-1.0, 1.0, size=(batch_size, code_length)).astype(np.float32) 166 | 167 | _, g_loss = session.run([training_step_g, loss_g], 168 | feed_dict={ 169 | input_x: X, 170 | input_z: Z, 171 | is_training: True 172 | }) 173 | 174 | g_losses.append(g_loss) 175 | 176 | print('Epoch {}) Avg. discriminator loss: {} - Avg. generator loss: {}'.format(e + 1, np.mean(d_losses), 177 | np.mean(g_losses))) 178 | 179 | # Show some results 180 | Z = np.random.uniform(-1.0, 1.0, size=(50, code_length)).astype(np.float32) 181 | 182 | Ys = session.run([gen], 183 | feed_dict={ 184 | input_z: Z, 185 | is_training: False 186 | }) 187 | 188 | Ys = np.squeeze((Ys[0] + 1.0) * 0.5 * 255.0).astype(np.uint8) 189 | 190 | fig, ax = plt.subplots(5, 10, figsize=(15, 8)) 191 | 192 | for i in range(5): 193 | for j in range(10): 194 | ax[i, j].imshow(Ys[(i * 10) + j], cmap='gray') 195 | ax[i, j].set_xticks([]) 196 | ax[i, j].set_yticks([]) 197 | 198 | plt.show() 199 | 200 | session.close() 201 | 202 | -------------------------------------------------------------------------------- /Chapter12/wgan.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import tensorflow as tf 4 | 5 | from keras.datasets import fashion_mnist 6 | 7 | 8 | # Set random seed for reproducibility 9 | np.random.seed(1000) 10 | tf.set_random_seed(1000) 11 | 12 | 13 | nb_samples = 5000 14 | nb_epochs = 100 15 | nb_critic = 5 16 | batch_size = 64 17 | nb_iterations = int(nb_samples / batch_size) 18 | code_length = 100 19 | 20 | 21 | def generator(z, is_training=True): 22 | with tf.variable_scope('generator'): 23 | conv_0 = tf.layers.conv2d_transpose(inputs=z, 24 | filters=1024, 25 | kernel_size=(4, 4), 26 | padding='valid') 27 | 28 | b_conv_0 = tf.layers.batch_normalization(inputs=conv_0, training=is_training) 29 | 30 | conv_1 = tf.layers.conv2d_transpose(inputs=tf.nn.leaky_relu(b_conv_0), 31 | filters=512, 32 | kernel_size=(4, 4), 33 | strides=(2, 2), 34 | padding='same') 35 | 36 | b_conv_1 = tf.layers.batch_normalization(inputs=conv_1, training=is_training) 37 | 38 | conv_2 = tf.layers.conv2d_transpose(inputs=tf.nn.leaky_relu(b_conv_1), 39 | filters=256, 40 | kernel_size=(4, 4), 41 | strides=(2, 2), 42 | padding='same') 43 | 44 | b_conv_2 = tf.layers.batch_normalization(inputs=conv_2, training=is_training) 45 | 46 | conv_3 = tf.layers.conv2d_transpose(inputs=tf.nn.leaky_relu(b_conv_2), 47 | filters=128, 48 | kernel_size=(4, 4), 49 | strides=(2, 2), 50 | padding='same') 51 | 52 | b_conv_3 = tf.layers.batch_normalization(inputs=conv_3, training=is_training) 53 | 54 | conv_4 = tf.layers.conv2d_transpose(inputs=tf.nn.leaky_relu(b_conv_3), 55 | filters=1, 56 | kernel_size=(4, 4), 57 | strides=(2, 2), 58 | padding='same') 59 | 60 | return tf.nn.tanh(conv_4) 61 | 62 | 63 | def critic(x, is_training=True, reuse_variables=True): 64 | with tf.variable_scope('critic', reuse=reuse_variables): 65 | conv_0 = tf.layers.conv2d(inputs=x, 66 | filters=128, 67 | kernel_size=(4, 4), 68 | strides=(2, 2), 69 | padding='same') 70 | 71 | conv_1 = tf.layers.conv2d(inputs=tf.nn.leaky_relu(conv_0), 72 | filters=256, 73 | kernel_size=(4, 4), 74 | strides=(2, 2), 75 | padding='same') 76 | 77 | b_conv_1 = tf.layers.batch_normalization(inputs=conv_1, training=is_training) 78 | 79 | conv_2 = tf.layers.conv2d(inputs=tf.nn.leaky_relu(b_conv_1), 80 | filters=512, 81 | kernel_size=(4, 4), 82 | strides=(2, 2), 83 | padding='same') 84 | 85 | b_conv_2 = tf.layers.batch_normalization(inputs=conv_2, training=is_training) 86 | 87 | conv_3 = tf.layers.conv2d(inputs=tf.nn.leaky_relu(b_conv_2), 88 | filters=1024, 89 | kernel_size=(4, 4), 90 | strides=(2, 2), 91 | padding='same') 92 | 93 | b_conv_3 = tf.layers.batch_normalization(inputs=conv_3, training=is_training) 94 | 95 | conv_4 = tf.layers.conv2d(inputs=tf.nn.leaky_relu(b_conv_3), 96 | filters=1, 97 | kernel_size=(4, 4), 98 | padding='valid') 99 | 100 | return conv_4 101 | 102 | 103 | if __name__ == '__main__': 104 | # Load the dataset 105 | (X_train, _), (_, _) = fashion_mnist.load_data() 106 | X_train = X_train.astype(np.float32)[0:nb_samples] / 255.0 107 | X_train = (2.0 * X_train) - 1.0 108 | 109 | width = X_train.shape[1] 110 | height = X_train.shape[2] 111 | 112 | # Create the graph 113 | graph = tf.Graph() 114 | 115 | with graph.as_default(): 116 | input_x = tf.placeholder(tf.float32, shape=(None, width, height, 1)) 117 | input_z = tf.placeholder(tf.float32, shape=(None, code_length)) 118 | is_training = tf.placeholder(tf.bool) 119 | 120 | gen = generator(z=tf.reshape(input_z, (-1, 1, 1, code_length)), is_training=is_training) 121 | 122 | r_input_x = tf.image.resize_images(images=input_x, size=(64, 64)) 123 | 124 | crit_1_l = critic(x=r_input_x, is_training=is_training, reuse_variables=False) 125 | crit_2_l = critic(x=gen, is_training=is_training, reuse_variables=True) 126 | 127 | loss_c = tf.reduce_mean(crit_2_l - crit_1_l) 128 | loss_g = tf.reduce_mean(-crit_2_l) 129 | 130 | variables_g = [variable for variable in tf.trainable_variables() if variable.name.startswith('generator')] 131 | variables_c = [variable for variable in tf.trainable_variables() if variable.name.startswith('critic')] 132 | 133 | with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)): 134 | optimizer_c = tf.train.AdamOptimizer(0.00005, beta1=0.5, beta2=0.9).minimize(loss=loss_c, 135 | var_list=variables_c) 136 | 137 | with tf.control_dependencies([optimizer_c]): 138 | training_step_c = tf.tuple(tensors=[tf.assign(variable, tf.clip_by_value(variable, -0.01, 0.01)) 139 | for variable in variables_c]) 140 | 141 | training_step_g = tf.train.AdamOptimizer(0.00005, beta1=0.5, beta2=0.9).minimize(loss=loss_g, 142 | var_list=variables_g) 143 | 144 | # Train the model 145 | session = tf.InteractiveSession(graph=graph) 146 | tf.global_variables_initializer().run() 147 | 148 | samples_range = np.arange(nb_samples) 149 | 150 | for e in range(nb_epochs): 151 | c_losses = [] 152 | g_losses = [] 153 | 154 | for i in range(nb_iterations): 155 | for j in range(nb_critic): 156 | Xi = np.random.choice(samples_range, size=batch_size) 157 | X = np.expand_dims(X_train[Xi], axis=3) 158 | Z = np.random.uniform(-1.0, 1.0, size=(batch_size, code_length)).astype(np.float32) 159 | 160 | _, c_loss = session.run([training_step_c, loss_c], 161 | feed_dict={ 162 | input_x: X, 163 | input_z: Z, 164 | is_training: True 165 | }) 166 | c_losses.append(c_loss) 167 | 168 | Z = np.random.uniform(-1.0, 1.0, size=(batch_size, code_length)).astype(np.float32) 169 | 170 | _, g_loss = session.run([training_step_g, loss_g], 171 | feed_dict={ 172 | input_x: np.zeros(shape=(batch_size, width, height, 1)), 173 | input_z: Z, 174 | is_training: True 175 | }) 176 | 177 | g_losses.append(g_loss) 178 | 179 | print('Epoch {}) Avg. critic loss: {} - Avg. generator loss: {}'.format(e + 1, np.mean(c_losses), 180 | np.mean(g_losses))) 181 | 182 | # Show some results 183 | Z = np.random.uniform(-1.0, 1.0, size=(50, code_length)).astype(np.float32) 184 | 185 | Ys = session.run([gen], 186 | feed_dict={ 187 | input_z: Z, 188 | is_training: False 189 | }) 190 | 191 | Ys = np.squeeze((Ys[0] + 1.0) * 0.5 * 255.0).astype(np.uint8) 192 | 193 | fig, ax = plt.subplots(5, 10, figsize=(15, 8)) 194 | 195 | for i in range(5): 196 | for j in range(10): 197 | ax[i, j].imshow(Ys[(i * 10) + j], cmap='gray') 198 | ax[i, j].set_xticks([]) 199 | ax[i, j].set_yticks([]) 200 | 201 | plt.show() 202 | 203 | session.close() -------------------------------------------------------------------------------- /Chapter13/supervised_dbn.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # To install the DBN package: pip install git+git://github.com/albertbup/deep-belief-network.git 4 | # Further information: https://github.com/albertbup/deep-belief-network 5 | from dbn.tensorflow import SupervisedDBNClassification 6 | 7 | from sklearn.datasets import fetch_kddcup99 8 | from sklearn.metrics.classification import accuracy_score 9 | from sklearn.model_selection import train_test_split 10 | from sklearn.preprocessing import LabelEncoder, StandardScaler 11 | 12 | 13 | # Set random seed for reproducibility 14 | np.random.seed(1000) 15 | 16 | 17 | if __name__ == '__main__': 18 | # Load and normalize the dataset 19 | kddcup = fetch_kddcup99(subset='smtp', shuffle=True, random_state=1000) 20 | 21 | ss = StandardScaler() 22 | X = ss.fit_transform(kddcup['data']).astype(np.float32) 23 | 24 | le = LabelEncoder() 25 | Y = le.fit_transform(kddcup['target']).astype(np.float32) 26 | 27 | # Create train and test sets 28 | X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, random_state=1000) 29 | 30 | # Train the model 31 | classifier = SupervisedDBNClassification(hidden_layers_structure=[64, 64], 32 | learning_rate_rbm=0.001, 33 | learning_rate=0.01, 34 | n_epochs_rbm=20, 35 | n_iter_backprop=150, 36 | batch_size=256, 37 | activation_function='relu', 38 | dropout_p=0.25) 39 | 40 | classifier.fit(X_train, Y_train) 41 | 42 | Y_pred = classifier.predict(X_test) 43 | print('Accuracy score: {}'.format(accuracy_score(Y_test, Y_pred))) 44 | 45 | -------------------------------------------------------------------------------- /Chapter13/unsupervised_dbn.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import matplotlib.cm as cm 3 | import numpy as np 4 | 5 | # To install the DBN package: pip install git+git://github.com/albertbup/deep-belief-network.git 6 | # Further information: https://github.com/albertbup/deep-belief-network 7 | from dbn.tensorflow import UnsupervisedDBN 8 | 9 | from keras.datasets import mnist 10 | 11 | from sklearn.manifold import TSNE 12 | from sklearn.utils import shuffle 13 | 14 | 15 | # Set random seed for reproducibility 16 | np.random.seed(1000) 17 | 18 | 19 | nb_samples = 400 20 | 21 | 22 | if __name__ == '__main__': 23 | # Load the dataset 24 | (X_train, Y_train), (_, _) = mnist.load_data() 25 | X_train, Y_train = shuffle(X_train, Y_train, random_state=1000) 26 | 27 | width = X_train.shape[1] 28 | height = X_train.shape[2] 29 | 30 | X = X_train[0:nb_samples].reshape((nb_samples, width * height)).astype(np.float32) / 255.0 31 | Y = Y_train[0:nb_samples] 32 | 33 | # Train the unsupervised DBN 34 | unsupervised_dbn = UnsupervisedDBN(hidden_layers_structure=[512, 256, 64], 35 | learning_rate_rbm=0.05, 36 | n_epochs_rbm=100, 37 | batch_size=64, 38 | activation_function='sigmoid') 39 | 40 | X_dbn = unsupervised_dbn.fit_transform(X) 41 | 42 | # Perform t-SNE 43 | tsne = TSNE(n_components=2, perplexity=20, random_state=1000) 44 | X_tsne = tsne.fit_transform(X_dbn) 45 | 46 | # Show the result 47 | fig, ax = plt.subplots(figsize=(18, 10)) 48 | 49 | colors = [cm.tab10(i) for i in Y] 50 | 51 | for i in range(nb_samples): 52 | ax.scatter(X_tsne[:, 0], X_tsne[:, 1], c=colors, marker='o', s=30) 53 | ax.annotate('%d' % Y[i], xy=(X_tsne[i, 0] + 1, X_tsne[i, 1] + 1)) 54 | 55 | ax.set_xlabel(r'$x_0$') 56 | ax.set_ylabel(r'$x_1$') 57 | ax.grid() 58 | 59 | plt.show() 60 | 61 | -------------------------------------------------------------------------------- /Chapter14/policy_iteration.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import matplotlib.cm as cm 3 | import numpy as np 4 | 5 | 6 | # Set random seed for reproducibility 7 | np.random.seed(1000) 8 | 9 | 10 | width = 15 11 | height = 5 12 | 13 | y_final = width - 1 14 | x_final = height - 1 15 | 16 | y_wells = [0, 1, 3, 5, 5, 7, 9, 11, 12, 14] 17 | x_wells = [3, 1, 2, 0, 4, 1, 3, 2, 4, 1] 18 | 19 | gamma = 0.9 20 | nb_max_epochs = 100000 21 | tolerance = 1e-5 22 | nb_actions = 4 23 | 24 | # Initial tunnel rewards 25 | standard_reward = -0.1 26 | tunnel_rewards = np.ones(shape=(height, width)) * standard_reward 27 | 28 | for x_well, y_well in zip(x_wells, y_wells): 29 | tunnel_rewards[x_well, y_well] = -5.0 30 | 31 | tunnel_rewards[x_final, y_final] = 5.0 32 | 33 | policy = np.random.randint(0, nb_actions, size=(height, width)).astype(np.uint8) 34 | tunnel_values = np.zeros(shape=(height, width)) 35 | 36 | 37 | def show_values(t): 38 | fig, ax = plt.subplots(figsize=(15, 6)) 39 | 40 | ax.matshow(tunnel_values, cmap=cm.Pastel1) 41 | ax.set_xlabel('y') 42 | ax.set_ylabel('x') 43 | ax.set_xticks(np.arange(width)) 44 | ax.set_yticks(np.arange(height)) 45 | ax.set_title('Values (t={})'.format(t)) 46 | 47 | for i in range(height): 48 | for j in range(width): 49 | if i == x_final and j == y_final: 50 | msg = 'E' 51 | elif (i, j) in zip(x_wells, y_wells): 52 | msg = r'$\otimes$' 53 | else: 54 | msg = '{:.1f}'.format(tunnel_values[i, j]) 55 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 56 | 57 | plt.show() 58 | 59 | 60 | def show_policy(t): 61 | fig, ax = plt.subplots(figsize=(15, 6)) 62 | 63 | ax.matshow(np.zeros_like(tunnel_values), cmap=cm.Pastel1) 64 | ax.set_xlabel('y') 65 | ax.set_ylabel('x') 66 | ax.set_xticks(np.arange(width)) 67 | ax.set_yticks(np.arange(height)) 68 | ax.set_title('Policy (t={})'.format(t)) 69 | 70 | for i in range(height): 71 | for j in range(width): 72 | action = policy[i, j] 73 | 74 | if i == x_final and j == y_final: 75 | msg = 'E' 76 | elif (i, j) in zip(x_wells, y_wells): 77 | msg = r'$\otimes$' 78 | else: 79 | if action == 0: 80 | msg = r'$\uparrow$' 81 | elif action == 1: 82 | msg = r'$\rightarrow$' 83 | elif action == 2: 84 | msg = r'$\downarrow$' 85 | else: 86 | msg = r'$\leftarrow$' 87 | 88 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 89 | 90 | plt.show() 91 | 92 | 93 | def is_final(x, y): 94 | if (x, y) in zip(x_wells, y_wells) or (x, y) == (x_final, y_final): 95 | return True 96 | return False 97 | 98 | 99 | def policy_evaluation(): 100 | old_tunnel_values = tunnel_values.copy() 101 | 102 | for i in range(height): 103 | for j in range(width): 104 | action = policy[i, j] 105 | 106 | if action == 0: 107 | if i == 0: 108 | x = 0 109 | else: 110 | x = i - 1 111 | y = j 112 | 113 | elif action == 1: 114 | if j == width - 1: 115 | y = width - 1 116 | else: 117 | y = j + 1 118 | x = i 119 | 120 | elif action == 2: 121 | if i == height - 1: 122 | x = height - 1 123 | else: 124 | x = i + 1 125 | y = j 126 | 127 | else: 128 | if j == 0: 129 | y = 0 130 | else: 131 | y = j - 1 132 | x = i 133 | 134 | reward = tunnel_rewards[x, y] 135 | tunnel_values[i, j] = reward + (gamma * old_tunnel_values[x, y]) 136 | 137 | 138 | def policy_improvement(): 139 | for i in range(height): 140 | for j in range(width): 141 | if is_final(i, j): 142 | continue 143 | 144 | values = np.zeros(shape=(nb_actions,)) 145 | 146 | values[0] = (tunnel_rewards[i - 1, j] + (gamma * tunnel_values[i - 1, j])) if i > 0 else -np.inf 147 | values[1] = (tunnel_rewards[i, j + 1] + (gamma * tunnel_values[i, j + 1])) if j < width - 1 else -np.inf 148 | values[2] = (tunnel_rewards[i + 1, j] + (gamma * tunnel_values[i + 1, j])) if i < height - 1 else -np.inf 149 | values[3] = (tunnel_rewards[i, j - 1] + (gamma * tunnel_values[i, j - 1])) if j > 0 else -np.inf 150 | 151 | policy[i, j] = np.argmax(values).astype(np.uint8) 152 | 153 | 154 | if __name__ == '__main__': 155 | # Show tunnel rewards 156 | fig, ax = plt.subplots(figsize=(15, 6)) 157 | 158 | ax.matshow(tunnel_rewards, cmap=cm.Pastel1) 159 | ax.set_xlabel('y') 160 | ax.set_ylabel('x') 161 | ax.set_xticks(np.arange(width)) 162 | ax.set_yticks(np.arange(height)) 163 | ax.set_title('Rewards') 164 | 165 | for i in range(height): 166 | for j in range(width): 167 | msg = '{:.2f}'.format(tunnel_rewards[i, j]) 168 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 169 | 170 | plt.show() 171 | 172 | # Show initial values 173 | show_values(t=0) 174 | 175 | # Show initial policy 176 | show_policy(t=0) 177 | 178 | # Train the model 179 | e = 0 180 | 181 | while e < nb_max_epochs: 182 | e += 1 183 | old_tunnel_values = tunnel_values.copy() 184 | policy_evaluation() 185 | 186 | if np.mean(np.abs(tunnel_values - old_tunnel_values)) < tolerance: 187 | old_policy = policy.copy() 188 | policy_improvement() 189 | 190 | if np.sum(policy - old_policy) == 0: 191 | break 192 | 193 | # Show final values 194 | show_values(t=e) 195 | 196 | # Show final policy 197 | show_policy(t=e) 198 | 199 | 200 | 201 | -------------------------------------------------------------------------------- /Chapter14/td0.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import matplotlib.cm as cm 3 | import numpy as np 4 | 5 | 6 | # Set random seed for reproducibility 7 | np.random.seed(1000) 8 | 9 | 10 | width = 15 11 | height = 5 12 | 13 | y_final = width - 1 14 | x_final = height - 1 15 | 16 | y_wells = [0, 1, 3, 5, 5, 7, 9, 11, 12, 14] 17 | x_wells = [3, 1, 2, 0, 4, 1, 3, 2, 4, 1] 18 | 19 | gamma = 0.9 20 | nb_actions = 4 21 | max_steps = 1000 22 | alpha = 0.25 23 | n_episodes = 5000 24 | 25 | 26 | # Initial tunnel rewards 27 | standard_reward = -0.1 28 | tunnel_rewards = np.ones(shape=(height, width)) * standard_reward 29 | 30 | for x_well, y_well in zip(x_wells, y_wells): 31 | tunnel_rewards[x_well, y_well] = -5.0 32 | 33 | tunnel_rewards[x_final, y_final] = 5.0 34 | 35 | policy = np.random.randint(0, nb_actions, size=(height, width)).astype(np.uint8) 36 | tunnel_values = np.zeros(shape=(height, width)) 37 | 38 | # Setup allowed starting points 39 | xy_grid = np.meshgrid(np.arange(0, height), np.arange(0, width), sparse=False) 40 | xy_grid = np.array(xy_grid).T.reshape(-1, 2) 41 | 42 | xy_final = list(zip(x_wells, y_wells)) 43 | xy_final.append([x_final, y_final]) 44 | 45 | xy_start = [] 46 | 47 | for x, y in xy_grid: 48 | if (x, y) not in xy_final: 49 | xy_start.append([x, y]) 50 | 51 | xy_start = np.array(xy_start) 52 | 53 | 54 | def starting_point(): 55 | xy = np.squeeze(xy_start[np.random.randint(0, xy_start.shape[0], size=1)]) 56 | return xy[0], xy[1] 57 | 58 | 59 | def show_values(t): 60 | fig, ax = plt.subplots(figsize=(15, 6)) 61 | 62 | ax.matshow(tunnel_values, cmap=cm.Pastel1) 63 | ax.set_xlabel('y') 64 | ax.set_ylabel('x') 65 | ax.set_xticks(np.arange(width)) 66 | ax.set_yticks(np.arange(height)) 67 | ax.set_title('Values (t={})'.format(t)) 68 | 69 | for i in range(height): 70 | for j in range(width): 71 | if i == x_final and j == y_final: 72 | msg = 'E' 73 | elif (i, j) in zip(x_wells, y_wells): 74 | msg = r'$\otimes$' 75 | else: 76 | msg = '{:.1f}'.format(tunnel_values[i, j]) 77 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 78 | 79 | plt.show() 80 | 81 | 82 | def show_policy(t): 83 | fig, ax = plt.subplots(figsize=(15, 6)) 84 | 85 | ax.matshow(np.zeros_like(tunnel_values), cmap=cm.Pastel1) 86 | ax.set_xlabel('y') 87 | ax.set_ylabel('x') 88 | ax.set_xticks(np.arange(width)) 89 | ax.set_yticks(np.arange(height)) 90 | ax.set_title('Policy (t={})'.format(t)) 91 | 92 | for i in range(height): 93 | for j in range(width): 94 | action = policy[i, j] 95 | 96 | if i == x_final and j == y_final: 97 | msg = 'E' 98 | elif (i, j) in zip(x_wells, y_wells): 99 | msg = r'$\otimes$' 100 | else: 101 | if action == 0: 102 | msg = r'$\uparrow$' 103 | elif action == 1: 104 | msg = r'$\rightarrow$' 105 | elif action == 2: 106 | msg = r'$\downarrow$' 107 | else: 108 | msg = r'$\leftarrow$' 109 | 110 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 111 | 112 | plt.show() 113 | 114 | 115 | def is_final(x, y): 116 | if (x, y) in zip(x_wells, y_wells) or (x, y) == (x_final, y_final): 117 | return True 118 | return False 119 | 120 | 121 | def episode(): 122 | (i, j) = starting_point() 123 | x = y = 0 124 | 125 | e = 0 126 | 127 | while e < max_steps: 128 | e += 1 129 | 130 | action = policy[i, j] 131 | 132 | if action == 0: 133 | if i == 0: 134 | x = 0 135 | else: 136 | x = i - 1 137 | y = j 138 | 139 | elif action == 1: 140 | if j == width - 1: 141 | y = width - 1 142 | else: 143 | y = j + 1 144 | x = i 145 | 146 | elif action == 2: 147 | if i == height - 1: 148 | x = height - 1 149 | else: 150 | x = i + 1 151 | y = j 152 | 153 | else: 154 | if j == 0: 155 | y = 0 156 | else: 157 | y = j - 1 158 | x = i 159 | 160 | reward = tunnel_rewards[x, y] 161 | tunnel_values[i, j] += alpha * (reward + (gamma * tunnel_values[x, y]) - tunnel_values[i, j]) 162 | 163 | if is_final(x, y): 164 | break 165 | else: 166 | i = x 167 | j = y 168 | 169 | 170 | def policy_selection(): 171 | for i in range(height): 172 | for j in range(width): 173 | if is_final(i, j): 174 | continue 175 | 176 | values = np.zeros(shape=(nb_actions,)) 177 | 178 | values[0] = (tunnel_rewards[i - 1, j] + (gamma * tunnel_values[i - 1, j])) if i > 0 else -np.inf 179 | values[1] = (tunnel_rewards[i, j + 1] + (gamma * tunnel_values[i, j + 1])) if j < width - 1 else -np.inf 180 | values[2] = (tunnel_rewards[i + 1, j] + (gamma * tunnel_values[i + 1, j])) if i < height - 1 else -np.inf 181 | values[3] = (tunnel_rewards[i, j - 1] + (gamma * tunnel_values[i, j - 1])) if j > 0 else -np.inf 182 | 183 | policy[i, j] = np.argmax(values).astype(np.uint8) 184 | 185 | 186 | if __name__ == '__main__': 187 | # Show tunnel rewards 188 | fig, ax = plt.subplots(figsize=(15, 6)) 189 | 190 | ax.matshow(tunnel_rewards, cmap=cm.Pastel1) 191 | ax.set_xlabel('y') 192 | ax.set_ylabel('x') 193 | ax.set_xticks(np.arange(width)) 194 | ax.set_yticks(np.arange(height)) 195 | ax.set_title('Rewards') 196 | 197 | for i in range(height): 198 | for j in range(width): 199 | msg = '{:.2f}'.format(tunnel_rewards[i, j]) 200 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 201 | 202 | plt.show() 203 | 204 | # Show initial values 205 | show_values(t=0) 206 | 207 | # Show initial policy 208 | show_policy(t=0) 209 | 210 | # Train the model 211 | for _ in range(n_episodes): 212 | episode() 213 | policy_selection() 214 | 215 | # Show final values 216 | show_values(t=5000) 217 | 218 | # Show final policy 219 | show_policy(t=5000) 220 | 221 | -------------------------------------------------------------------------------- /Chapter14/value_iteration.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import matplotlib.cm as cm 3 | import numpy as np 4 | 5 | 6 | # Set random seed for reproducibility 7 | np.random.seed(1000) 8 | 9 | 10 | width = 15 11 | height = 5 12 | 13 | y_final = width - 1 14 | x_final = height - 1 15 | 16 | y_wells = [0, 1, 3, 5, 5, 7, 9, 11, 12, 14] 17 | x_wells = [3, 1, 2, 0, 4, 1, 3, 2, 4, 1] 18 | 19 | gamma = 0.9 20 | nb_max_epochs = 100000 21 | tolerance = 1e-5 22 | nb_actions = 4 23 | 24 | # Initial tunnel rewards 25 | standard_reward = -0.1 26 | tunnel_rewards = np.ones(shape=(height, width)) * standard_reward 27 | 28 | for x_well, y_well in zip(x_wells, y_wells): 29 | tunnel_rewards[x_well, y_well] = -5.0 30 | 31 | tunnel_rewards[x_final, y_final] = 5.0 32 | 33 | policy = np.random.randint(0, nb_actions, size=(height, width)).astype(np.uint8) 34 | tunnel_values = np.zeros(shape=(height, width)) 35 | 36 | 37 | def show_values(t): 38 | fig, ax = plt.subplots(figsize=(15, 6)) 39 | 40 | ax.matshow(tunnel_values, cmap=cm.Pastel1) 41 | ax.set_xlabel('y') 42 | ax.set_ylabel('x') 43 | ax.set_xticks(np.arange(width)) 44 | ax.set_yticks(np.arange(height)) 45 | ax.set_title('Values (t={})'.format(t)) 46 | 47 | for i in range(height): 48 | for j in range(width): 49 | if i == x_final and j == y_final: 50 | msg = 'E' 51 | elif (i, j) in zip(x_wells, y_wells): 52 | msg = r'$\otimes$' 53 | else: 54 | msg = '{:.1f}'.format(tunnel_values[i, j]) 55 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 56 | 57 | plt.show() 58 | 59 | 60 | def show_policy(t): 61 | fig, ax = plt.subplots(figsize=(15, 6)) 62 | 63 | ax.matshow(np.zeros_like(tunnel_values), cmap=cm.Pastel1) 64 | ax.set_xlabel('y') 65 | ax.set_ylabel('x') 66 | ax.set_xticks(np.arange(width)) 67 | ax.set_yticks(np.arange(height)) 68 | ax.set_title('Policy (t={})'.format(t)) 69 | 70 | for i in range(height): 71 | for j in range(width): 72 | action = policy[i, j] 73 | 74 | if i == x_final and j == y_final: 75 | msg = 'E' 76 | elif (i, j) in zip(x_wells, y_wells): 77 | msg = r'$\otimes$' 78 | else: 79 | if action == 0: 80 | msg = r'$\uparrow$' 81 | elif action == 1: 82 | msg = r'$\rightarrow$' 83 | elif action == 2: 84 | msg = r'$\downarrow$' 85 | else: 86 | msg = r'$\leftarrow$' 87 | 88 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 89 | 90 | plt.show() 91 | 92 | 93 | def is_final(x, y): 94 | if (x, y) in zip(x_wells, y_wells) or (x, y) == (x_final, y_final): 95 | return True 96 | return False 97 | 98 | 99 | def value_evaluation(): 100 | old_tunnel_values = tunnel_values.copy() 101 | 102 | for i in range(height): 103 | for j in range(width): 104 | rewards = np.zeros(shape=(nb_actions,)) 105 | old_values = np.zeros(shape=(nb_actions,)) 106 | 107 | for k in range(nb_actions): 108 | if k == 0: 109 | if i == 0: 110 | x = 0 111 | else: 112 | x = i - 1 113 | y = j 114 | 115 | elif k == 1: 116 | if j == width - 1: 117 | y = width - 1 118 | else: 119 | y = j + 1 120 | x = i 121 | 122 | elif k == 2: 123 | if i == height - 1: 124 | x = height - 1 125 | else: 126 | x = i + 1 127 | y = j 128 | 129 | else: 130 | if j == 0: 131 | y = 0 132 | else: 133 | y = j - 1 134 | x = i 135 | 136 | rewards[k] = tunnel_rewards[x, y] 137 | old_values[k] = old_tunnel_values[x, y] 138 | 139 | new_values = np.zeros(shape=(nb_actions,)) 140 | 141 | for k in range(nb_actions): 142 | new_values[k] = rewards[k] + (gamma * old_values[k]) 143 | 144 | tunnel_values[i, j] = np.max(new_values) 145 | 146 | 147 | def policy_selection(): 148 | policy = np.zeros(shape=(height, width)).astype(np.uint8) 149 | 150 | for i in range(height): 151 | for j in range(width): 152 | if is_final(i, j): 153 | continue 154 | 155 | values = np.zeros(shape=(nb_actions,)) 156 | 157 | values[0] = (tunnel_rewards[i - 1, j] + (gamma * tunnel_values[i - 1, j])) if i > 0 else -np.inf 158 | values[1] = (tunnel_rewards[i, j + 1] + (gamma * tunnel_values[i, j + 1])) if j < width - 1 else -np.inf 159 | values[2] = (tunnel_rewards[i + 1, j] + (gamma * tunnel_values[i + 1, j])) if i < height - 1 else -np.inf 160 | values[3] = (tunnel_rewards[i, j - 1] + (gamma * tunnel_values[i, j - 1])) if j > 0 else -np.inf 161 | 162 | policy[i, j] = np.argmax(values).astype(np.uint8) 163 | 164 | return policy 165 | 166 | 167 | if __name__ == '__main__': 168 | # Show tunnel rewards 169 | fig, ax = plt.subplots(figsize=(15, 6)) 170 | 171 | ax.matshow(tunnel_rewards, cmap=cm.Pastel1) 172 | ax.set_xlabel('y') 173 | ax.set_ylabel('x') 174 | ax.set_xticks(np.arange(width)) 175 | ax.set_yticks(np.arange(height)) 176 | ax.set_title('Rewards') 177 | 178 | for i in range(height): 179 | for j in range(width): 180 | msg = '{:.2f}'.format(tunnel_rewards[i, j]) 181 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 182 | 183 | plt.show() 184 | 185 | # Show initial values 186 | show_values(t=0) 187 | 188 | # Show initial policy 189 | show_policy(t=0) 190 | 191 | # Train the model 192 | e = 0 193 | 194 | policy = None 195 | 196 | while e < nb_max_epochs: 197 | e += 1 198 | old_tunnel_values = tunnel_values.copy() 199 | value_evaluation() 200 | 201 | if np.mean(np.abs(tunnel_values - old_tunnel_values)) < tolerance: 202 | policy = policy_selection() 203 | break 204 | 205 | # Show final values 206 | show_values(t=e) 207 | 208 | # Show final policy 209 | show_policy(t=e) 210 | 211 | -------------------------------------------------------------------------------- /Chapter15/actor_critic_td0.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import matplotlib.cm as cm 3 | import numpy as np 4 | 5 | 6 | # Set random seed for reproducibility 7 | np.random.seed(1000) 8 | 9 | 10 | width = 15 11 | height = 5 12 | 13 | y_final = width - 1 14 | x_final = height - 1 15 | 16 | y_wells = [0, 1, 3, 5, 5, 7, 9, 11, 12, 14] 17 | x_wells = [3, 1, 2, 0, 4, 1, 3, 2, 4, 1] 18 | 19 | gamma = 0.99 20 | alpha = 0.25 21 | rho = 0.001 22 | nb_actions = 4 23 | max_steps = 1000 24 | n_episodes = 50000 25 | n_exploration = 30000 26 | 27 | 28 | # Initial tunnel rewards 29 | standard_reward = -0.1 30 | tunnel_rewards = np.ones(shape=(height, width)) * standard_reward 31 | 32 | for x_well, y_well in zip(x_wells, y_wells): 33 | tunnel_rewards[x_well, y_well] = -5.0 34 | 35 | tunnel_rewards[x_final, y_final] = 5.0 36 | 37 | # Setup allowed starting points 38 | xy_grid = np.meshgrid(np.arange(0, height), np.arange(0, width), sparse=False) 39 | xy_grid = np.array(xy_grid).T.reshape(-1, 2) 40 | 41 | xy_final = list(zip(x_wells, y_wells)) 42 | xy_final.append([x_final, y_final]) 43 | 44 | xy_start = [] 45 | 46 | for x, y in xy_grid: 47 | if (x, y) not in xy_final: 48 | xy_start.append([x, y]) 49 | 50 | xy_start = np.array(xy_start) 51 | 52 | 53 | def starting_point(): 54 | xy = np.squeeze(xy_start[np.random.randint(0, xy_start.shape[0], size=1)]) 55 | return xy[0], xy[1] 56 | 57 | 58 | tunnel_values = np.zeros(shape=(height, width)) 59 | policy_importances = np.zeros(shape=(height, width, nb_actions)) 60 | 61 | 62 | def show_values(t): 63 | fig, ax = plt.subplots(figsize=(15, 6)) 64 | 65 | ax.matshow(tunnel_values, cmap=cm.Pastel1) 66 | ax.set_xlabel('y') 67 | ax.set_ylabel('x') 68 | ax.set_xticks(np.arange(width)) 69 | ax.set_yticks(np.arange(height)) 70 | ax.set_title('Values (t={})'.format(t)) 71 | 72 | for i in range(height): 73 | for j in range(width): 74 | if i == x_final and j == y_final: 75 | msg = 'E' 76 | elif (i, j) in zip(x_wells, y_wells): 77 | msg = r'$\otimes$' 78 | else: 79 | msg = '{:.1f}'.format(tunnel_values[i, j]) 80 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 81 | 82 | plt.show() 83 | 84 | 85 | def show_policy(t): 86 | policy = get_softmax_policy() 87 | 88 | fig, ax = plt.subplots(figsize=(15, 6)) 89 | 90 | ax.matshow(np.zeros_like(tunnel_values), cmap=cm.Pastel1) 91 | ax.set_xlabel('y') 92 | ax.set_ylabel('x') 93 | ax.set_xticks(np.arange(width)) 94 | ax.set_yticks(np.arange(height)) 95 | ax.set_title('Policy (t={})'.format(t)) 96 | 97 | for i in range(height): 98 | for j in range(width): 99 | action = np.argmax(policy[i, j]) 100 | 101 | if i == x_final and j == y_final: 102 | msg = 'E' 103 | elif (i, j) in zip(x_wells, y_wells): 104 | msg = r'$\otimes$' 105 | else: 106 | if action == 0: 107 | msg = r'$\uparrow$' 108 | elif action == 1: 109 | msg = r'$\rightarrow$' 110 | elif action == 2: 111 | msg = r'$\downarrow$' 112 | else: 113 | msg = r'$\leftarrow$' 114 | 115 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 116 | 117 | plt.show() 118 | 119 | 120 | def get_softmax_policy(): 121 | softmax_policy = policy_importances - np.amax(policy_importances, axis=2, keepdims=True) 122 | return np.exp(softmax_policy) / np.sum(np.exp(softmax_policy), axis=2, keepdims=True) 123 | 124 | 125 | def is_final(x, y): 126 | if (x, y) in zip(x_wells, y_wells) or (x, y) == (x_final, y_final): 127 | return True 128 | return False 129 | 130 | 131 | def select_action(epsilon, i, j): 132 | if np.random.uniform(0.0, 1.0) < epsilon: 133 | return np.random.randint(0, nb_actions) 134 | 135 | policy = get_softmax_policy() 136 | return np.argmax(policy[i, j]) 137 | 138 | 139 | def action_critic_episode(epsilon): 140 | (i, j) = starting_point() 141 | x = y = 0 142 | 143 | e = 0 144 | 145 | while e < max_steps: 146 | e += 1 147 | 148 | action = select_action(epsilon, i, j) 149 | 150 | if action == 0: 151 | if i == 0: 152 | x = 0 153 | else: 154 | x = i - 1 155 | y = j 156 | 157 | elif action == 1: 158 | if j == width - 1: 159 | y = width - 1 160 | else: 161 | y = j + 1 162 | x = i 163 | 164 | elif action == 2: 165 | if i == height - 1: 166 | x = height - 1 167 | else: 168 | x = i + 1 169 | y = j 170 | 171 | else: 172 | if j == 0: 173 | y = 0 174 | else: 175 | y = j - 1 176 | x = i 177 | 178 | reward = tunnel_rewards[x, y] 179 | td_error = reward + (gamma * tunnel_values[x, y]) - tunnel_values[i, j] 180 | 181 | tunnel_values[i, j] += (alpha * td_error) 182 | policy_importances[i, j, action] += (rho * td_error) 183 | 184 | if is_final(x, y): 185 | break 186 | else: 187 | i = x 188 | j = y 189 | 190 | 191 | if __name__ == '__main__': 192 | # Show tunnel rewards 193 | fig, ax = plt.subplots(figsize=(15, 6)) 194 | 195 | ax.matshow(tunnel_rewards, cmap=cm.Pastel1) 196 | ax.set_xlabel('y') 197 | ax.set_ylabel('x') 198 | ax.set_xticks(np.arange(width)) 199 | ax.set_yticks(np.arange(height)) 200 | ax.set_title('Rewards') 201 | 202 | for i in range(height): 203 | for j in range(width): 204 | msg = '{:.2f}'.format(tunnel_rewards[i, j]) 205 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 206 | 207 | plt.show() 208 | 209 | # Show initial values 210 | show_values(t=0) 211 | 212 | # Show initial policy 213 | show_policy(t=0) 214 | 215 | # Train the model 216 | for t in range(n_episodes): 217 | epsilon = 0.0 218 | 219 | if t <= n_exploration: 220 | epsilon = 1.0 - (float(t) / float(n_exploration)) 221 | 222 | action_critic_episode(epsilon) 223 | 224 | # Show final values 225 | show_values(t=n_episodes) 226 | 227 | # Show final policy 228 | show_policy(t=n_episodes) 229 | 230 | 231 | -------------------------------------------------------------------------------- /Chapter15/q_learning.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import matplotlib.cm as cm 3 | import numpy as np 4 | 5 | 6 | # Set random seed for reproducibility 7 | np.random.seed(1000) 8 | 9 | 10 | width = 15 11 | height = 5 12 | 13 | y_final = width - 1 14 | x_final = height - 1 15 | 16 | y_wells = [0, 1, 3, 5, 5, 7, 9, 11, 12, 14] 17 | x_wells = [3, 1, 2, 0, 4, 1, 3, 2, 4, 1] 18 | 19 | 20 | # Initial tunnel rewards 21 | standard_reward = -0.1 22 | tunnel_rewards = np.ones(shape=(height, width)) * standard_reward 23 | 24 | for x_well, y_well in zip(x_wells, y_wells): 25 | tunnel_rewards[x_well, y_well] = -5.0 26 | 27 | tunnel_rewards[x_final, y_final] = 5.0 28 | 29 | 30 | gamma = 0.9 31 | nb_actions = 4 32 | 33 | Q = np.zeros(shape=(height, width, nb_actions)) 34 | 35 | x_start = 0 36 | y_start = 0 37 | 38 | max_steps = 2000 39 | alpha = 0.25 40 | n_episodes = 5000 41 | n_exploration = 3500 42 | 43 | 44 | def show_values(t): 45 | fig, ax = plt.subplots(figsize=(15, 6)) 46 | 47 | ax.matshow(np.max(Q, axis=2), cmap=cm.Pastel1) 48 | ax.set_xlabel('y') 49 | ax.set_ylabel('x') 50 | ax.set_xticks(np.arange(width)) 51 | ax.set_yticks(np.arange(height)) 52 | ax.set_title('Values (t={})'.format(t)) 53 | 54 | for i in range(height): 55 | for j in range(width): 56 | if i == x_final and j == y_final: 57 | msg = 'E' 58 | elif (i, j) in zip(x_wells, y_wells): 59 | msg = r'$\otimes$' 60 | else: 61 | msg = '{:.2f}'.format(np.max(Q[i, j])) 62 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 63 | 64 | plt.show() 65 | 66 | 67 | def show_policy(t): 68 | fig, ax = plt.subplots(figsize=(15, 6)) 69 | 70 | ax.matshow(np.zeros_like(Q[:, :, 0]), cmap=cm.Pastel1) 71 | ax.set_xlabel('y') 72 | ax.set_ylabel('x') 73 | ax.set_xticks(np.arange(width)) 74 | ax.set_yticks(np.arange(height)) 75 | ax.set_title('Policy (t={})'.format(t)) 76 | 77 | for i in range(height): 78 | for j in range(width): 79 | action = np.argmax(Q[i, j]) 80 | 81 | if i == x_final and j == y_final: 82 | msg = 'E' 83 | elif (i, j) in zip(x_wells, y_wells): 84 | msg = r'$\otimes$' 85 | else: 86 | if action == 0: 87 | msg = r'$\uparrow$' 88 | elif action == 1: 89 | msg = r'$\rightarrow$' 90 | elif action == 2: 91 | msg = r'$\downarrow$' 92 | else: 93 | msg = r'$\leftarrow$' 94 | 95 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 96 | 97 | plt.show() 98 | 99 | 100 | def is_final(x, y): 101 | if (x, y) in zip(x_wells, y_wells) or (x, y) == (x_final, y_final): 102 | return True 103 | return False 104 | 105 | 106 | def select_action(epsilon, i, j): 107 | if np.random.uniform(0.0, 1.0) < epsilon: 108 | return np.random.randint(0, nb_actions) 109 | return np.argmax(Q[i, j]) 110 | 111 | 112 | def q_step(epsilon): 113 | e = 0 114 | 115 | i = x_start 116 | j = y_start 117 | 118 | while e < max_steps: 119 | e += 1 120 | 121 | action = select_action(epsilon, i, j) 122 | 123 | if action == 0: 124 | if i == 0: 125 | x = 0 126 | else: 127 | x = i - 1 128 | y = j 129 | 130 | elif action == 1: 131 | if j == width - 1: 132 | y = width - 1 133 | else: 134 | y = j + 1 135 | x = i 136 | 137 | elif action == 2: 138 | if i == height - 1: 139 | x = height - 1 140 | else: 141 | x = i + 1 142 | y = j 143 | 144 | else: 145 | if j == 0: 146 | y = 0 147 | else: 148 | y = j - 1 149 | x = i 150 | 151 | reward = tunnel_rewards[x, y] 152 | 153 | if is_final(x, y): 154 | Q[i, j, action] += alpha * (reward - Q[i, j, action]) 155 | break 156 | 157 | else: 158 | Q[i, j, action] += alpha * (reward + (gamma * np.max(Q[x, y])) - Q[i, j, action]) 159 | 160 | i = x 161 | j = y 162 | 163 | 164 | if __name__ == '__main__': 165 | # Show tunnel rewards 166 | fig, ax = plt.subplots(figsize=(15, 6)) 167 | 168 | ax.matshow(tunnel_rewards, cmap=cm.Pastel1) 169 | ax.set_xlabel('y') 170 | ax.set_ylabel('x') 171 | ax.set_xticks(np.arange(width)) 172 | ax.set_yticks(np.arange(height)) 173 | ax.set_title('Rewards') 174 | 175 | for i in range(height): 176 | for j in range(width): 177 | msg = '{:.2f}'.format(tunnel_rewards[i, j]) 178 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 179 | 180 | plt.show() 181 | 182 | # Show initial values 183 | show_values(t=0) 184 | 185 | # Show initial policy 186 | show_policy(t=0) 187 | 188 | # Train the model 189 | for t in range(n_episodes): 190 | epsilon = 0.0 191 | 192 | if t <= n_exploration: 193 | epsilon = 1.0 - (float(t) / float(n_exploration)) 194 | 195 | q_step(epsilon) 196 | 197 | # Show final values 198 | show_values(t=n_episodes) 199 | 200 | # Show final policy 201 | show_policy(t=n_episodes) -------------------------------------------------------------------------------- /Chapter15/q_learning_nn.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import matplotlib.cm as cm 3 | import numpy as np 4 | 5 | from keras.models import Sequential 6 | from keras.layers import Dense, Activation 7 | 8 | 9 | # Set random seed for reproducibility 10 | np.random.seed(1000) 11 | 12 | 13 | width = 5 14 | height = 5 15 | 16 | y_final = width - 1 17 | x_final = height - 1 18 | 19 | y_wells = [0, 1, 3, 4] 20 | x_wells = [3, 1, 2, 0] 21 | 22 | tunnel = np.zeros(shape=(height, width), dtype=np.float32) 23 | 24 | max_steps = 150 25 | gamma = 0.95 26 | n_episodes = 10000 27 | n_exploration = 7500 28 | 29 | 30 | # Initial tunnel rewards 31 | standard_reward = -0.1 32 | tunnel_rewards = np.ones(shape=(height, width)) * standard_reward 33 | 34 | for x_well, y_well in zip(x_wells, y_wells): 35 | tunnel_rewards[x_well, y_well] = -5.0 36 | 37 | tunnel_rewards[x_final, y_final] = 5.0 38 | 39 | # Setup allowed starting points 40 | xy_grid = np.meshgrid(np.arange(0, height), np.arange(0, width), sparse=False) 41 | xy_grid = np.array(xy_grid).T.reshape(-1, 2) 42 | 43 | xy_final = list(zip(x_wells, y_wells)) 44 | xy_final.append([x_final, y_final]) 45 | 46 | xy_start = [] 47 | 48 | for x, y in xy_grid: 49 | if (x, y) not in xy_final: 50 | xy_start.append([x, y]) 51 | 52 | xy_start = np.array(xy_start) 53 | 54 | 55 | def starting_point(): 56 | xy = np.squeeze(xy_start[np.random.randint(0, xy_start.shape[0], size=1)]) 57 | return xy[0], xy[1] 58 | 59 | 60 | x_start = 0 61 | y_start = 0 62 | nb_actions = 4 63 | 64 | 65 | # Neural model 66 | model = Sequential() 67 | 68 | model.add(Dense(8, input_dim=width * height)) 69 | model.add(Activation('tanh')) 70 | 71 | model.add(Dense(4)) 72 | model.add(Activation('tanh')) 73 | 74 | model.add(Dense(nb_actions)) 75 | model.add(Activation('linear')) 76 | 77 | # Compile the model 78 | model.compile(optimizer='rmsprop', 79 | loss='mse') 80 | 81 | 82 | def train(state, q_value): 83 | model.train_on_batch(np.expand_dims(state.flatten(), axis=0), np.expand_dims(q_value, axis=0)) 84 | 85 | 86 | def get_Q_value(state): 87 | return model.predict(np.expand_dims(state.flatten(), axis=0))[0] 88 | 89 | 90 | def select_action_neural_network(epsilon, state): 91 | Q_value = get_Q_value(state) 92 | 93 | if np.random.uniform(0.0, 1.0) < epsilon: 94 | return Q_value, np.random.randint(0, nb_actions) 95 | 96 | return Q_value, np.argmax(Q_value) 97 | 98 | 99 | def is_final(x, y): 100 | if (x, y) in zip(x_wells, y_wells) or (x, y) == (x_final, y_final): 101 | return True 102 | return False 103 | 104 | 105 | def reset_tunnel(): 106 | tunnel = np.zeros(shape=(height, width), dtype=np.float32) 107 | 108 | for x_well, y_well in zip(x_wells, y_wells): 109 | tunnel[x_well, y_well] = -1.0 110 | 111 | tunnel[x_final, y_final] = 0.5 112 | 113 | return tunnel 114 | 115 | 116 | def q_step_neural_network(epsilon, initial_state): 117 | e = 0 118 | total_reward = 0.0 119 | 120 | (i, j) = starting_point() 121 | 122 | prev_value = 0.0 123 | tunnel = initial_state.copy() 124 | tunnel[i, j] = 1.0 125 | 126 | while e < max_steps: 127 | e += 1 128 | 129 | q_value, action = select_action_neural_network(epsilon, tunnel) 130 | 131 | if action == 0: 132 | if i == 0: 133 | x = 0 134 | else: 135 | x = i - 1 136 | y = j 137 | 138 | elif action == 1: 139 | if j == width - 1: 140 | y = width - 1 141 | else: 142 | y = j + 1 143 | x = i 144 | 145 | elif action == 2: 146 | if i == height - 1: 147 | x = height - 1 148 | else: 149 | x = i + 1 150 | y = j 151 | 152 | else: 153 | if j == 0: 154 | y = 0 155 | else: 156 | y = j - 1 157 | x = i 158 | 159 | reward = tunnel_rewards[x, y] 160 | total_reward += reward 161 | 162 | tunnel_n = tunnel.copy() 163 | tunnel_n[i, j] = prev_value 164 | tunnel_n[x, y] = 1.0 165 | 166 | prev_value = tunnel[x, y] 167 | 168 | if is_final(x, y): 169 | q_value[action] = reward 170 | train(tunnel, q_value) 171 | break 172 | 173 | else: 174 | q_value[action] = reward + (gamma * np.max(get_Q_value(tunnel_n))) 175 | train(tunnel, q_value) 176 | 177 | i = x 178 | j = y 179 | 180 | tunnel = tunnel_n.copy() 181 | 182 | return total_reward 183 | 184 | 185 | if __name__ == '__main__': 186 | # Show tunnel rewards 187 | fig, ax = plt.subplots(figsize=(15, 6)) 188 | 189 | ax.matshow(tunnel_rewards, cmap=cm.Pastel1) 190 | ax.set_xlabel('y') 191 | ax.set_ylabel('x') 192 | ax.set_xticks(np.arange(width)) 193 | ax.set_yticks(np.arange(height)) 194 | ax.set_title('Rewards') 195 | 196 | for i in range(height): 197 | for j in range(width): 198 | msg = '{:.2f}'.format(tunnel_rewards[i, j]) 199 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 200 | 201 | plt.show() 202 | 203 | # Train the model 204 | total_rewards = [] 205 | 206 | for t in range(n_episodes): 207 | tunnel = reset_tunnel() 208 | 209 | epsilon = 0.0 210 | 211 | if t <= n_exploration: 212 | epsilon = 1.0 - (float(t) / float(n_exploration)) 213 | 214 | t_reward = q_step_neural_network(epsilon, tunnel) 215 | total_rewards.append(t_reward) 216 | print('{} - {:.2f}'.format(t + 1, t_reward)) 217 | 218 | # Show total rewards 219 | fig, ax = plt.subplots(figsize=(15, 8)) 220 | 221 | ax.plot(total_rewards) 222 | ax.set_xlabel('Episode') 223 | ax.set_ylabel('Total Reward') 224 | ax.set_title('Total Rewards (t={})'.format(n_episodes)) 225 | ax.grid() 226 | plt.show() 227 | 228 | # Generate some trajectories 229 | trajectories = [] 230 | tunnels_c = [] 231 | 232 | for i, j in xy_start: 233 | tunnel = reset_tunnel() 234 | 235 | prev_value = 0.0 236 | 237 | trajectory = [[i, j, -1]] 238 | 239 | tunnel_c = tunnel.copy() 240 | tunnel[i, j] = 1.0 241 | tunnel_c[i, j] = 1.0 242 | 243 | final = False 244 | e = 0 245 | 246 | while not final and e < max_steps: 247 | e += 1 248 | 249 | q_value = get_Q_value(tunnel) 250 | action = np.argmax(q_value) 251 | 252 | if action == 0: 253 | if i == 0: 254 | x = 0 255 | else: 256 | x = i - 1 257 | y = j 258 | 259 | elif action == 1: 260 | if j == width - 1: 261 | y = width - 1 262 | else: 263 | y = j + 1 264 | x = i 265 | 266 | elif action == 2: 267 | if i == height - 1: 268 | x = height - 1 269 | else: 270 | x = i + 1 271 | y = j 272 | 273 | else: 274 | if j == 0: 275 | y = 0 276 | else: 277 | y = j - 1 278 | x = i 279 | 280 | trajectory[e - 1][2] = action 281 | trajectory.append([x, y, -1]) 282 | 283 | tunnel[i, j] = prev_value 284 | 285 | prev_value = tunnel[x, y] 286 | 287 | tunnel[x, y] = 1.0 288 | tunnel_c[x, y] = 1.0 289 | 290 | i = x 291 | j = y 292 | 293 | final = is_final(x, y) 294 | 295 | trajectories.append(np.array(trajectory)) 296 | tunnels_c.append(tunnel_c) 297 | 298 | trajectories = np.array(trajectories) 299 | 300 | # Show the trajectories 301 | fig, ax = plt.subplots(3, 5, figsize=(14, 8)) 302 | 303 | for i in range(3): 304 | for j in range(5): 305 | ax[i, j].matshow(tunnels_c[(j * 4) + i], cmap=cm.Pastel1) 306 | ax[i, j].set_xticks([]) 307 | ax[i, j].set_yticks([]) 308 | 309 | for x, y, action in trajectories[(j * 4) + i]: 310 | if x == x_final and y == y_final: 311 | msg = 'E' 312 | else: 313 | if action == -1: 314 | msg = r'$\otimes$' 315 | elif action == 0: 316 | msg = r'$\uparrow$' 317 | elif action == 1: 318 | msg = r'$\rightarrow$' 319 | elif action == 2: 320 | msg = r'$\downarrow$' 321 | else: 322 | msg = r'$\leftarrow$' 323 | 324 | ax[i, j].text(x=y, y=x, s=msg, va='center', ha='center') 325 | 326 | plt.show() -------------------------------------------------------------------------------- /Chapter15/sarsa.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import matplotlib.cm as cm 3 | import numpy as np 4 | 5 | 6 | # Set random seed for reproducibility 7 | np.random.seed(1000) 8 | 9 | 10 | width = 15 11 | height = 5 12 | 13 | y_final = width - 1 14 | x_final = height - 1 15 | 16 | y_wells = [0, 1, 3, 5, 5, 7, 9, 11, 12, 14] 17 | x_wells = [3, 1, 2, 0, 4, 1, 3, 2, 4, 1] 18 | 19 | 20 | # Initial tunnel rewards 21 | standard_reward = -0.1 22 | tunnel_rewards = np.ones(shape=(height, width)) * standard_reward 23 | 24 | for x_well, y_well in zip(x_wells, y_wells): 25 | tunnel_rewards[x_well, y_well] = -5.0 26 | 27 | tunnel_rewards[x_final, y_final] = 5.0 28 | 29 | 30 | gamma = 0.9 31 | nb_actions = 4 32 | 33 | Q = np.zeros(shape=(height, width, nb_actions)) 34 | 35 | x_start = 0 36 | y_start = 0 37 | 38 | max_steps = 2000 39 | alpha = 0.25 40 | n_episodes = 20000 41 | n_exploration = 15000 42 | 43 | 44 | def show_values(t): 45 | fig, ax = plt.subplots(figsize=(15, 6)) 46 | 47 | ax.matshow(np.max(Q, axis=2), cmap=cm.Pastel1) 48 | ax.set_xlabel('y') 49 | ax.set_ylabel('x') 50 | ax.set_xticks(np.arange(width)) 51 | ax.set_yticks(np.arange(height)) 52 | ax.set_title('Values (t={})'.format(t)) 53 | 54 | for i in range(height): 55 | for j in range(width): 56 | if i == x_final and j == y_final: 57 | msg = 'E' 58 | elif (i, j) in zip(x_wells, y_wells): 59 | msg = r'$\otimes$' 60 | else: 61 | msg = '{:.2f}'.format(np.max(Q[i, j])) 62 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 63 | 64 | plt.show() 65 | 66 | 67 | def show_policy(t): 68 | fig, ax = plt.subplots(figsize=(15, 6)) 69 | 70 | ax.matshow(np.zeros_like(Q[:, :, 0]), cmap=cm.Pastel1) 71 | ax.set_xlabel('y') 72 | ax.set_ylabel('x') 73 | ax.set_xticks(np.arange(width)) 74 | ax.set_yticks(np.arange(height)) 75 | ax.set_title('Policy (t={})'.format(t)) 76 | 77 | for i in range(height): 78 | for j in range(width): 79 | action = np.argmax(Q[i, j]) 80 | 81 | if i == x_final and j == y_final: 82 | msg = 'E' 83 | elif (i, j) in zip(x_wells, y_wells): 84 | msg = r'$\otimes$' 85 | else: 86 | if action == 0: 87 | msg = r'$\uparrow$' 88 | elif action == 1: 89 | msg = r'$\rightarrow$' 90 | elif action == 2: 91 | msg = r'$\downarrow$' 92 | else: 93 | msg = r'$\leftarrow$' 94 | 95 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 96 | 97 | plt.show() 98 | 99 | 100 | def is_final(x, y): 101 | if (x, y) in zip(x_wells, y_wells) or (x, y) == (x_final, y_final): 102 | return True 103 | return False 104 | 105 | 106 | def select_action(epsilon, i, j): 107 | if np.random.uniform(0.0, 1.0) < epsilon: 108 | return np.random.randint(0, nb_actions) 109 | return np.argmax(Q[i, j]) 110 | 111 | 112 | def sarsa_step(epsilon): 113 | e = 0 114 | 115 | i = x_start 116 | j = y_start 117 | 118 | while e < max_steps: 119 | e += 1 120 | 121 | action = select_action(epsilon, i, j) 122 | 123 | if action == 0: 124 | if i == 0: 125 | x = 0 126 | else: 127 | x = i - 1 128 | y = j 129 | 130 | elif action == 1: 131 | if j == width - 1: 132 | y = width - 1 133 | else: 134 | y = j + 1 135 | x = i 136 | 137 | elif action == 2: 138 | if i == height - 1: 139 | x = height - 1 140 | else: 141 | x = i + 1 142 | y = j 143 | 144 | else: 145 | if j == 0: 146 | y = 0 147 | else: 148 | y = j - 1 149 | x = i 150 | 151 | action_n = select_action(epsilon, x, y) 152 | reward = tunnel_rewards[x, y] 153 | 154 | if is_final(x, y): 155 | Q[i, j, action] += alpha * (reward - Q[i, j, action]) 156 | break 157 | 158 | else: 159 | Q[i, j, action] += alpha * (reward + (gamma * Q[x, y, action_n]) - Q[i, j, action]) 160 | 161 | i = x 162 | j = y 163 | 164 | 165 | if __name__ == '__main__': 166 | # Show tunnel rewards 167 | fig, ax = plt.subplots(figsize=(15, 6)) 168 | 169 | ax.matshow(tunnel_rewards, cmap=cm.Pastel1) 170 | ax.set_xlabel('y') 171 | ax.set_ylabel('x') 172 | ax.set_xticks(np.arange(width)) 173 | ax.set_yticks(np.arange(height)) 174 | ax.set_title('Rewards') 175 | 176 | for i in range(height): 177 | for j in range(width): 178 | msg = '{:.2f}'.format(tunnel_rewards[i, j]) 179 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 180 | 181 | plt.show() 182 | 183 | # Show initial values 184 | show_values(t=0) 185 | 186 | # Show initial policy 187 | show_policy(t=0) 188 | 189 | # Train the model 190 | for t in range(n_episodes): 191 | epsilon = 0.0 192 | 193 | if t <= n_exploration: 194 | epsilon = 1.0 - (float(t) / float(n_exploration)) 195 | 196 | sarsa_step(epsilon) 197 | 198 | # Show final values 199 | show_values(t=n_episodes) 200 | 201 | # Show final policy 202 | show_policy(t=n_episodes) -------------------------------------------------------------------------------- /Chapter15/td_lambda.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import matplotlib.cm as cm 3 | import numpy as np 4 | 5 | 6 | # Set random seed for reproducibility 7 | np.random.seed(1000) 8 | 9 | 10 | width = 15 11 | height = 5 12 | 13 | y_final = width - 1 14 | x_final = height - 1 15 | 16 | y_wells = [0, 1, 3, 5, 5, 6, 7, 9, 10, 11, 12, 14] 17 | x_wells = [3, 1, 2, 0, 4, 3, 1, 3, 1, 2, 4, 1] 18 | 19 | y_prizes = [0, 3, 4, 6, 7, 8, 9, 12] 20 | x_prizes = [2, 4, 3, 2, 1, 4, 0, 2] 21 | 22 | gamma = 0.95 23 | tolerance = 1e-5 24 | nb_actions = 4 25 | max_steps = 1000 26 | alpha = 0.25 27 | lambd = 0.6 28 | n_episodes = 5000 29 | 30 | standard_reward = -0.1 31 | tunnel_rewards = np.ones(shape=(height, width)) * standard_reward 32 | 33 | tunnel_values = np.zeros(shape=(height, width)) 34 | eligibility_traces = np.zeros(shape=(height, width)) 35 | policy = np.random.randint(0, nb_actions, size=(height, width)).astype(np.uint8) 36 | 37 | # Setup allowed starting points 38 | xy_grid = np.meshgrid(np.arange(0, height), np.arange(0, width), sparse=False) 39 | xy_grid = np.array(xy_grid).T.reshape(-1, 2) 40 | 41 | xy_final = list(zip(x_wells, y_wells)) + list(zip(x_prizes, y_prizes)) 42 | xy_final.append([x_final, y_final]) 43 | 44 | xy_start = [] 45 | 46 | for x, y in xy_grid: 47 | if (x, y) not in xy_final: 48 | xy_start.append([x, y]) 49 | 50 | xy_start = np.array(xy_start) 51 | 52 | 53 | def starting_point(): 54 | xy = np.squeeze(xy_start[np.random.randint(0, xy_start.shape[0], size=1)]) 55 | return xy[0], xy[1] 56 | 57 | 58 | def init_tunnel_rewards(): 59 | for x_well, y_well in zip(x_wells, y_wells): 60 | tunnel_rewards[x_well, y_well] = -5.0 61 | 62 | for x_prize, y_prize in zip(x_prizes, y_prizes): 63 | tunnel_rewards[x_prize, y_prize] = 1.0 64 | 65 | tunnel_rewards[x_final, y_final] = 5.0 66 | 67 | 68 | def show_values(t): 69 | fig, ax = plt.subplots(figsize=(15, 6)) 70 | 71 | ax.matshow(tunnel_values, cmap=cm.Pastel1) 72 | ax.set_xlabel('y') 73 | ax.set_ylabel('x') 74 | ax.set_xticks(np.arange(width)) 75 | ax.set_yticks(np.arange(height)) 76 | ax.set_title('Values (t={})'.format(t)) 77 | 78 | for i in range(height): 79 | for j in range(width): 80 | if i == x_final and j == y_final: 81 | msg = 'E' 82 | elif (i, j) in zip(x_wells, y_wells): 83 | msg = r'$\otimes$' 84 | elif (i, j) in zip(x_prizes, y_prizes): 85 | msg = r'$\diamond$' 86 | else: 87 | msg = '{:.2f}'.format(tunnel_values[i, j]) 88 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 89 | 90 | plt.show() 91 | 92 | 93 | def show_policy(t): 94 | fig, ax = plt.subplots(figsize=(15, 6)) 95 | 96 | ax.matshow(np.zeros_like(tunnel_values), cmap=cm.Pastel1) 97 | ax.set_xlabel('y') 98 | ax.set_ylabel('x') 99 | ax.set_xticks(np.arange(width)) 100 | ax.set_yticks(np.arange(height)) 101 | ax.set_title('Policy (t={})'.format(t)) 102 | 103 | for i in range(height): 104 | for j in range(width): 105 | action = policy[i, j] 106 | 107 | if i == x_final and j == y_final: 108 | msg = 'E' 109 | elif (i, j) in zip(x_wells, y_wells): 110 | msg = r'$\otimes$' 111 | else: 112 | msg = '' 113 | if (i, j) in zip(x_prizes, y_prizes): 114 | msg = r'$\diamond$ ' 115 | 116 | if action == 0: 117 | msg += r'$\uparrow$' 118 | elif action == 1: 119 | msg += r'$\rightarrow$' 120 | elif action == 2: 121 | msg += r'$\downarrow$' 122 | else: 123 | msg += r'$\leftarrow$' 124 | 125 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 126 | 127 | plt.show() 128 | 129 | 130 | def is_final(x, y): 131 | if (x, y) in zip(x_wells, y_wells) or (x, y) == (x_final, y_final): 132 | return True 133 | return False 134 | 135 | 136 | def episode(): 137 | (i, j) = starting_point() 138 | x = y = 0 139 | 140 | e = 0 141 | 142 | state_history = [(i, j)] 143 | 144 | init_tunnel_rewards() 145 | total_reward = 0.0 146 | 147 | while e < max_steps: 148 | e += 1 149 | 150 | action = policy[i, j] 151 | 152 | if action == 0: 153 | if i == 0: 154 | x = 0 155 | else: 156 | x = i - 1 157 | y = j 158 | 159 | elif action == 1: 160 | if j == width - 1: 161 | y = width - 1 162 | else: 163 | y = j + 1 164 | x = i 165 | 166 | elif action == 2: 167 | if i == height - 1: 168 | x = height - 1 169 | else: 170 | x = i + 1 171 | y = j 172 | 173 | else: 174 | if j == 0: 175 | y = 0 176 | else: 177 | y = j - 1 178 | x = i 179 | 180 | reward = tunnel_rewards[x, y] 181 | total_reward += reward 182 | 183 | td_error = reward + (gamma * tunnel_values[x, y]) - tunnel_values[i, j] 184 | eligibility_traces[i, j] += 1.0 185 | 186 | for sx, sy in state_history: 187 | tunnel_values[sx, sy] += (alpha * td_error * eligibility_traces[sx, sy]) 188 | eligibility_traces[sx, sy] *= (gamma * lambd) 189 | 190 | if is_final(x, y): 191 | break 192 | else: 193 | i = x 194 | j = y 195 | 196 | state_history.append([x, y]) 197 | 198 | tunnel_rewards[x_prizes, y_prizes] *= 0.85 199 | 200 | return total_reward 201 | 202 | 203 | def policy_selection(): 204 | for i in range(height): 205 | for j in range(width): 206 | if is_final(i, j): 207 | continue 208 | 209 | values = np.zeros(shape=(nb_actions,)) 210 | 211 | values[0] = (tunnel_rewards[i - 1, j] + (gamma * tunnel_values[i - 1, j])) if i > 0 else -np.inf 212 | values[1] = (tunnel_rewards[i, j + 1] + (gamma * tunnel_values[i, j + 1])) if j < width - 1 else -np.inf 213 | values[2] = (tunnel_rewards[i + 1, j] + (gamma * tunnel_values[i + 1, j])) if i < height - 1 else -np.inf 214 | values[3] = (tunnel_rewards[i, j - 1] + (gamma * tunnel_values[i, j - 1])) if j > 0 else -np.inf 215 | 216 | policy[i, j] = np.argmax(values).astype(np.uint8) 217 | 218 | 219 | if __name__ == '__main__': 220 | # Init tunnel rewards 221 | init_tunnel_rewards() 222 | 223 | # Show tunnel rewards 224 | fig, ax = plt.subplots(figsize=(15, 6)) 225 | 226 | ax.matshow(tunnel_rewards, cmap=cm.Pastel1) 227 | ax.set_xlabel('y') 228 | ax.set_ylabel('x') 229 | ax.set_xticks(np.arange(width)) 230 | ax.set_yticks(np.arange(height)) 231 | ax.set_title('Rewards') 232 | 233 | for i in range(height): 234 | for j in range(width): 235 | msg = '{:.2f}'.format(tunnel_rewards[i, j]) 236 | ax.text(x=j, y=i, s=msg, va='center', ha='center') 237 | 238 | plt.show() 239 | 240 | # Show initial values 241 | show_values(t=0) 242 | 243 | # Show initial policy 244 | show_policy(t=0) 245 | 246 | # Train the model 247 | total_rewards = [] 248 | 249 | for _ in range(n_episodes): 250 | e_reward = episode() 251 | total_rewards.append(e_reward) 252 | policy_selection() 253 | 254 | # Plot the total rewards 255 | fig, ax = plt.subplots(figsize=(15, 8)) 256 | 257 | ax.plot(total_rewards) 258 | ax.set_xlabel('Episode') 259 | ax.set_ylabel('Total Reward') 260 | ax.set_title('Total Rewards (t={})'.format(n_episodes)) 261 | ax.grid() 262 | plt.show() 263 | 264 | # Show final values 265 | show_values(t=n_episodes) 266 | 267 | # Show final policy 268 | show_policy(t=n_episodes) 269 | 270 | 271 | 272 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Machine Learning Summit 2025

2 | 3 | ## Machine Learning Summit 2025 4 | **Bridging Theory and Practice: ML Solutions for Today’s Challenges** 5 | 6 | 3 days, 20+ experts, and 25+ tech sessions and talks covering critical aspects of: 7 | - **Agentic and Generative AI** 8 | - **Applied Machine Learning in the Real World** 9 | - **ML Engineering and Optimization** 10 | 11 | 👉 [Book your ticket now >>](https://packt.link/mlsumgh) 12 | 13 | --- 14 | 15 | ## Join Our Newsletters 📬 16 | 17 | ### DataPro 18 | *The future of AI is unfolding. Don’t fall behind.* 19 | 20 |

DataPro QR

21 | 22 | Stay ahead with [**DataPro**](https://landing.packtpub.com/subscribe-datapronewsletter/?link_from_packtlink=yes), the free weekly newsletter for data scientists, AI/ML researchers, and data engineers. 23 | From trending tools like **PyTorch**, **scikit-learn**, **XGBoost**, and **BentoML** to hands-on insights on **database optimization** and real-world **ML workflows**, you’ll get what matters, fast. 24 | 25 | > Stay sharp with [DataPro](https://landing.packtpub.com/subscribe-datapronewsletter/?link_from_packtlink=yes). Join **115K+ data professionals** who never miss a beat. 26 | 27 | --- 28 | 29 | ### BIPro 30 | *Business runs on data. Make sure yours tells the right story.* 31 | 32 |

BIPro QR

33 | 34 | [**BIPro**](https://landing.packtpub.com/subscribe-bipro-newsletter/?link_from_packtlink=yes) is your free weekly newsletter for BI professionals, analysts, and data leaders. 35 | Get practical tips on **dashboarding**, **data visualization**, and **analytics strategy** with tools like **Power BI**, **Tableau**, **Looker**, **SQL**, and **dbt**. 36 | 37 | > Get smarter with [BIPro](https://landing.packtpub.com/subscribe-bipro-newsletter/?link_from_packtlink=yes). Trusted by **35K+ BI professionals**, see what you’re missing. 38 | 39 | 40 | 41 | 42 | # Mastering Machine Learning Algorithms 43 | This is the code repository for [Mastering Machine Learning Algorithms](https://www.packtpub.com/big-data-and-business-intelligence/mastering-machine-learning-algorithms?utm_source=github&utm_medium=repository&utm_campaign=9781788621113), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the book from start to finish. 44 | ## About the Book 45 | Machine learning is a subset of AI that aims to make modern-day computer systems smarter and more intelligent. The real power of machine learning resides in its algorithms, which make even the most difficult things capable of being handled by machines. However, with the advancement in the technology and requirements of data, machines will have to be smarter than they are today to meet the overwhelming data needs; mastering these algorithms and using them optimally is the need of the hour. 46 | ## Instructions and Navigation 47 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02. 48 | 49 | 50 | 51 | The code will look like the following: 52 | ``` 53 | from sklearn.model_selection import train_test_split 54 | X_train, X_test, Y_train, Y_test = train_test_split(X, Y, train_size=0.7, 55 | random_state=1) 56 | ``` 57 | 58 | There are no strict prerequisites for this book; however, it's important to have basic 59 | intermediate Python knowledge with a specific focus on NumPy. Whenever necessary, I 60 | will provide instructions/references to install specific packages and exploit more advanced 61 | functionalities. As Python is based on a semantic indentation, the published version can 62 | contain incorrect newlines that raise exceptions when executing the code. For this reason, I 63 | invite all readers without deep knowledge of this language to refer to the original source 64 | code provided with the book. 65 | All the examples are based on Python 3.5+. I suggest using the Anaconda distribution 66 | (https://www. anaconda. com/download/), which is probably the most complete and 67 | powerful one for scientific projects. The majority of the required packages are already built 68 | in and it's very easy to install the new ones (sometimes with optimized versions). However, 69 | any other Python distribution can be used. Moreover, I invite readers to test the examples 70 | using Jupyter (formerly known as IPython) notebooks so as to avoid rerunning the whole 71 | example when a change is made. If instead an IDE is preferred, I suggest PyCharm, which 72 | offers many built-in functionalities that are very helpful in data-oriented and scientific 73 | projects (such as the internal Matplotlib viewer). 74 | A good mathematics background is necessary to fully understand the theoretical part. In 75 | particular, basic skills in probability theory, calculus, and linear algebra are required. 76 | However, I advise you not to give up when a concept seems too difficult. The reference 77 | sections contain many useful books, and the majority of concepts are explained quite well 78 | on Wikipedia too. When something unknown is encountered, I suggest reading the specific 79 | documentation before continuing. In many cases, it's not necessary to have complete 80 | knowledge and even an introductory paragraph can be enough to understand their 81 | rationale. 82 | 83 | ## Related Products 84 | * [Hands-on Machine Learning with JavaScript](https://www.packtpub.com/big-data-and-business-intelligence/hands-machine-learning-javascript?utm_source=github&utm_medium=repository&utm_campaign=9781788998246) 85 | 86 | * [Learning JavaScript Data Structures and Algorithms - Third Edition](https://www.packtpub.com/web-development/learning-javascript-data-structures-and-algorithms-third-edition?utm_source=github&utm_medium=repository&utm_campaign=9781788623872) 87 | 88 | * [Mastering The Faster Web with PHP, MySQL, and JavaScript](https://www.packtpub.com/web-development/mastering-faster-web-php-mysql-and-javascript?utm_source=github&utm_medium=repository&utm_campaign=9781788392211) 89 | 90 | ### Suggestions and Feedback 91 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSe5qwunkGf6PUvzPirPDtuy1Du5Rlzew23UBp2S-P3wB-GcwQ/viewform) if you have any feedback or suggestions. 92 | ### Download a free PDF 93 | 94 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
95 |

https://packt.link/free-ebook/9781838820299

--------------------------------------------------------------------------------