def optimize_cnn(hype_space): """Build a convolutional neural network and train it.""" try: model, model_name, result, _ = build_and_train(hype_space, save_best_weights=True, log_for_tensorboard=True) # Save training results to disks with unique filenames h.save_json_result(model_name, result) K.clear_session() del model return result except Exception as err: try: K.clear_session() except: pass err_str = str(err) print(err_str) traceback_str = str(traceback.format_exc()) print(traceback_str) return { 'status': STATUS_FAIL, 'err': err_str, 'traceback': traceback_str } print("\n\n")
def optimize_cnn(hype_space): """Build a convolutional neural network and train it.""" if not is_gpu_available(): tf.logging.warning('GPUs are not available') tf.logging.debug("Hyperspace: ", hype_space) tf.logging.debug("\n") try: model, model_name, result, _ = build_and_train( hype_space, log_for_tensorboard=True) tf.logging.info("Training ended with success:") tf.logging.info("Model name: %s", model_name) # Save training results to disks with unique filenames save_json_result(model_name, result) export_model(model_name) K.clear_session() del model tf.logging.info('before return result') return result except Exception as err: err_str = str(err) tf.logging.error(err_str) traceback_str = str(traceback.format_exc()) tf.logging.error(traceback_str) return { 'status': STATUS_FAIL, 'err': err_str, 'traceback': traceback_str }
def optimize_cnn(hype_space): """Build a convolutional neural network and train it.""" if not is_gpu_available(): tf.logging.warning('GPUs are not available') tf.logging.debug("Hyperspace: ", hype_space) tf.logging.debug("\n") try: model, model_name, result, _ = build_and_train(hype_space) tf.logging.info("Training ended with success:") tf.logging.info("Model name: ", model_name) # Save training results to disks with unique filenames # TODO do we need this? this save to json on disc not to mongo. Not sure if we want always save to disc save_json_result(model_name, result) K.clear_session() del model tf.logging.info('before return result') return result except Exception as err: try: K.clear_session() except: pass err_str = str(err) tf.logging.error(err_str) traceback_str = str(traceback.format_exc()) tf.logging.error(traceback_str) return { 'status': STATUS_FAIL, 'err': err_str, 'traceback': traceback_str }
def optimize_cnn(hype_space): """Build a convolutional neural network and train it.""" try: model, model_name, result, model_uuid = build_and_train( hype_space, save_best_weights=True) # Save training results to disks with unique filenames save_json_result(model_name, result) # Save .png plot of the model according to its hyper-parameters plot(result['space'], model_uuid) K.clear_session() del model return result except Exception as err: try: K.clear_session() except: pass err_str = str(err) print(err_str) traceback_str = str(traceback.format_exc()) print(traceback_str) print("\n\n") return { 'status': STATUS_FAIL, 'err': err_str, 'traceback': traceback_str }
from keras.layers.core import K import os __author__ = "Vooban Inc." __copyright__ = "Copyright 2017, Vooban Inc." __license__ = "MIT License" # See: https://github.com/Vooban/Hyperopt-Keras-CNN-CIFAR-100/blob/master/LICENSE" if __name__ == "__main__": """Retrain best model with TensorBoard. Also save best weights.""" space_best_model = load_best_hyperspace() print("Hyperspace:") print_json(space_best_model) model, model_name, results, log_path = build_and_train( space_best_model, save_best_weights=True, log_for_tensorboard=True) print("Model Name:", model_name) print("Note: results 'json' file not saved to 'results/' since it is now " "available in TensorBoard. See above console output for json-styled " "results.") print("Model summary:") model.summary() print("TensorBoard logs directory:", log_path) print("You may now want to run this command to launch TensorBoard:\n" "tensorboard --logdir={}".format(TENSORBOARD_DIR))