def train_network(parameters, data, epochs=1000, batch_size=32, loss='mse', verbose=False, seed=None, use_min_perf=False, callbacks=[], model_path='', model_storage=''): assert (isinstance(parameters, dict)), 'Parameters should be a dictionary.' clear_session() set_random_seed(seed) set_numpy_seed(seed) t_start = datetime.datetime.now() input_eular, input_crp, data = get_inputs(data, rnn_type=parameters['rnn_type']) [[x1t, x1v], [x2t, x2v], [yt, yv]] = data model = build_model(input_eular=input_eular, input_crp=input_crp, parameters=parameters, seed=seed) if verbose: model.summary() optimizer = get_optimizer(parameters['optimizer'], parameters['learning_rate']) model.compile(optimizer=optimizer, loss=loss) hist = model.fit(x=[x1t, x2t], y=yt, batch_size=batch_size, epochs=epochs, verbose=verbose, callbacks=callbacks, validation_data=([x1v, x2v], yv)) min_perf = min(hist.history['val_loss']) last_perf = hist.history['val_loss'][-1] time = datetime.datetime.now() - t_start time = time.seconds + time.microseconds / 1e6 return last_perf, min_perf, time
cbs = Tr.get_callbacks(plat=True, es=True) head = ['iteration', 'seed'] head += pg.get_head() head += ['last_perf', 'min_perf', 'time'] print(head) log = Bu.CSVWriter(filename, head=head) model_path = 'current_weights.h5' while True: seed = randint(0, 2**32-1) set_random_seed(seed) set_numpy_seed(seed) for i_param, param in enumerate(parameters): last_perfs = 0 min_perfs = 0 time = 0 for i_cv, cv in enumerate(cvs): if i_param + i_cv == 0: model_storage = 'save' else: model_storage = 'load' last_perf, min_perf, dt = Tr.train_network(param, cv, seed=seed, callbacks=cbs, verbose=False, model_path=model_path, model_storage=model_storage) last_perfs += last_perf min_perfs += min_perf time += dt