def train(data, model, num_frontends, resume_epoch, model_dir): cb = SaveModelsAndTerminateEarly() cb.set_params(model_dir, resume_epoch) X_train, y_train = data[0] X_val, y_val = data[1] if 1 < num_frontends: X_train = [X_train] * num_frontends X_val = [X_val] * num_frontends try: model.fit( X_train, y_train, validation_data=(X_val, y_val), nb_epoch=10000, batch_size=128, callbacks=[cb], show_accuracy=True) except EarlyTermination: pass
def train(data, model, num_frontends, resume_epoch, model_dir): cb = SaveModelsAndTerminateEarly() cb.set_params(model_dir, resume_epoch) X_train, y_train = data[0] X_val, y_val = data[1] if 1 < num_frontends: X_train = [X_train] * num_frontends X_val = [X_val] * num_frontends try: model.fit(X_train, y_train, validation_data=(X_val, y_val), nb_epoch=10000, batch_size=128, callbacks=[cb], show_accuracy=True) except EarlyTermination: pass
def train(pipeline_func_name, network_func_name, train_set, val_set, save_dir): # Init the data transformers. f = getattr(pipelines, pipeline_func_name) data_pipe, class_pipe, need_to_embed = f() data_pipe = TransformerPipeline(data_pipe) class_pipe = TransformerPipeline(class_pipe) # Transform the data. (X_train, y_train), (X_val, y_val) = \ fit_transform(train_set, val_set, data_pipe, class_pipe) # Build the model, now that we know its input/output dimensions. print 'X_train.shape:', X_train.shape print 'y_train.shape:', y_train.shape if need_to_embed: input_dim = max(map(max, X_train)) + 1 else: input_dim = X_train.shape[-1] output_dim = y_train.shape[-1] print 'Input dim:', input_dim print 'Output dim:', output_dim kmodel, nb_frontends = \ construct(network_func_name, input_dim, output_dim) # Save the model configuration. if os.path.exists(save_dir): shutil.rmtree(save_dir) os.makedirs(save_dir) d = { 'pipeline_func_name': pipeline_func_name, 'data_pipe': data_pipe, 'class_pipe': class_pipe, 'network_func_name': network_func_name, 'input_dim': input_dim, 'output_dim': output_dim, } fn = os.path.join(save_dir, 'model.pkl') cPickle.dump(d, open(fn, 'wb')) # Train the model, saving checkpoints. print 'Training...' if nb_frontends != 1: X_train = [X_train] * nb_frontends X_val = [X_val] * nb_frontends cb = SaveModelsAndTerminateEarly() cb.set_params(save_dir) try: kmodel.fit( X_train, y_train, validation_data=(X_val, y_val), nb_epoch=10000, batch_size=128, callbacks=[cb], show_accuracy=True, verbose=True) except EarlyTermination: logging.info('Terminated training early.') pass print 'Done training.' return Model(data_pipe, kmodel, class_pipe, nb_frontends)