|
| 1 | +from __future__ import division, print_function |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import argparse |
| 6 | +import logging |
| 7 | + |
| 8 | + |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +# For non-interactive plot |
| 12 | +import matplotlib as mpl |
| 13 | +mpl.use('Agg') |
| 14 | +import matplotlib.pyplot as plt |
| 15 | + |
| 16 | + |
| 17 | +import neon |
| 18 | +from neon.util.argparser import NeonArgparser |
| 19 | +from neon.data import ArrayIterator |
| 20 | +from neon.callbacks.callbacks import Callbacks |
| 21 | +from neon.layers import GeneralizedCost, Affine, Dropout, Reshape |
| 22 | +from neon.models import Model |
| 23 | +from neon.backends import gen_backend |
| 24 | + |
| 25 | +#from neon import logger as neon_logger |
| 26 | + |
| 27 | +import p1b1 |
| 28 | +import p1_common |
| 29 | +import p1_common_neon |
| 30 | + |
| 31 | + |
| 32 | +def get_p1b1_parser(): |
| 33 | + |
| 34 | + # Construct neon arg parser. It generates a large set of options by default |
| 35 | + parser = NeonArgparser(__doc__) |
| 36 | + # Specify the default config_file |
| 37 | + parser.add_argument("--config_file", dest='config_file', type=str, |
| 38 | + default=os.path.join(p1b1.file_path, 'p1b1_default_model.txt'), |
| 39 | + help="specify model configuration file") |
| 40 | + |
| 41 | + # Parse other options that are not included on neon arg parser |
| 42 | + parser = p1_common.get_p1_common_parser(parser) |
| 43 | + |
| 44 | + |
| 45 | + return parser |
| 46 | + |
| 47 | + |
| 48 | +def main(): |
| 49 | + # Get command-line parameters |
| 50 | + parser = get_p1b1_parser() |
| 51 | + args = parser.parse_args() |
| 52 | + #print('Args:', args) |
| 53 | + # Get parameters from configuration file |
| 54 | + fileParameters = p1b1.read_config_file(args.config_file) |
| 55 | + #print ('Params:', fileParameters) |
| 56 | + |
| 57 | + # Correct for arguments set by default by neon parser |
| 58 | + # (i.e. instead of taking the neon parser default value fall back to the config file, |
| 59 | + # if effectively the command-line was used, then use the command-line value) |
| 60 | + # This applies to conflictive parameters: batch_size, epochs and rng_seed |
| 61 | + if not any("--batch_size" in ag or "-z" in ag for ag in sys.argv): |
| 62 | + args.batch_size = fileParameters['batch_size'] |
| 63 | + if not any("--epochs" in ag or "-e" in ag for ag in sys.argv): |
| 64 | + args.epochs = fileParameters['epochs'] |
| 65 | + if not any("--rng_seed" in ag or "-r" in ag for ag in sys.argv): |
| 66 | + args.rng_seed = fileParameters['rng_seed'] |
| 67 | + |
| 68 | + # Consolidate parameter set. Command-line parameters overwrite file configuration |
| 69 | + gParameters = p1_common.args_overwrite_config(args, fileParameters) |
| 70 | + print ('Params:', gParameters) |
| 71 | + |
| 72 | + # Determine verbosity level |
| 73 | + loggingLevel = logging.DEBUG if args.verbose else logging.INFO |
| 74 | + logging.basicConfig(level=loggingLevel, format='') |
| 75 | + # Construct extension to save model |
| 76 | + ext = p1b1.extension_from_parameters(gParameters, '.neon') |
| 77 | + |
| 78 | + # Get default parameters for initialization and optimizer functions |
| 79 | + kerasDefaults = p1_common.keras_default_config() |
| 80 | + seed = gParameters['rng_seed'] |
| 81 | + |
| 82 | + # Load dataset |
| 83 | + X_train, X_val, X_test = p1b1.load_data(gParameters, seed) |
| 84 | + |
| 85 | + print ("Shape X_train: ", X_train.shape) |
| 86 | + print ("Shape X_val: ", X_val.shape) |
| 87 | + print ("Shape X_test: ", X_test.shape) |
| 88 | + |
| 89 | + print ("Range X_train --> Min: ", np.min(X_train), ", max: ", np.max(X_train)) |
| 90 | + print ("Range X_val --> Min: ", np.min(X_val), ", max: ", np.max(X_val)) |
| 91 | + print ("Range X_test --> Min: ", np.min(X_test), ", max: ", np.max(X_test)) |
| 92 | + |
| 93 | + input_dim = X_train.shape[1] |
| 94 | + output_dim = input_dim |
| 95 | + |
| 96 | + # Re-generate the backend after consolidating parsing and file config |
| 97 | + gen_backend(backend=args.backend, |
| 98 | + rng_seed=seed, |
| 99 | + device_id=args.device_id, |
| 100 | + batch_size=gParameters['batch_size'], |
| 101 | + datatype=gParameters['datatype'], |
| 102 | + max_devices=args.max_devices, |
| 103 | + compat_mode=args.compat_mode) |
| 104 | + |
| 105 | + # Set input and target to X_train |
| 106 | + train = ArrayIterator(X_train) |
| 107 | + val = ArrayIterator(X_val) |
| 108 | + test = ArrayIterator(X_test) |
| 109 | + |
| 110 | + # Initialize weights and learning rule |
| 111 | + initializer_weights = p1_common_neon.build_initializer(gParameters['initialization'], kerasDefaults) |
| 112 | + initializer_bias = p1_common_neon.build_initializer('constant', kerasDefaults, 0.) |
| 113 | + |
| 114 | + activation = p1_common_neon.get_function(gParameters['activation'])() |
| 115 | + |
| 116 | + # Define Autoencoder architecture |
| 117 | + layers = [] |
| 118 | + reshape = None |
| 119 | + |
| 120 | + # Autoencoder |
| 121 | + layers_params = gParameters['dense'] |
| 122 | + |
| 123 | + if layers_params != None: |
| 124 | + if type(layers_params) != list: |
| 125 | + layers_params = list(layers_params) |
| 126 | + # Encoder Part |
| 127 | + for i,l in enumerate(layers_params): |
| 128 | + layers.append(Affine(nout=l, init=initializer_weights, bias=initializer_bias, activation=activation)) |
| 129 | + # Decoder Part |
| 130 | + for i,l in reversed( list(enumerate(layers_params)) ): |
| 131 | + if i < len(layers)-1: |
| 132 | + layers.append(Affine(nout=l, init=initializer_weights, bias=initializer_bias, activation=activation)) |
| 133 | + |
| 134 | + layers.append(Affine(nout=output_dim, init=initializer_weights, bias=initializer_bias, activation=activation)) |
| 135 | + |
| 136 | + # Build Autoencoder model |
| 137 | + ae = Model(layers=layers) |
| 138 | + |
| 139 | + # Define cost and optimizer |
| 140 | + cost = GeneralizedCost(p1_common_neon.get_function(gParameters['loss'])()) |
| 141 | + optimizer = p1_common_neon.build_optimizer(gParameters['optimizer'], |
| 142 | + gParameters['learning_rate'], |
| 143 | + kerasDefaults) |
| 144 | + |
| 145 | + callbacks = Callbacks(ae, eval_set=val, eval_freq = 1) |
| 146 | + |
| 147 | + # Seed random generator for training |
| 148 | + np.random.seed(seed) |
| 149 | + |
| 150 | + |
| 151 | + ae.fit(train, optimizer=optimizer, num_epochs=gParameters['epochs'], cost=cost, callbacks=callbacks) |
| 152 | + |
| 153 | + # model save |
| 154 | + #save_fname = "model_ae_W" + ext |
| 155 | + #ae.save_params(save_fname) |
| 156 | + |
| 157 | + # Compute errors |
| 158 | + X_pred = ae.get_outputs(test) |
| 159 | + scores = p1b1.evaluate_autoencoder(X_pred, X_test) |
| 160 | + print('Evaluation on test data:', scores) |
| 161 | + |
| 162 | + diff = X_pred - X_test |
| 163 | + # Plot histogram of errors comparing input and output of autoencoder |
| 164 | + plt.hist(diff.ravel(), bins='auto') |
| 165 | + plt.title("Histogram of Errors with 'auto' bins") |
| 166 | + plt.savefig('histogram_neon.png') |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == '__main__': |
| 171 | + main() |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
0 commit comments