├── .gitattributes ├── .gitignore ├── Chapter02 ├── .gitignore ├── data │ ├── test │ │ └── test_data.csv │ ├── train │ │ └── train_data.csv │ ├── val │ │ └── val_data.csv │ ├── winequality-names.txt │ ├── winequality-red.csv │ └── winequality-white.csv ├── data_prep.py ├── keras_regression.py ├── keras_regression_deeper.py └── pred_dist.jpg ├── Chapter03 ├── .gitignore ├── .idea │ ├── chapter_2.iml │ ├── encodings.xml │ ├── markdown-navigator │ │ └── profiles_settings.xml │ ├── misc.xml │ ├── modules.xml │ ├── vcs.xml │ └── workspace.xml ├── data │ ├── test │ │ └── test_data.csv │ ├── train │ │ └── train_data.csv │ └── val │ │ └── val_data.csv ├── keras_regression.py ├── keras_regression_deep_broken.py └── keras_regression_deeper.py ├── Chapter04 ├── binary_classifier.py ├── data │ ├── data.csv │ ├── test │ │ └── test_data.csv │ ├── train │ │ └── train_data.csv │ └── val │ │ └── val_data.csv ├── data_prep.py └── roc_callback.py ├── Chapter05 ├── multiclass_classifier.py ├── multiclass_classifier_dropout.py └── multiclass_classifier_l2.py ├── Chapter06 ├── hyperband-output-mnist-resultsonly.txt ├── hyperband-output-mnist.txt ├── hyperband.py ├── mnist_hyperband_search.py └── mnist_random_search.py ├── Chapter07 ├── cifar10_cnn.py └── cifar10_cnn_image_aug.py ├── Chapter08 ├── data_setup.py └── inceptionV3.py ├── Chapter09 ├── .gitattributes ├── data │ ├── .gitattributes │ └── bitcoin.csv ├── lstm_bitcoin.py ├── lstm_model.h5 └── predict_and_graph.py ├── Chapter10 ├── imdb_sentiment.py ├── newsgroup_classifier_pretrained_word_embeddings.py └── newsgroup_classifier_word_embeddings.py ├── Chapter11 ├── infer_char_seq2seq.py └── train_char_seq2seq.py ├── Chapter12 ├── dqn_BreakoutDeterministic-v4_log.json ├── dqn_BreakoutDeterministic-v4_weights.h5f ├── dqn_BreakoutDeterministic-v4_weights_1750000.h5f ├── dqn_LunarLander-v2_log.json ├── dqn_LunarLander-v2_weights.h5f ├── dqn_LunarLander-v2_weights_510000.h5f ├── dqn_breakout.py ├── dqn_breakout_test.py ├── dqn_cartpole.py ├── dqn_lunar_lander.py ├── dqn_lunar_lander_test.py └── visualize_log.py ├── Chapter13 ├── cifar_10_gan.py └── mnist_gan.py ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Chapter02/.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints* 2 | -------------------------------------------------------------------------------- /Chapter02/data/winequality-names.txt: -------------------------------------------------------------------------------- 1 | Citation Request: 2 | This dataset is public available for research. The details are described in [Cortez et al., 2009]. 3 | Please include this citation if you plan to use this database: 4 | 5 | P. Cortez, A. Cerdeira, F. Almeida, T. Matos and J. Reis. 6 | Modeling wine preferences by data mining from physicochemical properties. 7 | In Decision Support Systems>, Elsevier, 47(4):547-553. ISSN: 0167-9236. 8 | 9 | Available at: [@Elsevier] http://dx.doi.org/10.1016/j.dss.2009.05.016 10 | [Pre-press (pdf)] http://www3.dsi.uminho.pt/pcortez/winequality09.pdf 11 | [bib] http://www3.dsi.uminho.pt/pcortez/dss09.bib 12 | 13 | 1. Title: Wine Quality 14 | 15 | 2. Sources 16 | Created by: Paulo Cortez (Univ. Minho), António Cerdeira, Fernando Almeida, Telmo Matos and José Reis (CVRVV) @ 2009 17 | 18 | 3. Past Usage: 19 | 20 | P. Cortez, A. Cerdeira, F. Almeida, T. Matos and J. Reis. 21 | Modeling wine preferences by data mining from physicochemical properties. 22 | In Decision Support Systems>, Elsevier, 47(4):547-553. ISSN: 0167-9236. 23 | 24 | In the above reference, two datasets were created, using red and white wine samples. 25 | The inputs include objective tests (e.g. PH values) and the output is based on sensory data 26 | (median of at least 3 evaluations made by wine experts). Each expert graded the wine quality 27 | between 0 (very bad) and 10 (very excellent). Several data mining methods were applied to model 28 | these datasets under a regression approach. The support vector machine model achieved the 29 | best results. Several metrics were computed: MAD, confusion matrix for a fixed error tolerance (T), 30 | etc. Also, we plot the relative importances of the input variables (as measured by a sensitivity 31 | analysis procedure). 32 | 33 | 4. Relevant Information: 34 | 35 | These datasets can be viewed as classification or regression tasks. 36 | The classes are ordered and not balanced (e.g. there are munch more normal wines than 37 | excellent or poor ones). Outlier detection algorithms could be used to detect the few excellent 38 | or poor wines. Also, we are not sure if all input variables are relevant. So 39 | it could be interesting to test feature selection methods. 40 | 41 | 5. Number of Instances: red wine - 1599; white wine - 4898. 42 | 43 | 6. Number of Attributes: 11 + output attribute 44 | 45 | Note: several of the attributes may be correlated, thus it makes sense to apply some sort of 46 | feature selection. 47 | 48 | 7. Attribute information: 49 | 50 | For more information, read [Cortez et al., 2009]. 51 | 52 | Input variables (based on physicochemical tests): 53 | 1 - fixed acidity 54 | 2 - volatile acidity 55 | 3 - citric acid 56 | 4 - residual sugar 57 | 5 - chlorides 58 | 6 - free sulfur dioxide 59 | 7 - total sulfur dioxide 60 | 8 - density 61 | 9 - pH 62 | 10 - sulphates 63 | 11 - alcohol 64 | Output variable (based on sensory data): 65 | 12 - quality (score between 0 and 10) 66 | 67 | 8. Missing Attribute Values: None 68 | -------------------------------------------------------------------------------- /Chapter02/data_prep.py: -------------------------------------------------------------------------------- 1 | # Deep Learning Quick Reference Chapter 2: Using Deep Learning To Solve Regression Problems 2 | # Mike Bernico 3 | # Data prep code - Creates train/val/test split for winequality-white.csv 4 | # NOTE: The reader should not need to run this. 5 | 6 | import pandas as pd 7 | from sklearn.model_selection import train_test_split 8 | import os 9 | import random 10 | random.seed = 42 11 | 12 | 13 | def safe_create_directory(dir_name): 14 | if not os.path.isdir((os.path.join(os.getcwd(), "data", dir_name))): 15 | os.mkdir(os.path.join(os.getcwd(), "data", dir_name)) 16 | 17 | 18 | def create_data_dirs(): 19 | [safe_create_directory(dir) for dir in ["train", "val", "test"]] 20 | 21 | 22 | def train_val_test_split(df, val_pct=0.1, test_pct=0.1): 23 | size = df.shape[0] 24 | val_pct = (val_pct * size) / (size * (1 - test_pct)) 25 | train_val, test = train_test_split(df, test_size=test_pct) 26 | train, val = train_test_split(train_val, test_size=val_pct) 27 | return train, val, test 28 | 29 | 30 | def serialize_dataset(dataset): 31 | for k,v in dataset.items(): 32 | out_filename = os.path.join(os.getcwd(), "data", k, k + "_data.csv") 33 | v.to_csv(out_filename, sep=",", index=False) 34 | print("Writing " + k + " to " + out_filename + " Shape:" + str(v.shape)) 35 | 36 | 37 | def main(): 38 | df = pd.read_csv("./data/winequality-white.csv", sep=";") 39 | create_data_dirs() 40 | dataset = dict() 41 | dataset["train"], dataset["val"], dataset["test"] = train_val_test_split(df) 42 | serialize_dataset(dataset) 43 | 44 | 45 | if __name__ == "__main__": 46 | main() 47 | -------------------------------------------------------------------------------- /Chapter02/keras_regression.py: -------------------------------------------------------------------------------- 1 | # Deep Learning Quick Reference Chapter 2: Using Deep Learning To Solve Regression Problems 2 | # Mike Bernico 3 | 4 | # random seed setting for reproducibility 5 | from numpy.random import seed 6 | seed(42) 7 | from tensorflow import set_random_seed 8 | set_random_seed(42) 9 | 10 | 11 | import pandas as pd 12 | from sklearn.preprocessing import StandardScaler 13 | from keras.models import Model 14 | from keras.layers import Input, Dense 15 | from sklearn.metrics import mean_absolute_error 16 | import seaborn as sns 17 | from matplotlib import pyplot as plt 18 | 19 | 20 | 21 | TRAIN_DATA = "./data/train/train_data.csv" 22 | VAL_DATA = "./data/val/val_data.csv" 23 | TEST_DATA = "./data/test/test_data.csv" 24 | 25 | def load_data(): 26 | """Loads train, val, and test datasets from disk""" 27 | train = pd.read_csv(TRAIN_DATA) 28 | val = pd.read_csv(VAL_DATA) 29 | test = pd.read_csv(TEST_DATA) 30 | 31 | # we will use sklearn's StandardScaler to scale our data to 0 mean, unit variance. 32 | scaler = StandardScaler() 33 | train = scaler.fit_transform(train) 34 | val = scaler.transform(val) 35 | test = scaler.transform(test) 36 | # we will use a dict to keep all this data tidy. 37 | data = dict() 38 | 39 | data["train_y"] = train[:, 10] 40 | data["train_X"] = train[:, 0:9] 41 | data["val_y"] = val[:, 10] 42 | data["val_X"] = val[:, 0:9] 43 | data["test_y"] = test[:, 10] 44 | data["test_X"] = test[:, 0:9] 45 | # it's a good idea to keep the scaler (or at least the mean/variance) so we can unscale predictions 46 | data["scaler"] = scaler 47 | return data 48 | 49 | def build_network(input_features=None): 50 | # first we specify an input layer, with a shape == features 51 | inputs = Input(shape=(input_features,), name="input") 52 | # 32 neuron hidden layer 53 | x = Dense(32, activation='relu', name="hidden")(inputs) 54 | # for regression we will use a single neuron with linear (no) activation 55 | prediction = Dense(1, activation='linear', name="final")(x) 56 | 57 | model = Model(inputs=inputs, outputs=prediction) 58 | model.compile(optimizer='adam', loss='mean_absolute_error') 59 | return model 60 | 61 | def main(): 62 | data = load_data() 63 | 64 | # Network with single 32 neuron hidden layer... 65 | input_features = data["train_X"].shape[1] 66 | model = build_network(input_features=input_features) 67 | print("Network Structure") 68 | print(model.summary()) 69 | print("Training Data Shape: " + str(data["train_X"].shape)) 70 | model.fit(x=data["train_X"], y=data["train_y"], batch_size=32, epochs=200, verbose=1, 71 | validation_data=(data["val_X"], data["val_y"])) 72 | 73 | # an example of saving the model for later use 74 | model.save("regression_model.h5") 75 | # use model = model.load("regression_model.h5") to load it 76 | 77 | print("Model Train MAE: " + str(mean_absolute_error(data["train_y"], model.predict(data["train_X"])))) 78 | print("Model Val MAE: " + str(mean_absolute_error(data["val_y"], model.predict(data["val_X"])))) 79 | print("Model Test MAE: " + str(mean_absolute_error(data["test_y"], model.predict(data["test_X"])))) 80 | 81 | plt.title("Predicted Distribution vs. Actual") 82 | y_hat = model.predict(data["test_X"]) 83 | sns.distplot(y_hat.flatten(), label="y_hat") 84 | sns.distplot(data["test_y"], label="y_true") 85 | plt.legend() 86 | plt.savefig("pred_dist.jpg") 87 | 88 | 89 | 90 | if __name__ == "__main__": 91 | main() 92 | 93 | -------------------------------------------------------------------------------- /Chapter02/keras_regression_deeper.py: -------------------------------------------------------------------------------- 1 | # Deep Learning Quick Reference Chapter 2: Using Deep Learning To Solve Regression Problems 2 | # Mike Bernico 3 | 4 | # random seed setting for reproducibility 5 | from numpy.random import seed 6 | seed(42) 7 | from tensorflow import set_random_seed 8 | set_random_seed(42) 9 | 10 | import pandas as pd 11 | from sklearn.preprocessing import StandardScaler 12 | from keras.models import Model 13 | from keras.layers import Input, Dense 14 | from sklearn.metrics import mean_absolute_error 15 | import seaborn as sns 16 | from matplotlib import pyplot as plt 17 | 18 | 19 | TRAIN_DATA = "./data/train/train_data.csv" 20 | VAL_DATA = "./data/val/val_data.csv" 21 | TEST_DATA = "./data/test/test_data.csv" 22 | 23 | def load_data(): 24 | """Loads train, val, and test datasets from disk""" 25 | train = pd.read_csv(TRAIN_DATA) 26 | val = pd.read_csv(VAL_DATA) 27 | test = pd.read_csv(TEST_DATA) 28 | 29 | # we will use sklearn's StandardScaler to scale our data to 0 mean, unit variance. 30 | scaler = StandardScaler() 31 | train = scaler.fit_transform(train) 32 | val = scaler.transform(val) 33 | test = scaler.transform(test) 34 | # we will use a dict to keep all this data tidy. 35 | data = dict() 36 | 37 | data["train_y"] = train[:, 10] 38 | data["train_X"] = train[:, 0:9] 39 | data["val_y"] = val[:, 10] 40 | data["val_X"] = val[:, 0:9] 41 | data["test_y"] = test[:, 10] 42 | data["test_X"] = test[:, 0:9] 43 | # it's a good idea to keep the scaler (or at least the mean/variance) so we can unscale predictions 44 | data["scaler"] = scaler 45 | return data 46 | 47 | def build_network(input_features=None): 48 | # first we specify an input layer, with a shape == features 49 | inputs = Input(shape=(input_features,), name="input") 50 | x = Dense(32, activation='relu', name="hidden1")(inputs) 51 | x = Dense(32, activation='relu', name="hidden2")(x) 52 | x = Dense(32, activation='relu', name="hidden3")(x) 53 | x = Dense(32, activation='relu', name="hidden4")(x) 54 | x = Dense(16, activation='relu', name="hidden5")(x) 55 | # for regression we will use a single neuron with linear (no) activation 56 | prediction = Dense(1, activation='linear', name="final")(x) 57 | 58 | model = Model(inputs=inputs, outputs=prediction) 59 | model.compile(optimizer='adam', loss='mean_absolute_error') 60 | return model 61 | 62 | def main(): 63 | data = load_data() 64 | 65 | # Network with single 32 neuron hidden layer... 66 | input_features = data["train_X"].shape[1] 67 | model = build_network(input_features=input_features) 68 | print("Network Structure") 69 | print(model.summary()) 70 | print("Training Data Shape: " + str(data["train_X"].shape)) 71 | model.fit(x=data["train_X"], y=data["train_y"], batch_size=32, epochs=500, verbose=1, 72 | validation_data=(data["val_X"], data["val_y"])) 73 | 74 | # an example of saving the model for later use 75 | model.save("regression_model.h5") 76 | # use model = model.load("regression_model.h5") to load it 77 | 78 | print("Model Train MAE: " + str(mean_absolute_error(data["train_y"], model.predict(data["train_X"])))) 79 | print("Model Val MAE: " + str(mean_absolute_error(data["val_y"], model.predict(data["val_X"])))) 80 | print("Model Test MAE: " + str(mean_absolute_error(data["test_y"], model.predict(data["test_X"])))) 81 | 82 | plt.title("Predicted Distribution vs. Actual") 83 | y_hat = model.predict(data["test_X"]) 84 | sns.distplot(y_hat.flatten(), label="y_hat") 85 | sns.distplot(data["test_y"], label="y_true") 86 | plt.legend() 87 | plt.savefig("pred_dist_deep.jpg") 88 | 89 | 90 | 91 | if __name__ == "__main__": 92 | main() 93 | 94 | -------------------------------------------------------------------------------- /Chapter02/pred_dist.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Deep-Learning-Quick-Reference/fb7913e8f818a2d968455184eab11a4ea0fb7223/Chapter02/pred_dist.jpg -------------------------------------------------------------------------------- /Chapter03/.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints* 2 | -------------------------------------------------------------------------------- /Chapter03/.idea/chapter_2.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 12 | -------------------------------------------------------------------------------- /Chapter03/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter03/.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /Chapter03/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 33 | 34 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /Chapter03/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter03/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter03/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 74 | 75 | 76 | 78 | 79 | 88 | 89 | 90 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 |