def fit_test(): m = Sequential() m.add(Dense(5, input_shape=(10, ))) n = 1024 _x = np.random.random((n, 10)) _y = np.random.random((n, 5)) epochs = 10 m.compile("adam", "mean_squared_error") if int(keras.__version__.split(".")[0]) == 2: m.fit(_x, _y, epochs=epochs, verbose=0, callbacks=[TQDMCallback()]) else: m.fit(_x, _y, nb_epoch=epochs, verbose=0, callbacks=[TQDMCallback()])
def fit(self, subpolicies, X, y): which = np.random.randint(len(subpolicies), size=len(X)) for i, subpolicy in enumerate(subpolicies): X[which == i] = subpolicy(X[which == i]) callback = TQDMCallback(leave_inner=False, leave_outer=False) callback.on_train_batch_begin = callback.on_batch_begin callback.on_train_batch_end = callback.on_batch_end self.model.fit(X, y, CHILD_BATCH_SIZE, CHILD_EPOCHS, verbose=0, callbacks=[callback]) return self
def train_model(model, space='K', n=1): print(model.summary(line_length=150)) run_id = f'kikinet_sep_{space}{n}_af{AF}_{int(time.time())}' chkpt_path = f'checkpoints/{run_id}' + '-{epoch:02d}.hdf5' print(run_id) chkpt_cback = ModelCheckpoint(chkpt_path, period=n_epochs // 2) log_dir = op.join('logs', run_id) tboard_cback = TensorBoard( profile_batch=0, log_dir=log_dir, histogram_freq=0, write_graph=True, write_images=False, ) lrate_cback = LearningRateScheduler(learning_rate_from_epoch) tqdm_cb = TQDMCallback(metric_format="{name}: {value:e}") tqdm_cb.on_train_batch_begin = tqdm_cb.on_batch_begin tqdm_cb.on_train_batch_end = tqdm_cb.on_batch_end if space == 'K': train_gen = train_gen_k val_gen = val_gen_k elif space == 'I': if n == 2: train_gen = train_gen_last val_gen = val_gen_last elif n == 1: train_gen = train_gen_i val_gen = val_gen_i model.fit_generator( train_gen, steps_per_epoch=n_volumes_train, epochs=n_epochs, validation_data=val_gen, validation_steps=1, verbose=0, callbacks=[ tqdm_cb, tboard_cback, chkpt_cback, lrate_cback, ], # max_queue_size=35, use_multiprocessing=True, workers=35, shuffle=True, ) return model
def train(self, train_x, train_y, batch_size, nepochs): """ Trains the model. Arguments. - train_x. Array or list containing the SMILES representation of the molecules. - train_y. The real values of the desired properties. - batch_size. The size of the batch. - nepochs. The maximum number of epochs. Returns. A string containing the development of the training program. """ input_x = self.computeFingerprints(train_x) callbacks = [EarlyStopping(monitor='val_loss', min_delta=0.01, patience=10, verbose=0, mode='auto'), TQDMCallback()] history = self.nn.fit(input_x, train_y, shuffle=True, epochs=nepochs, batch_size=batch_size, validation_split=0.1, verbose=2, callbacks=callbacks) return history
def fit_model(self, epochs=1, batch_size=32, verbose=True): # todo: Quitting with Ctrl-C causes CUDA to get stuck and leaves last program in gpumem. # todo: attach callbacks and configs to model class, not here # TF needs to exit cleanly epochs = int( epochs ) # make sure this is an int, since it may be fed in as string arg print('Fitting {} epochs, batch_size={}'.format(epochs, batch_size)) filepath = self.modelfile modelname = os.path.splitext(os.path.basename(self.modelfile))[0] checkpointer = ModelCheckpoint(monitor='val_acc', filepath=filepath, verbose=1, save_best_only=True) csvlogger = CSVLogger( 'logs/' + modelname + '.csv', append=True) # todo point this to proper location tensorboard = TensorBoard() progbar = TQDMCallback( ) # is actually interfering with displaying val_acc, so resorting to default progbar callbacks = [checkpointer, tensorboard, csvlogger] inputs_train, queries_train, answers_train = self.vectorizer.vectorize_all( 'train') inputs_test, queries_test, answers_test = self.vectorizer.vectorize_all( 'test') dmn.model.fit([inputs_train, queries_train], answers_train, batch_size=batch_size, epochs=epochs, validation_data=([inputs_test, queries_test], answers_test), verbose=1, callbacks=callbacks)
def train(model, A, labels, features, with_progress=True): """Train `model` using adjacency matrix `A` and `labels` as input. `with_progress` controls whether or not to show a TQDM progress bar. Note that the fullbatcher takes care of normalising and centring the input labels, so `labels` can be the verbatim values from the STBM scenario. """ # Show a progressbar if asked to. callbacks = [] if with_progress: callbacks.append(TQDMCallback(show_inner=False, leave_outer=False)) # Ignore warnings here: TensorFlow always complains that we convert a sparse matrix to a dense matrix, # which might use memory. I know, it's an open issue on GitHub, but it's not a problem for now. with warnings.catch_warnings(): warnings.simplefilter("ignore") return model.fit_fullbatches( # kwargs sent to the fullbatch generator. See `nw2vec.batching.fullbatches()` to see # what happens to these. batcher_kws={'adj': A, 'features': features, 'target_func': make_target_func(labels)}, # Self-explanatory. epochs=n_epochs, verbose=0, callbacks=callbacks )
def replay_all(self, batch_size): x = [] y = [] # minibatch = [x for x in self.memory if x[1] == 0][-batch_size:] + [x for x in self.memory if x[1] == 1][-batch_size:] + [x for x in self.memory if x[2] <= -1][-batch_size:] + random.sample(self.memory, batch_size) # minibatch = random.sample(minibatch, batch_size) minibatch = random.sample(self.memory, batch_size) for state, action, reward, next_state, done in tqdm( minibatch, desc='Building training set'): target = reward if not done: target = (reward + self.gamma * np.amax(self.predict(next_state))) target_f = self.predict(state) target_f[action] = target x += [state] y += [[target_f]] return self.model.fit( np.array(x).reshape(batch_size, self.input_memory_size, self.state_size), np.array(y).reshape(batch_size, self.action_size), epochs=64, batch_size=16, shuffle=True, verbose=0, callbacks=[ TQDMCallback(metric_format="{name}: {value:e}"), EarlyStopping(monitor='loss', min_delta=0, patience=10, verbose=0) ])
def train(self, epochs=500, trainX=None, trainY=None, valX=None, valY=None): try: tqdm._instances.clear() except: pass if trainX is None or trainY is None or valX is None or valY is None: trainX, trainY, valX, valY = self.trainX, self.trainY, self.valX, self.valY plot_losses = PlotLosses() plot_losses.task = self.task history = self.model.fit( trainX, trainY, epochs=epochs, batch_size=self.batch_size, validation_data=(valX, valY), shuffle=False, verbose=0, callbacks=[ TQDMCallback(leave_inner=False, show_inner=False), plot_losses, k.callbacks.ModelCheckpoint(self.task + '_Model_0.hdf5', monitor='val_loss', verbose=0, save_best_only=True) ]) self.histories.append(history) return history
def train(self, x, target, batch_size=500, epochs=10000, verbose=0): self.model.fit(x, target, batch_size=batch_size, epochs=epochs, verbose=verbose, callbacks=[TQDMCallback() ]) # ,callbacks=[TQDMCallback()]
def make_steps(imageset, mappings, model, execution, train, steps, ampl): """ Perform training epochs @param step Number of epochs to perform @param ampl the K, the randomized component of the score matrix. """ # shuffle the training pictures random.shuffle(train) # Compute the score matrix by scoring every pictures from the training set against every other picture O(n^2). trainImages = utils.hashes2images(mappings.h2p, train) features = model.branch.predict_generator(FeatureGen(imageset, trainImages, verbose=1), max_queue_size=12, workers=6, verbose=0) score = model.head.predict_generator(ScoreGen(features, verbose=1), max_queue_size=12, workers=6, verbose=0) execution.score = score_reshape(score, features) # Train the model for 'steps' epochs history = model.siamese.fit_generator(TrainingData( imageset, mappings, train, execution.score + ampl * np.random.random_sample(size=execution.score.shape), steps=steps, batch_size=32), initial_epoch=execution.steps, epochs=execution.steps + steps, max_queue_size=12, workers=6, verbose=0, callbacks=[ TQDMCallback( leave_inner=True, metric_format='{value:0.3f}') ]).history execution.steps += steps print("STEPS: ", execution.steps) # Collect history data history['epochs'] = execution.steps history['ms'] = np.mean(execution.score) history['lr'] = get_lr(model.siamese) print(history['epochs'], history['lr'], history['ms']) execution.histories.append(history)
def train_model(self, model, train_X: np.array, train_y: np.array, batch_size: int): earlyStop = EarlyStopping( monitor="val_loss", verbose=0, mode='min', patience=self.gs_data['model']['training']['patience'], restore_best_weights=True) if self.gs_data['model']['training']['early_stop'] == 1: r = model.fit(train_X, train_y, epochs=self.gs_data['model']['training']['n_epochs'], verbose=0, shuffle=True, validation_split=self.gs_data['model']['training'] ['validation_perc'], batch_size=batch_size, callbacks=[ earlyStop, TQDMCallback(leave_inner=False, leave_outer=True) ]) else: r = model.fit( train_X, train_y, epochs=self.gs_data['model']['training']['n_epochs'], verbose=0, shuffle=True, batch_size=batch_size, callbacks=[TQDMCallback(leave_inner=False, leave_outer=True)]) es_epochs = len(r.history['loss']) train_loss = r.history['loss'][-1] validation_loss = r.history['val_loss'][-1] return { 'epochs': es_epochs, 'train_loss': train_loss, 'validation_loss': validation_loss }
def fit(self, train_x: Union[Dict, List, np.ndarray], train_y: Union[Dict, List, np.ndarray], test_x: Union[Dict, List, np.ndarray] = None, test_y: Union[Dict, List, np.ndarray] = None) -> pd.DataFrame: """Fit the model using given training parameters. Parameters -------------------------- train: Tuple[Union[Dict, List, np.ndarray]], Either a tuple of list, np.ndarrays or dictionaries, containing the training data. test: Tuple[Union[Dict, List, np.ndarray]] = None Either a tuple of list, np.ndarrays or dictionaries, containing the validation data data. These data are optional, but they are required if the given monitor metric starts with "val_" Raises -------------------------- ValueError, If no test data are given but the monitor metric starts with the word "val_", meaning it has to be computed on the test data. Returns --------------------------- The training history as a pandas dataframe. """ test = None if all(d is not None for d in (test_x, test_y)): test = (test_x, test_y) if test is None and self._monitor.startswith("val_"): raise ValueError( "No test set was given, " "but a validation metric was required for the early stopping.") return pd.DataFrame( self._model.fit( train_x, train_y, epochs=self._max_epochs, batch_size=self._batch_size, validation_data=test, verbose=False, shuffle=True, callbacks=[ EarlyStopping(self._monitor, patience=self._patience), # We show the correct kind of callback depending if this # is running in a CLI or jupyter notebook-like environment. TQDMNotebookCallback() if is_notebook() else TQDMCallback() ]).history)
def trainModel(model, X_train, Y_train, X_test, Y_test, nepochs, nbatch): if nbatch == 0: nbatch = X_train.shape[0] model.fit(X_train, Y_train, epochs=nepochs, batch_size=nbatch, verbose=0, callbacks=[TQDMCallback()]) score = model.evaluate(X_test, Y_test) return model, score
def make_steps(step, ampl): """ Perform training epochs @param step Number of epochs to perform @param ampl the K, the randomized component of the score matrix. """ global w2ts, t2i, steps, features, score, histories # shuffle the training pictures random.shuffle(train) # Map whale id to the list of associated training picture hash value w2ts = {} for w, hs in w2hs.items(): for h in hs: if h in train_set: if w not in w2ts: w2ts[w] = [] if h not in w2ts[w]: w2ts[w].append(h) for w, ts in w2ts.items(): w2ts[w] = np.array(ts) # Map training picture hash value to index in 'train' array t2i = {} for i, t in enumerate(train): t2i[t] = i # Compute the match score for each picture pair features, score = compute_score() # Train the model for 'step' epochs history = model.fit_generator( TrainingData(score + ampl * np.random.random_sample(size=score.shape), steps=step, batch_size=32), initial_epoch=steps, epochs=steps + step, max_queue_size=12, workers=6, verbose=0, callbacks=[ TQDMCallback(leave_inner=True, metric_format='{value:0.3f}') ]).history steps += step # Collect history data history['epochs'] = steps history['ms'] = np.mean(score) history['lr'] = get_lr(model) print(history['epochs'], history['lr'], history['ms']) histories.append(history)
def fit(self, epochs, batch_size): progress = TQDMCallback(leave_outer=True, leave_inner=True) setattr(progress, 'on_train_batch_begin', lambda x, y: None) setattr(progress, 'on_train_batch_end', lambda x, y: None) plotWhileTraining = PlotWhileTraining(self.plot_every_n, self.hsi.size, self.n_end, self.hsi, self.hsi.GT, self.is_GT, True) hist = self.model.fit(self.hsi.data, self.hsi.data, epochs=epochs, batch_size=batch_size, verbose=0, callbacks=[progress, plotWhileTraining], shuffle=True) return hist
def fit(self, X, y=None, **fit_params): Xt, fit_params = self._fit(X, y, **fit_params) if self._final_estimator is not None: if isinstance(self._final_estimator, NeuralNetwork): return self._final_estimator.fit( Xt, y, **fit_params, validation_split=0.25, callbacks=[ TQDMCallback(leave_inner=False, show_inner=False) ] if False else [], verbose=0) else: self._final_estimator.fit(Xt, y, **fit_params) return self
def train_model(model, save_path, tensors, targets, epochs=5, batch_size=20, resume=False): print("summarizing model...") model.summary() if resume: print("loading saved weights...") model.load_weights(save_path) start = time() model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) stop = time() compilation_time = round(stop - start, 0) print('model compiled in %d seconds.\n' % compilation_time) print("unpacking tensors...") train_tensors, train_targets = tensors['train'], targets['train'] valid_tensors, valid_targets = tensors['valid'], targets['valid'] print("tensors unpacked!") checkpointer = ModelCheckpoint(filepath=save_path, verbose=1, save_best_only=True) start = time() model.fit(train_tensors, train_targets, validation_data=(valid_tensors, valid_targets), epochs=epochs, batch_size=batch_size, callbacks=[checkpointer, TQDMCallback()], verbose=0) stop = time() fit_time = round(stop - start, 0) print('model fitted in 5 epochs over %d seconds.\n' % fit_time)
def train(self, targets): if self.model is None: self.new_model() self.compile_model() moves = [[targets[x][i][0] for i in range(len(targets[0]))] for x in range(len(targets))] boards = moves_to_boards(moves) nninputs = [ NNInput(board, self.args['state_history']).as_input() for board in boards ] input_games = { 'p1_pieces': np.asarray([game['p1_pieces'] for game in nninputs]), 'p2_pieces': np.asarray([game['p2_pieces'] for game in nninputs]), 'helpers': np.asarray([game['helpers'] for game in nninputs]), 'legal_moves': np.asarray([game['legal_moves'] for game in nninputs]) } target_outputs = { 'pi_output': np.asarray([ targets[x][i][1] for i in range(len(targets[0])) for x in range(len(targets)) ]), 'v_output': np.asarray([ targets[x][i][2] for i in range(len(targets[0])) for x in range(len(targets)) ]) } self.model.fit(x=input_games, y=target_outputs, batch_size=self.args['batch_size'], epochs=self.args['epochs'], validation_split=self.args['validation_split'], verbose=self.args['verbose'], callbacks=[TQDMCallback()])
def replay_all(self, batch_size): x = [] y = [] minibatch = random.sample(self.memory, int(len(self.memory) / 100)) #+ [self.memory[i] for i in range(-int(len(self.memory) / 10), 0, 1) if self.memory[i][1] == 0] for state, action, reward, next_state, done in tqdm(minibatch, desc='Building training set'): target = reward if not done: target = (reward + self.gamma * np.amax(self.predict(next_state))) target_f = self.predict(state) target_f[0][action] = target x += [state] y += [target_f] self.model.fit( np.array(x), np.array(y), epochs=100, batch_size=batch_size, verbose=0, callbacks=[TQDMCallback(metric_format="{name}: {value:e}"), EarlyStopping(monitor='loss', min_delta=0, patience=10, verbose=0)] ) self.resetENV()
def continue_training(self, epochs=100, last_epoch=None, trainX=None, trainY=None, valX=None, valY=None): try: tqdm._instances.clear() except: pass last_epoch = self.histories[-1].history['epoch'][ -1] if last_epoch is None else last_epoch if trainX is None or trainY is None or valX is None or valY is None: trainX, trainY, valX, valY = self.trainX, self.trainY, self.valX, self.valY plot_losses = PlotLosses() plot_losses.task = self.task + str(last_epoch) history = self.model.fit( trainX, trainY, epochs=last_epoch + epochs, batch_size=self.batch_size, validation_data=(valX, valY), shuffle=False, verbose=0, callbacks=[ TQDMCallback(leave_inner=False, show_inner=False), plot_losses, k.callbacks.ModelCheckpoint(self.task + '_Model_%i.hdf5' % last_epoch, monitor='val_loss', verbose=0, save_best_only=True) ], initial_epoch=last_epoch) self.histories.append(history) return history
def train(model, input_data, label_data): learning_rate = 0.00001 batch_size = 200 nb_epoch = 100 X_train, X_test, Y_train, Y_test = train_test_split(input_data, label_data, test_size=0.2, shuffle=False) checkpoint_dir = "../checkpoints/dncnn_lfw_res/" if not os.path.exists(checkpoint_dir): os.mkdir(checkpoint_dir) checkpointer = ModelCheckpoint(filepath=os.path.join( checkpoint_dir, "checkpoint-epoch_{epoch:02d}_val_loss_{val_loss:.4f}.hdf5"), save_best_only=False, period=1) lr_scheduler = LearningRateScheduler(lr_schedule) model.compile(loss='mean_squared_error', optimizer=optimizers.Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999), metrics=['accuracy']) model.fit(x=X_train, y=Y_train, batch_size=batch_size, epochs=nb_epoch, verbose=1, shuffle=True, validation_data=(X_test, Y_test), callbacks=[ checkpointer, TQDMCallback(leave_inner=True, leave_outer=True, metric_format="{name}: {value:0.6f}") ])
def run_model(name, img_size, num_classes, results, curve_values, i, X_train, y_train, X_test, y_test, train_form, test_form): strategy = tf.distribute.MirroredStrategy() gpus = strategy.num_replicas_in_sync print("Starting ", name, ' k=', 1, ' devices=', gpus, sep='') with strategy.scope(): model = names_funcs[name](*img_size, num_classes) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) history = model.fit(X_train, y_train, epochs=100, verbose=2, batch_size=64 * gpus, validation_data=(X_test, y_test), callbacks=[TQDMCallback()]) print("Evaluating") predict_train = model.predict(X_train) np.save(train_form % (name, i), predict_train) predict_test = model.predict(X_test) np.save(test_form % (name, i), predict_test) _, test_acc = model.evaluate(X_test, y_test, verbose=1) print(f'{name} fold {i}:', test_acc) results[name][i] = test_acc curve_values[name][i] = { k: np.array(v).tolist() for k, v in history.history.items() } with open(RESULTS_FILE, 'w') as fp: dump(results, fp) with open(CURVE_FILE, 'w') as fp: dump(curve_values, fp) with open(MODEL_FORM % (name, i), 'w') as fp: fp.write(model.to_json()) model.save_weights(WEIGTHS_FORM % (name, i))
# Load the model model = ResNet() # Loading of checkpoint if args.checkpoint: model.load(args.checkpoint) else: print('no checkpoint file') # Fit model model.fit( train_generator, steps_per_epoch=5000, # validation_data=val_generator, # validation_steps=1000, epochs=2000, verbose=0, callbacks=[ TensorBoard(log_dir=os.path.join(args.log_path, args.name + '_phase1'), write_graph=False), ModelCheckpoint( os.path.join(args.log_path, args.name + '_phase1', 'weights.{epoch:003d}-{loss:.2f}.h5'), # monitor='val_loss', # save_best_only=True, save_weights_only=True), LambdaCallback(on_epoch_end=lambda epoch, logs: plot_callback( model, args.test_path)), TQDMCallback() ])
def fit_trainer(epochs=1, lr=1e-3): trainer_net.optimizer.lr=lr trainer_net.fit_generator(content_data, content_data.batches_per_epoch, epochs=epochs, verbose=0, callbacks=[TQDMCallback(), SaveWeightsCallback()])
n_epochs = 300 run_id = f'cascadenet_af{AF}_{int(time.time())}' chkpt_path = f'checkpoints/{run_id}' + '-{epoch:02d}.hdf5' print(run_id) chkpt_cback = ModelCheckpoint(chkpt_path, period=100, save_weights_only=True) log_dir = op.join('logs', run_id) tboard_cback = TensorBoard( profile_batch=0, log_dir=log_dir, histogram_freq=0, write_graph=True, write_images=False, ) tqdm_cb = TQDMCallback(metric_format="{name}: {value:e}") tqdm_cb.on_train_batch_begin = tqdm_cb.on_batch_begin tqdm_cb.on_train_batch_end = tqdm_cb.on_batch_end model = cascade_net(lr=1e-3, **run_params) print(model.summary(line_length=150)) model.fit_generator( train_gen, steps_per_epoch=n_volumes_train, epochs=n_epochs, validation_data=val_gen, validation_steps=1, verbose=0, callbacks=[ tqdm_cb,
def __init__(self, time_series, look_back, look_forward, term_name, stateful=True, random_seed=None, verbose=False): self.__verbose = verbose self.__term_name = term_name if random_seed is not None: np.random.seed(random_seed) self.__source_time_series = np.reshape(time_series.astype(dtype=np.float), newshape=(-1, 1)) self.__look_back = look_back self.__look_forward = look_forward time_series_delta = time_series[1:] - time_series[:-1] self.__source_time_series_delta = np.reshape(time_series_delta.astype(dtype=np.float), newshape=(-1, 1)) self.__scaler = MinMaxScaler(feature_range=(-1, 1)) data_set = self.__scaler.fit_transform(self.__source_time_series_delta) data_set_x, data_set_y = self.__create_sliding_window_dataset(data_set, look_back, look_forward) self.__test_train_split = 0.67 train_size = int(len(data_set_x) * self.__test_train_split) train_x = data_set_x[0:train_size, :] train_y = data_set_y[0:train_size, :] test_x = data_set_x[train_size:, :] test_y = data_set_y[train_size:, :] self.__hidden_neurons = 100 # 20 # 100 if stateful: self.__model_name = 'encode-decode-stateful' self.__num_epochs = 200 batch_size = 1 self.__model = Sequential() self.__model.add( LSTM(batch_input_shape=(batch_size, None, 1), units=self.__hidden_neurons, return_sequences=False, stateful=True)) self.__model.add(RepeatVector(look_forward)) self.__model.add(LSTM(units=self.__hidden_neurons, return_sequences=True, stateful=True)) self.__model.add(TimeDistributed(Dense(1))) self.__model.add(Activation('linear')) if self.__verbose: self.__model.summary(print_fn=print) self.__model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy']) epoch_iterator = range(self.__num_epochs) if self.__verbose: epoch_iterator = tqdm(epoch_iterator, unit='epoch') for _ in epoch_iterator: history = self.__model.fit(x=train_x, y=train_y, validation_data=(test_x, test_y), epochs=1, batch_size=batch_size, verbose=0) val_loss = history.history['val_loss'][0] loss = history.history['loss'][0] if self.__verbose: epoch_iterator.set_description(f"val loss={val_loss:0.3f} loss={loss:0.3f}") self.__model.reset_states() else: self.__model_name = 'encode-decode-stateless' self.__model = Sequential() self.__model.add(LSTM(input_shape=(None, 1), units=self.__hidden_neurons, return_sequences=False)) self.__model.add(RepeatVector(look_forward)) self.__model.add(LSTM(units=self.__hidden_neurons, return_sequences=True)) self.__model.add(TimeDistributed(Dense(1))) self.__model.add(Activation('linear')) if self.__verbose: self.__model.summary(print_fn=print) self.__model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy']) early_stopping = EarlyStopping(monitor='loss', min_delta=0.000001, patience=50) # this big patience is important callbacks = [early_stopping] if self.__verbose: callbacks.append(TQDMCallback()) history = self.__model.fit(x=train_x, y=train_y, validation_data=(test_x, test_y), epochs=10000, callbacks=callbacks, verbose=0) self.__num_epochs = len(history.epoch)
train_path = data_paths[0:275] val_path = data_paths[275:] print('train_images:', train_path) print('val_path:', val_path) train_generator = GenerateData(train_path, input_shape, output_channels, batch_size) val_generator = GenerateData(val_path, input_shape, output_channels, batch_size) nb_epoch = 10 model = build_model(input_shape=input_shape, output_channels=3) check = ModelCheckpoint( "weights/weights.epoch:{epoch:02d}-loss:{loss:.5f}-dice:{dice_coefficient:.5f}.hdf5", monitor='dice_coefficient', verbose=1, save_best_only=True, save_weights_only=True, mode='max') model.fit_generator( generator=train_generator, validation_data=val_generator, use_multiprocessing=False, workers=1, verbose=0, epochs=nb_epoch, steps_per_epoch=len(train_generator), callbacks=[check, TQDMCallback()], )
def train_neural_network(x_train, y_train, x_val, y_val, configs, partial_model_architecture, batch_size=32, nb_epoch=5, normalize=True, checkpoint_dir=None, neural_network_name='my_neural_network', training_log_file='training.log', early_stopping=False): """Train a neural network to classify crystal structures represented as two-dimensional diffraction fingerprints. This model was introduced in [1]_. x_train: np.array, [batch, width, height, channels] .. [1] A. Ziletti, D. Kumar, M. Scheffler, and L. M. Ghiringhelli, “Insightful classification of crystal structures using deep learning”, Nature Communications, vol. 9, pp. 2775 (2018) .. codeauthor:: Angelo Ziletti <*****@*****.**> """ if checkpoint_dir is None: checkpoint_dir = configs['io']['results_folder'] filename_no_ext = os.path.abspath( os.path.normpath(os.path.join(checkpoint_dir, neural_network_name))) training_log_file_path = os.path.abspath( os.path.normpath(os.path.join(checkpoint_dir, training_log_file))) # reshape to follow the image conventions # - TensorFlow backend: [batch, width, height, channels] # - Theano backend: [batch, channels, width, height] x_train = reshape_images_to_theano(x_train) x_val = reshape_images_to_theano(x_val) assert x_train.shape[1] == x_val.shape[1] assert x_train.shape[2] == x_val.shape[2] assert x_train.shape[3] == x_val.shape[3] img_channels = x_train.shape[1] img_width = x_train.shape[2] img_height = x_train.shape[3] logger.info('Loading datasets.') logger.debug('x_train shape: {0}'.format(x_train.shape)) logger.debug('y_train shape: {0}'.format(y_train.shape)) logger.debug('x_val shape: {0}'.format(x_val.shape)) logger.debug('y_val shape: {0}'.format(y_val.shape)) logger.debug('Training samples: {0}'.format(x_train.shape[0])) logger.debug('Validation samples: {0}'.format(x_val.shape[0])) logger.debug("Img channels: {}".format(x_train.shape[1])) logger.debug("Img width: {}".format(x_train.shape[2])) logger.debug("Img height: {}".format(x_train.shape[3])) x_train = x_train.astype('float32') x_val = x_val.astype('float32') # normalize each image separately if normalize: for idx in range(x_train.shape[0]): x_train[idx, :, :, :] = (x_train[idx, :, :, :] - np.amin( x_train[idx, :, :, :])) / (np.amax(x_train[idx, :, :, :]) - np.amin(x_train[idx, :, :, :])) for idx in range(x_val.shape[0]): x_val[idx, :, :, :] = (x_val[idx, :, :, :] - np.amin( x_val[idx, :, :, :])) / (np.amax(x_val[idx, :, :, :]) - np.amin(x_val[idx, :, :, :])) # check if the image is already normalized logger.info( 'Maximum value in x_train for the 1st image (to check normalization): {0}' .format(np.amax(x_train[0, :, :, :]))) logger.info( 'Maximum value in x_val for the 1st image (to check normalization): {0}' .format(np.amax(x_val[0, :, :, :]))) # convert class vectors to binary class matrices nb_classes = len(set(y_train)) nb_classes_val = len(set(y_val)) if nb_classes_val != nb_classes: raise ValueError( "Different number of unique classes in training and validation set: {} vs {}." "Training set unique classes: {}" "Validation set unique classes: {}".format(nb_classes, nb_classes_val, set(y_train), set(y_val))) y_train = np_utils.to_categorical(y_train, nb_classes) y_val = np_utils.to_categorical(y_val, nb_classes) logger.info('Loading and formatting of data completed.') # return the Keras model model = partial_model_architecture(n_rows=img_width, n_columns=img_height, img_channels=img_channels, nb_classes=nb_classes) model.summary() # serialize model to JSON model_json = model.to_json() with open(filename_no_ext + ".json", "w") as json_file: json_file.write(model_json) callbacks = [] csv_logger = CSVLogger(training_log_file_path, separator=',', append=False) save_model_per_epoch = ModelCheckpoint(filename_no_ext + ".h5", monitor='val_acc', verbose=1, save_best_only=True, mode='max', period=1) callbacks.append(csv_logger) callbacks.append(save_model_per_epoch) # if you are running on Notebook if configs['runtime']['isBeaker']: callbacks.append( TQDMNotebookCallback(leave_inner=True, leave_outer=True)) else: callbacks.append(TQDMCallback(leave_inner=True, leave_outer=True)) if early_stopping: EarlyStopping(monitor='val_loss', min_delta=0.001, patience=1, verbose=0, mode='auto') adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, decay=0.0) model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy']) model.fit(x_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, validation_data=(x_val, y_val), shuffle=True, verbose=0, callbacks=callbacks) # serialize weights to HDF5 model.save(filename_no_ext + ".h5") logger.info("Model saved to disk.") logger.info("Filename: {0}".format(filename_no_ext)) del model
model.add(Dense(1, activation='sigmoid')) # compile the network model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # print the model summary model.summary() # define the validation split VALIDATION_SPLIT = 0.2 indices = np.arange(review_pad.shape[0]) np.random.shuffle(indices) review_pad = review_pad[indices] sentiment = sentiment[indices] num_validation_samples = int(VALIDATION_SPLIT * review_pad.shape[0]) X_train_pad = review_pad[:-num_validation_samples] y_train = sentiment[:-num_validation_samples] X_test_pad = review_pad[-num_validation_samples:] y_test = sentiment[-num_validation_samples:] # train the network model.fit(X_train_pad, y_train, batch_size=128, epochs=3, validation_data=(X_test_pad, y_test), verbose=2, callbacks=[TQDMCallback()]) model.save('modelGRU.h5')
""" This example trains a model on the MNIST data set using keras-tqdm progress bars. """ from mnist_model import mnist_model from keras_tqdm import TQDMCallback if __name__ == "__main__": mnist_model(verbose=0, callbacks=[TQDMCallback()])