Esempio n. 1
0
def main(args):
    # 1.1 Create Inputs
    input_data = optimus.Input(name='cqt', shape=(None, 1, TIME_DIM, 252))

    fret_bitmap = optimus.Input(name='fret_bitmap', shape=(None, 6, FRET_DIM))

    learning_rate = optimus.Input(name='learning_rate', shape=None)

    # 1.2 Create Nodes
    layer0 = optimus.Conv3D(name='layer0',
                            input_shape=input_data.shape,
                            weight_shape=(12, 1, 3, 19),
                            pool_shape=(1, 3),
                            act_type='relu')

    layer1 = optimus.Conv3D(name='layer1',
                            input_shape=layer0.output.shape,
                            weight_shape=(16, None, 3, 15),
                            act_type='relu')

    layer2 = optimus.Conv3D(name='layer2',
                            input_shape=layer1.output.shape,
                            weight_shape=(20, None, 1, 15),
                            act_type='relu')

    layer3 = optimus.Affine(name='layer3',
                            input_shape=layer2.output.shape,
                            output_shape=(
                                None,
                                512,
                            ),
                            act_type='relu')

    fretboard = optimus.MultiSoftmax(name='fretboard',
                                     input_shape=layer3.output.shape,
                                     output_shape=(None, 6, FRET_DIM),
                                     act_type='linear')

    all_nodes = [layer0, layer1, layer2, layer3, fretboard]

    # 1.1 Create Losses
    mse = optimus.MeanSquaredError(name="mean_squared_error")

    # 2. Define Edges
    trainer_edges = optimus.ConnectionManager([
        (input_data, layer0.input), (layer0.output, layer1.input),
        (layer1.output, layer2.input), (layer2.output, layer3.input),
        (layer3.output, fretboard.input), (fretboard.output, mse.prediction),
        (fret_bitmap, mse.target)
    ])

    update_manager = optimus.ConnectionManager([
        (learning_rate, layer0.weights), (learning_rate, layer0.bias),
        (learning_rate, layer1.weights), (learning_rate, layer1.bias),
        (learning_rate, layer2.weights), (learning_rate, layer2.bias),
        (learning_rate, layer3.weights), (learning_rate, layer3.bias),
        (learning_rate, fretboard.weights), (learning_rate, fretboard.bias)
    ])

    trainer = optimus.Graph(name=GRAPH_NAME,
                            inputs=[input_data, fret_bitmap, learning_rate],
                            nodes=all_nodes,
                            connections=trainer_edges.connections,
                            outputs=[optimus.Graph.TOTAL_LOSS],
                            losses=[mse],
                            updates=update_manager.connections)

    optimus.random_init(fretboard.weights)

    validator = optimus.Graph(name=GRAPH_NAME,
                              inputs=[input_data, fret_bitmap],
                              nodes=all_nodes,
                              connections=trainer_edges.connections,
                              outputs=[optimus.Graph.TOTAL_LOSS],
                              losses=[mse])

    posterior = optimus.Output(name='posterior')

    predictor_edges = optimus.ConnectionManager([
        (input_data, layer0.input), (layer0.output, layer1.input),
        (layer1.output, layer2.input), (layer2.output, layer3.input),
        (layer3.output, fretboard.input), (fretboard.output, posterior)
    ])

    predictor = optimus.Graph(name=GRAPH_NAME,
                              inputs=[input_data],
                              nodes=all_nodes,
                              connections=predictor_edges.connections,
                              outputs=[posterior])

    # 3. Create Data
    source = optimus.Queue(optimus.File(args.training_file),
                           transformers=[
                               T.cqt_sample(input_data.shape[2]),
                               T.pitch_shift(MAX_FRETS, bins_per_pitch=3),
                               T.fret_indexes_to_bitmap(FRET_DIM)
                           ],
                           **SOURCE_ARGS)

    driver = optimus.Driver(graph=trainer,
                            name=args.trial_name,
                            output_directory=args.model_directory)

    hyperparams = {learning_rate.name: LEARNING_RATE}

    driver.fit(source, hyperparams=hyperparams, **DRIVER_ARGS)

    validator_file = path.join(driver.output_directory, args.validator_file)
    optimus.save(validator, def_file=validator_file)

    predictor_file = path.join(driver.output_directory, args.predictor_file)
    optimus.save(predictor, def_file=predictor_file)
