コード例 #1
0
ファイル: train.py プロジェクト: dudany/deep_music_gen
def train(args):
    # First we extract the Data and vectorize it
    data_loc = get_path_to_loc(args.data_path)
    checkpoint_path = get_path_to_loc(args.checkpoint_prefix)

    list_test = run_data_extraction(data_loc)
    notes2idx, idx2note = get_notes_mapping_dict(list_test)
    notes_vec = vectorize_notes_by_mapping(list_test, notes2idx)

    # Now we set the model and the optimizer
    model = build_model(len(notes_vec), args.embedding_dim, args.rnn_units,
                        args.batch_size)
    optimizer = set_optimizer(args.optimizer, args.learning_rate)

    # train the model
    history = []
    # plotter = PeriodicPlotter(sec=2, xlabel='Iterations', ylabel='Loss') todo
    if hasattr(tqdm, '_instances'):
        tqdm._instances.clear()  # clear if it exists

    for iteration in tqdm(range(args.training_iterations)):

        # Grab a batch and propagate it through the network
        x_batch, y_batch = get_batch(notes_vec, args.seq_length,
                                     args.batch_size)
        loss = train_step(x_batch, y_batch, model, optimizer)

        # Update the progress bar
        history.append(loss.numpy().mean())
        # plotter.plot(history) Todo: put back

        # Update the model with the changed weights!
        if iteration % 100 == 0:
            model.save_weights(checkpoint_path)

    # Save the trained model and the weights
    model.save_weights(args.checkpoint_prefix)
class_weight = utils.class_weight(training_set[-1])
train_batch_fetcher = iterator_dr_640_segmentation_as_input.TrainBatchFetcher(
    training_set, batch_size, segmentation_home, class_weight)
val_batch_fetcher = iterator_dr_640_segmentation_as_input.ValidationBatchFetcher(
    validation_set, batch_size, segmentation_home)

# create networks
if FLAGS.load_model_dir:
    network_file = utils.all_files_under(FLAGS.load_model_dir,
                                         extension=".json")
    weight_file = utils.all_files_under(FLAGS.load_model_dir, extension=".h5")
    assert len(network_file) == 1 and len(weight_file) == 1
    with open(network_file[0], 'r') as f:
        network = model_from_json(f.read())
    network.load_weights(weight_file[0])
    network = model.set_optimizer(network)
else:
    network = model.dr_network_segmentation_as_input()
network.summary()
with open(os.path.join(model_out_dir, "network.json"), 'w') as f:
    f.write(network.to_json())

# start training
scheduler = utils.Scheduler(schedules)
for epoch in range(n_epochs):
    # update step sizes, learning rates
    scheduler.update_steps(epoch)
    K.set_value(network.optimizer.lr, scheduler.get_lr())

    # train on the training set
    start_time = time.time()