sys.exit(0) if args.submit == 'nni': builder.generate_code() tunor = builder.find_role('hyper-tunor', unique=True) commands = [ 'nnictl', 'create', '--config', tunor.store_nni_config() ] builder.node.submit_job(commands) sys.exit(0) if args.nni_trial: import nni builder.stages[0].update_trial_id('trial-' + random_string(size=8)) param = nni.get_parameters() print(param) builder.update_config(param) result = builder.build_and_train() # nni.report_final_result(0 if result is None else result['loss']) sys.exit(0) # build if args.train: builder = ModelPipeline(config, from_fe=args.front_end) builder.build_and_train() builder.notify('Succeed', 'Trainig has been done') sys.exit(0) except Exception as e: builder.notify( 'Failed',
model.weights = PARAMS['knr_weights'] except Exception as exception: LOG.exception(exception) raise return model def run(X_train, X_test, y_train, y_test, PARAMS): '''Train model and predict result''' model.fit(X_train, y_train) predict_y = model.predict(X_test) score = r2_score(y_test, predict_y) LOG.debug('r2 score: %s' % score) nni.report_final_result(score) if __name__ == '__main__': X_train, X_test, y_train, y_test = load_data() try: # get parameters from tuner RECEIVED_PARAMS = nni.get_parameters() LOG.debug(RECEIVED_PARAMS) PARAMS = get_default_parameters() PARAMS.update(RECEIVED_PARAMS) LOG.debug(PARAMS) model = get_model(PARAMS) run(X_train, X_test, y_train, y_test, model) except Exception as exception: LOG.exception(exception) raise
save_path = None max_epoch = args.max_epoch cfg = GAGConfig() cfg.batch_size = args.batch_size cfg.learning_rate = float(args.learning_rate) cfg.dropout = args.dropout_rate cfg.rnn_units = args.rnn_units cfg.labelsmoothing = args.labelsmoothing cfg.num_heads = args.num_heads timer = Timer() qp_pairs, dev_qp_pairs = load_data() logger.debug('Init finish.') original_params = nni.get_parameters() ''' with open('data.json') as f: original_params = json.load(f) ''' try: graph = graph.graph_loads(original_params) except Exception: logger.debug('Can\'t load graph.') train_loss, best_acc = train_with_graph(graph, qp_pairs, dev_qp_pairs) logger.debug('Send best acc: %s', str(best_acc)) nni.report_final_result(best_acc) logger.debug('Send final result done') except: logger.exception('Catch exception in trial.py.')
def generate_default_params(): ''' Generate default parameters for mnist network. ''' params = { 'data_dir': '/tmp/tensorflow/mnist/input_data', 'dropout_rate': 0.5, 'channel_1_num': 32, 'channel_2_num': 64, 'conv_size': 5, 'pool_size': 2, 'hidden_size': 1024, 'learning_rate': 1e-4, 'batch_num': 200} return params if __name__ == '__main__': try: # get parameters form tuner RCV_PARAMS = nni.get_parameters() logger.debug(RCV_PARAMS) # run params = generate_default_params() params.update(RCV_PARAMS) main(params) except Exception as exception: logger.exception(exception) raise
import nni params = nni.get_parameters() print('params:', params) x = params['x'] nni.report_final_result(x)
print('Saving..') state = { 'net': net.state_dict(), 'acc': acc, 'epoch': epoch, } if not os.path.isdir('checkpoint'): os.mkdir('checkpoint') torch.save(state, './checkpoint/ckpt.t7') best_acc = acc return acc, best_acc if __name__ == '__main__': try: RCV_CONFIG = nni.get_parameters() #RCV_CONFIG = {'lr': 0.1, 'optimizer': 'Adam', 'model':'senet18'} _logger.debug(RCV_CONFIG) prepare(RCV_CONFIG) acc = 0.0 best_acc = 0.0 for epoch in range(start_epoch, start_epoch+200): train(epoch) acc, best_acc = test(epoch) nni.report_intermediate_result(acc) nni.report_final_result(best_acc) except Exception as exception: _logger.exception(exception) raise
return params def parse_init_json(data): params = {} for key in data: value = data[key] if value == 'Empty': params[key] = ['Empty'] else: params[key] = [value[0], value[1]['_value'], value[1]['_value']] return params if __name__ == '__main__': try: # get parameters form tuner data = nni.get_parameters() logger.debug(data) RCV_PARAMS = parse_init_json(data) logger.debug(RCV_PARAMS) params = generate_defualt_params() params.update(RCV_PARAMS) print(RCV_PARAMS) main(params) except Exception as exception: logger.exception(exception) raise