Esempio n. 2
0
def main(args):
    # 1.1 Create Inputs
    input_data = optimus.Input(name='cqt', shape=(None, 1, TIME_DIM, 252))

    root_idx = optimus.Input(name='root_idx', shape=(None, ), dtype='int32')

    quality_idx = optimus.Input(name='quality_idx',
                                shape=(None, ),
                                dtype='int32')

    learning_rate = optimus.Input(name='learning_rate', shape=None)

    # 1.2 Create Nodes
    layer0 = optimus.Conv3D(name='layer0',
                            input_shape=input_data.shape,
                            weight_shape=(12, 1, 9, 19),
                            pool_shape=(1, 3),
                            act_type='relu')

    layer1 = optimus.Conv3D(name='layer1',
                            input_shape=layer0.output.shape,
                            weight_shape=(16, None, 7, 15),
                            act_type='relu')

    layer2 = optimus.Conv3D(name='layer2',
                            input_shape=layer1.output.shape,
                            weight_shape=(20, None, 6, 15),
                            act_type='relu')

    layer3 = optimus.Affine(name='layer3',
                            input_shape=layer2.output.shape,
                            output_shape=(
                                None,
                                512,
                            ),
                            act_type='relu')

    root_classifier = optimus.Softmax(name='root_classifier',
                                      input_shape=layer3.output.shape,
                                      n_out=12,
                                      act_type='linear')

    quality_classifier = optimus.MultiSoftmax(name='quality_classifier',
                                              input_shape=layer3.output.shape,
                                              output_shape=(None, 12, 13),
                                              act_type='linear')

    all_nodes = [
        layer0, layer1, layer2, layer3, root_classifier, quality_classifier
    ]

    # 1.1 Create Losses
    root_nll = optimus.NegativeLogLikelihood(name="root_nll")

    quality_nll = optimus.ConditionalNegativeLogLikelihood(name="quality_nll")

    # 2. Define Edges
    trainer_edges = optimus.ConnectionManager([
        (input_data, layer0.input), (layer0.output, layer1.input),
        (layer1.output, layer2.input), (layer2.output, layer3.input),
        (layer3.output, root_classifier.input),
        (layer3.output, quality_classifier.input),
        (root_classifier.output, root_nll.likelihood),
        (root_idx, root_nll.target_idx),
        (quality_classifier.output, quality_nll.likelihood),
        (quality_idx, quality_nll.conditional_idx),
        (root_idx, quality_nll.independent_idx)
    ])

    update_manager = optimus.ConnectionManager([
        (learning_rate, layer0.weights), (learning_rate, layer0.bias),
        (learning_rate, layer1.weights), (learning_rate, layer1.bias),
        (learning_rate, layer2.weights), (learning_rate, layer2.bias),
        (learning_rate, layer3.weights), (learning_rate, layer3.bias),
        (learning_rate, root_classifier.weights),
        (learning_rate, root_classifier.bias),
        (learning_rate, quality_classifier.weights),
        (learning_rate, quality_classifier.bias)
    ])

    print "Trainer"
    trainer = optimus.Graph(
        name=GRAPH_NAME,
        inputs=[input_data, root_idx, quality_idx, learning_rate],
        nodes=all_nodes,
        connections=trainer_edges.connections,
        outputs=[optimus.Graph.TOTAL_LOSS],
        losses=[root_nll, quality_nll],
        updates=update_manager.connections)

    optimus.random_init(root_classifier.weights)
    optimus.random_init(quality_classifier.weights)

    print "Validator"
    validator = optimus.Graph(name=GRAPH_NAME,
                              inputs=[input_data, root_idx, quality_idx],
                              nodes=all_nodes,
                              connections=trainer_edges.connections,
                              outputs=[optimus.Graph.TOTAL_LOSS],
                              losses=[root_nll, quality_nll])

    root_posterior = optimus.Output(name='root_posterior')

    quality_posterior = optimus.Output(name='quality_posterior')

    predictor_edges = optimus.ConnectionManager([
        (input_data, layer0.input), (layer0.output, layer1.input),
        (layer1.output, layer2.input), (layer2.output, layer3.input),
        (layer3.output, root_classifier.input),
        (layer3.output, quality_classifier.input),
        (quality_classifier.output, quality_posterior),
        (root_classifier.output, root_posterior)
    ])

    print "predictor"
    predictor = optimus.Graph(name=GRAPH_NAME,
                              inputs=[input_data],
                              nodes=all_nodes,
                              connections=predictor_edges.connections,
                              outputs=[root_posterior, quality_posterior])

    # 3. Create Data
    print "Opening data"
    stash = biggie.Stash(args.training_file)
    stream = S.minibatch(D.create_uniform_factored_stream(stash,
                                                          TIME_DIM,
                                                          vocab_dim=VOCAB),
                         batch_size=50)

    driver = optimus.Driver(graph=trainer,
                            name=args.trial_name,
                            output_directory=args.model_directory)

    hyperparams = {learning_rate.name: LEARNING_RATE}

    print "...aaand we're off!"
    driver.fit(stream, hyperparams=hyperparams, **DRIVER_ARGS)

    validator_file = path.join(driver.output_directory, args.validator_file)
    optimus.save(validator, def_file=validator_file)

    predictor_file = path.join(driver.output_directory, args.predictor_file)
    optimus.save(predictor, def_file=predictor_file)