예제 #1
0
def iXc3_nll(n_in, size='large', use_dropout=False):
    k0, k1, k2 = dict(
        small=(10, 20, 40),
        med=(12, 24, 48),
        large=(16, 32, 64),
        xlarge=(20, 40, 80),
        xxlarge=(24, 48, 96))[size]

    n0, n1, n2 = {
        1: (1, 1, 1),
        4: (3, 2, 1),
        8: (5, 3, 2),
        10: (3, 3, 1),
        20: (5, 5, 1)}[n_in]

    p0, p1, p2 = {
        1: (1, 1, 1),
        4: (1, 1, 1),
        8: (1, 1, 1),
        10: (2, 2, 1),
        12: (2, 2, 1),
        20: (2, 2, 2)}[n_in]

    input_data = optimus.Input(
        name='cqt',
        shape=(None, 1, n_in, 252))

    indexes = []
    for name in 'EADGBe':
        indexes.append(optimus.Input(
            name='{0}_index'.format(name),
            shape=(None,),
            dtype='int32'))

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

    inputs = [input_data, learning_rate] + indexes

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

    # 1.2 Create Nodes
    layer0 = optimus.Conv3D(
        name='layer0',
        input_shape=input_data.shape,
        weight_shape=(k0, None, n0, 13),
        pool_shape=(p0, 3),
        act_type='relu')

    layer1 = optimus.Conv3D(
        name='layer1',
        input_shape=layer0.output.shape,
        weight_shape=(k1, None, n1, 37),
        pool_shape=(p1, 1),
        act_type='relu')

    layer2 = optimus.Conv3D(
        name='layer2',
        input_shape=layer1.output.shape,
        weight_shape=(k2, None, n2, 33),
        pool_shape=(p2, 1),
        act_type='relu')

    trainer_edges = []
    if use_dropout:
        layer0.enable_dropout()
        layer1.enable_dropout()
        layer2.enable_dropout()
        inputs += [dropout]
        trainer_edges += [(dropout, layer0.dropout),
                          (dropout, layer1.dropout),
                          (dropout, layer2.dropout)]

    predictors = []
    softmaxes = []
    for name in 'EADGBe':
        predictors.append(optimus.Affine(
            name='{0}_predictor'.format(name),
            input_shape=layer2.output.shape,
            output_shape=(None, NUM_FRETS),
            act_type='linear'))
        softmaxes.append(optimus.Softmax('{0}_softmax'.format(name)))

    stack = optimus.Stack('stacker', num_inputs=6, axes=(1, 0, 2))

    param_nodes = [layer0, layer1, layer2] + predictors
    misc_nodes = [stack] + softmaxes

    # 1.1 Create Loss
    likelihoods = []
    logs = []
    neg_ones = []
    for name in 'EADGBe':
        likelihoods.append(
            optimus.SelectIndex(name='{0}_likelihood'.format(name)))

        logs.append(optimus.Log(name='{0}_log'.format(name)))
        neg_ones.append(optimus.Multiply(name='{0}_gain'.format(name),
                                         weight_shape=None))
        neg_ones[-1].weight.value = -1.0

    loss_sum = optimus.Add(name='loss_sum', num_inputs=6)
    ave_loss = optimus.Mean(name='ave_loss')
    loss_nodes = likelihoods + logs + neg_ones + [loss_sum, ave_loss]
    total_loss = optimus.Output(name='total_loss')

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

    # 2. Define Edges
    base_edges = [
        (input_data, layer0.input),
        (layer0.output, layer1.input),
        (layer1.output, layer2.input)]

    for p, smax in zip(predictors, softmaxes):
        base_edges += [
            (layer2.output, p.input),
            (p.output, smax.input),
        ]
    base_edges += [
        (softmaxes[0].output, stack.input_0),
        (softmaxes[1].output, stack.input_1),
        (softmaxes[2].output, stack.input_2),
        (softmaxes[3].output, stack.input_3),
        (softmaxes[4].output, stack.input_4),
        (softmaxes[5].output, stack.input_5),
        (stack.output, fretboard)
    ]

    for n, name in enumerate('EADGBe'):
        trainer_edges += [
            (softmaxes[n].output, likelihoods[n].input),
            (indexes[n], likelihoods[n].index),
            (likelihoods[n].output, logs[n].input),
            (logs[n].output, neg_ones[n].input)
        ]
    trainer_edges += [
        (neg_ones[0].output, loss_sum.input_0),
        (neg_ones[1].output, loss_sum.input_1),
        (neg_ones[2].output, loss_sum.input_2),
        (neg_ones[3].output, loss_sum.input_3),
        (neg_ones[4].output, loss_sum.input_4),
        (neg_ones[5].output, loss_sum.input_5),
        (loss_sum.output, ave_loss.input),
        (ave_loss.output, total_loss)
    ]

    update_manager = optimus.ConnectionManager(
        map(lambda n: (learning_rate, n.weights), param_nodes) +
        map(lambda n: (learning_rate, n.bias), param_nodes))

    classifier_init(param_nodes)

    trainer = optimus.Graph(
        name=GRAPH_NAME,
        inputs=inputs,
        nodes=param_nodes + misc_nodes + loss_nodes,
        connections=optimus.ConnectionManager(
            base_edges + trainer_edges).connections,
        outputs=[total_loss, fretboard],
        loss=total_loss,
        updates=update_manager.connections,
        verbose=True)

    if use_dropout:
        layer0.disable_dropout()
        layer1.disable_dropout()
        layer2.disable_dropout()

    predictor = optimus.Graph(
        name=GRAPH_NAME,
        inputs=[input_data],
        nodes=param_nodes + misc_nodes,
        connections=optimus.ConnectionManager(base_edges).connections,
        outputs=[fretboard],
        verbose=True)

    return trainer, predictor
예제 #2
0
def iXc3_rbf_weighted(n_in, size='large', use_dropout=False):
    k0, k1, k2 = dict(
        small=(10, 20, 40),
        med=(12, 24, 48),
        large=(16, 32, 64),
        xlarge=(20, 40, 80),
        xxlarge=(24, 48, 96))[size]

    n0, n1, n2 = {
        1: (1, 1, 1),
        4: (3, 2, 1),
        8: (5, 3, 2),
        10: (3, 3, 1),
        20: (5, 5, 1)}[n_in]

    p0, p1, p2 = {
        1: (1, 1, 1),
        4: (1, 1, 1),
        8: (1, 1, 1),
        10: (2, 2, 1),
        12: (2, 2, 1),
        20: (2, 2, 2)}[n_in]

    input_data = optimus.Input(
        name='data',
        shape=(None, 1, n_in, 252))

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

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

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

    inputs = [input_data, chord_idx, class_weight, learning_rate]

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

    # 1.2 Create Nodes
    layer0 = optimus.Conv3D(
        name='layer0',
        input_shape=input_data.shape,
        weight_shape=(k0, None, n0, 13),
        pool_shape=(p0, 3),
        act_type='relu')

    layer1 = optimus.Conv3D(
        name='layer1',
        input_shape=layer0.output.shape,
        weight_shape=(k1, None, n1, 37),
        pool_shape=(p1, 1),
        act_type='relu')

    layer2 = optimus.Conv3D(
        name='layer2',
        input_shape=layer1.output.shape,
        weight_shape=(k2, None, n2, 33),
        pool_shape=(p2, 1),
        act_type='relu')

    trainer_edges = []
    if use_dropout:
        layer0.enable_dropout()
        layer1.enable_dropout()
        layer2.enable_dropout()
        inputs += [dropout]
        trainer_edges += [(dropout, layer0.dropout),
                          (dropout, layer1.dropout),
                          (dropout, layer2.dropout)]

    predictors = []
    softmaxes = []
    for name in 'EADGBe':
        predictors.append(optimus.Affine(
            name='{0}_predictor'.format(name),
            input_shape=layer2.output.shape,
            output_shape=(None, NUM_FRETS),
            act_type='linear'))
        softmaxes.append(optimus.Softmax('{0}_softmax'.format(name)))

    stack = optimus.Stack('stacker', num_inputs=6, axes=(1, 0, 2))
    param_nodes = [layer0, layer1, layer2] + predictors
    misc_nodes = [stack] + softmaxes

    # 1.1 Create Loss
    rbf = optimus.RadialBasis(
        name='rbf',
        input_shape=(None, 6, NUM_FRETS),
        output_shape=(None, 157))
    inverter = optimus.Multiply(name='inverter', weight_shape=None)
    inverter.weight.value = -1.0
    class_softmax = optimus.Softmax("class_softmax")

    energies = optimus.SelectIndex(name='energies')
    importance = optimus.Product(name='importance_weighting')
    ave_loss = optimus.Mean(name='ave_loss')
    total_loss = optimus.Output(name='total_loss')

    # 2. Define Edges
    base_edges = [
        (input_data, layer0.input),
        (layer0.output, layer1.input),
        (layer1.output, layer2.input)]

    for p, smax in zip(predictors, softmaxes):
        base_edges += [(layer2.output, p.input),
                       (p.output, smax.input)]

    base_edges += [(softmaxes[0].output, stack.input_0),
                   (softmaxes[1].output, stack.input_1),
                   (softmaxes[2].output, stack.input_2),
                   (softmaxes[3].output, stack.input_3),
                   (softmaxes[4].output, stack.input_4),
                   (softmaxes[5].output, stack.input_5)]

    trainer_edges += base_edges + [
        (stack.output, rbf.input),
        (rbf.output, energies.input),
        (rbf.output, inverter.input),
        (chord_idx, energies.index),
        (energies.output, importance.input_a),
        (class_weight, importance.input_b),
        (importance.output, ave_loss.input),
        (ave_loss.output, total_loss)]

    update_manager = optimus.ConnectionManager(
        map(lambda n: (learning_rate, n.weights), param_nodes) +
        map(lambda n: (learning_rate, n.bias), param_nodes))

    classifier_init(param_nodes)

    train_nodes = [rbf, inverter, energies, importance, ave_loss]

    trainer = optimus.Graph(
        name=GRAPH_NAME,
        inputs=inputs,
        nodes=param_nodes + misc_nodes + train_nodes,
        connections=optimus.ConnectionManager(trainer_edges).connections,
        outputs=[total_loss],
        loss=total_loss,
        updates=update_manager.connections,
        verbose=True)

    if use_dropout:
        layer0.disable_dropout()
        layer1.disable_dropout()
        layer2.disable_dropout()

    fretboard = optimus.Output(name='fretboard')
    fret_predictor = optimus.Graph(
        name=GRAPH_NAME,
        inputs=[input_data],
        nodes=param_nodes + misc_nodes,
        connections=optimus.ConnectionManager(
            base_edges + [(stack.output, fretboard)]).connections,
        outputs=[fretboard],
        verbose=True)

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

    classifier_edges = base_edges + [
        (stack.output, rbf.input),
        (rbf.output, inverter.input),
        (inverter.output, class_softmax.input),
        (class_softmax.output, posterior)]

    classifier = optimus.Graph(
        name=GRAPH_NAME,
        inputs=[input_data],
        nodes=param_nodes + misc_nodes + [rbf, inverter, class_softmax],
        connections=optimus.ConnectionManager(classifier_edges).connections,
        outputs=[posterior],
        verbose=True)

    return trainer, fret_predictor, classifier
예제 #3
0
def i8c3_pwmse(size='large'):
    k0, k1, k2 = dict(
        small=(8, 16, 20),
        med=(12, 24, 32),
        large=(16, 32, 48))[size]

    input_data = optimus.Input(
        name='cqt',
        shape=(None, 1, 8, 252))

    target = optimus.Input(
        name='target',
        shape=(None, 1))

    chord_idx = optimus.Input(
        name='chord_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=(k0, None, 3, 13),
        pool_shape=(1, 3),
        act_type='relu')

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

    layer2 = optimus.Conv3D(
        name='layer2',
        input_shape=layer1.output.shape,
        weight_shape=(k2, None, 3, 33),
        act_type='relu')

    chord_classifier = optimus.Conv3D(
        name='chord_classifier',
        input_shape=layer2.output.shape,
        weight_shape=(13, None, 2, 1),
        act_type='sigmoid')

    flatten = optimus.Flatten('flatten', 2)

    null_classifier = optimus.Affine(
        name='null_classifier',
        input_shape=layer2.output.shape,
        output_shape=(None, 1),
        act_type='sigmoid')

    cat = optimus.Concatenate('concatenate', num_inputs=2, axis=1)

    param_nodes = [layer0, layer1, layer2, chord_classifier, null_classifier]
    misc_nodes = [flatten, cat]

    # 1.1 Create Loss
    likelihoods = optimus.SelectIndex(name='likelihoods')
    dimshuffle = optimus.Dimshuffle('dimshuffle', (0, 'x'))
    squared_error = optimus.SquaredEuclidean(name='squared_error')
    loss = optimus.Mean(name='mean_squared_error')

    loss_nodes = [likelihoods, dimshuffle, squared_error, loss]

    # 2. Define Edges
    base_edges = [
        (input_data, layer0.input),
        (layer0.output, layer1.input),
        (layer1.output, layer2.input),
        (layer2.output, chord_classifier.input),
        (layer2.output, null_classifier.input),
        (chord_classifier.output, flatten.input),
        (flatten.output, cat.input_0),
        (null_classifier.output, cat.input_1)]

    trainer_edges = optimus.ConnectionManager(
        base_edges + [
            (cat.output, likelihoods.input),
            (chord_idx, likelihoods.index),
            (likelihoods.output, dimshuffle.input),
            (dimshuffle.output, squared_error.input_a),
            (target, squared_error.input_b),
            (squared_error.output, loss.input)])

    update_manager = optimus.ConnectionManager(
        map(lambda n: (learning_rate, n.weights), param_nodes) +
        map(lambda n: (learning_rate, n.bias), param_nodes))

    trainer = optimus.Graph(
        name=GRAPH_NAME,
        inputs=[input_data, target, chord_idx, learning_rate],
        nodes=param_nodes + misc_nodes + loss_nodes,
        connections=trainer_edges.connections,
        outputs=[loss.output],
        loss=loss.output,
        updates=update_manager.connections,
        verbose=True)

    classifier_init(param_nodes)

    posterior = optimus.Output(name='posterior')
    predictor_edges = optimus.ConnectionManager(
        base_edges + [(cat.output, posterior)])

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

    return trainer, predictor
예제 #4
0
def i8x1a3T_nll2(size, use_dropout=False):
    k0, k1, k2 = dict(
        large=(2048, 2048, 40),)[size]

    input_data = optimus.Input(
        name='data',
        shape=(None, 8, 1, 252))

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

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

    inputs = [input_data, chord_idx, learning_rate]

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

    # 1.2 Create Nodes
    layer0 = optimus.Affine(
        name='layer0',
        input_shape=input_data.shape,
        output_shape=(None, k0),
        act_type='relu')

    layer1 = optimus.Affine(
        name='layer1',
        input_shape=layer0.output.shape,
        output_shape=(None, k1),
        act_type='relu')

    layer2 = optimus.Affine(
        name='layer2',
        input_shape=layer1.output.shape,
        output_shape=(None, k2, 1, 12),
        act_type='relu')

    dropout_edges = []
    if use_dropout:
        layer0.enable_dropout()
        layer1.enable_dropout()
        layer2.enable_dropout()
        inputs += [dropout]
        dropout_edges += [(dropout, layer0.dropout),
                          (dropout, layer1.dropout),
                          (dropout, layer2.dropout)]

    chord_classifier = optimus.Conv3D(
        name='chord_classifier',
        input_shape=layer2.output.shape,
        weight_shape=(13, None, 1, 1),
        act_type='linear')

    flatten = optimus.Flatten('flatten', 2)

    null_classifier = optimus.Affine(
        name='null_classifier',
        input_shape=layer0.output.shape,
        output_shape=(None, 1),
        act_type='linear')

    cat = optimus.Concatenate('concatenate', num_inputs=2, axis=1)
    softmax = optimus.Softmax('softmax')
    prior = optimus.Multiply("prior", weight_shape=(1, 157), broadcast=[0])
    prior.weight.value = np.ones([1, 157])

    param_nodes = [layer0, layer1, layer2, null_classifier, chord_classifier]
    misc_nodes = [flatten, cat, softmax, prior]

    # 1.1 Create Loss
    likelihoods = optimus.SelectIndex(name='likelihoods')

    log = optimus.Log(name='log')
    neg = optimus.Multiply(name='gain', weight_shape=None)
    neg.weight.value = -1.0

    loss = optimus.Mean(name='negative_log_likelihood')
    loss_nodes = [likelihoods, log, neg, loss]
    total_loss = optimus.Output(name='total_loss')

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

    # 2. Define Edges
    base_edges = [
        (input_data, layer0.input),
        (layer0.output, layer1.input),
        (layer1.output, layer2.input),
        (layer2.output, chord_classifier.input),
        (layer0.output, null_classifier.input),
        (chord_classifier.output, flatten.input),
        (flatten.output, cat.input_0),
        (null_classifier.output, cat.input_1),
        (cat.output, softmax.input),
        (softmax.output, prior.input),
        (prior.output, posterior)]

    trainer_edges = optimus.ConnectionManager(
        base_edges + dropout_edges + [
            (softmax.output, likelihoods.input),
            (chord_idx, likelihoods.index),
            (likelihoods.output, log.input),
            (log.output, neg.input),
            (neg.output, loss.input),
            (loss.output, total_loss)])

    update_manager = optimus.ConnectionManager(
        map(lambda n: (learning_rate, n.weights), param_nodes) +
        map(lambda n: (learning_rate, n.bias), param_nodes))

    classifier_init(param_nodes)

    trainer = optimus.Graph(
        name=GRAPH_NAME,
        inputs=inputs,
        nodes=param_nodes + misc_nodes + loss_nodes,
        connections=trainer_edges.connections,
        outputs=[total_loss, posterior],
        loss=total_loss,
        updates=update_manager.connections,
        verbose=True)

    if use_dropout:
        layer0.disable_dropout()
        layer1.disable_dropout()
        layer2.disable_dropout()

    predictor = optimus.Graph(
        name=GRAPH_NAME,
        inputs=[input_data],
        nodes=param_nodes + misc_nodes,
        connections=optimus.ConnectionManager(base_edges).connections,
        outputs=[posterior],
        verbose=True)

    return trainer, predictor
예제 #5
0
def i8c4b10_nll_dropout(size='large'):
    k0, k1, k2 = dict(
        large=(24, 48, 64))[size]

    input_data = optimus.Input(
        name='cqt',
        shape=(None, 1, 8, 252))

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

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

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

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

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

    layer2 = optimus.Conv3D(
        name='layer2',
        input_shape=layer1.output.shape,
        weight_shape=(k2, None, 3, 33),
        act_type='relu')

    layer3 = optimus.Conv3D(
        name='layer3',
        input_shape=layer2.output.shape,
        weight_shape=(10, None, 2, 1),
        act_type='relu')

    chord_classifier = optimus.Conv3D(
        name='chord_classifier',
        input_shape=layer3.output.shape,
        weight_shape=(13, None, 1, 1),
        act_type='linear')

    flatten = optimus.Flatten('flatten', 2)

    null_classifier = optimus.Affine(
        name='null_classifier',
        input_shape=layer3.output.shape,
        output_shape=(None, 1),
        act_type='linear')

    cat = optimus.Concatenate('concatenate', num_inputs=2, axis=1)
    softmax = optimus.Softmax('softmax')

    param_nodes = [layer0, layer1, layer2, layer3,
                   null_classifier, chord_classifier]
    misc_nodes = [flatten, cat, softmax]

    # 1.1 Create Loss
    likelihoods = optimus.SelectIndex(name='likelihoods')

    log = optimus.Log(name='log')
    neg = optimus.Gain(name='gain')
    neg.weight.value = -1.0

    loss = optimus.Mean(name='negative_log_likelihood')
    loss_nodes = [likelihoods, log, neg, loss]
    total_loss = optimus.Output(name='total_loss')

    layer0.enable_dropout()
    layer1.enable_dropout()
    layer2.enable_dropout()

    # 2. Define Edges
    base_edges = [
        (input_data, layer0.input),
        (layer0.output, layer1.input),
        (layer1.output, layer2.input),
        (layer2.output, layer3.input),
        (layer3.output, chord_classifier.input),
        (layer3.output, null_classifier.input),
        (chord_classifier.output, flatten.input),
        (flatten.output, cat.input_0),
        (null_classifier.output, cat.input_1),
        (cat.output, softmax.input)]

    trainer_edges = optimus.ConnectionManager(
        base_edges + [
            (dropout, layer0.dropout),
            (dropout, layer1.dropout),
            (dropout, layer2.dropout),
            (softmax.output, likelihoods.input),
            (chord_idx, likelihoods.index),
            (likelihoods.output, log.input),
            (log.output, neg.input),
            (neg.output, loss.input),
            (loss.output, total_loss)])

    update_manager = optimus.ConnectionManager(
        map(lambda n: (learning_rate, n.weights), param_nodes[:-1]) +
        map(lambda n: (learning_rate, n.bias), param_nodes[:-1]))

    trainer = optimus.Graph(
        name=GRAPH_NAME,
        inputs=[input_data, chord_idx, learning_rate, dropout],
        nodes=param_nodes + misc_nodes + loss_nodes,
        connections=trainer_edges.connections,
        outputs=[total_loss],
        loss=total_loss,
        updates=update_manager.connections,
        verbose=True)

    classifier_init(param_nodes[:-1])

    semitones = L.semitone_matrix(157)[:13, 2:]
    chord_classifier.weights.value = semitones.reshape(13, 10, 1, 1)

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

    predictor_edges = optimus.ConnectionManager(
        base_edges + [(softmax.output, posterior)])

    layer0.disable_dropout()
    layer1.disable_dropout()
    layer2.disable_dropout()

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

    return trainer, predictor
예제 #6
0
def wcqt_likelihood_wmoia(n_dim=VOCAB):
    input_data = optimus.Input(
        name='cqt',
        shape=(None, 6, TIME_DIM, 40))

    target = optimus.Input(
        name='target',
        shape=(None, 1))

    chord_idx = optimus.Input(
        name='chord_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=(32, None, 5, 5),
        pool_shape=(2, 3),
        act_type='relu')

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

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

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

    chord_estimator = optimus.Affine(
        name='chord_estimator',
        input_shape=layer3.output.shape,
        output_shape=(None, n_dim),
        act_type='sigmoid')

    param_nodes = [layer0, layer1, layer2, layer3, chord_estimator]

    # 1.1 Create Loss
    likelihoods = optimus.SelectIndex('select')
    dimshuffle = optimus.Dimshuffle('dimshuffle', (0, 'x'))
    error = optimus.SquaredEuclidean(name='squared_error')
    main_loss = optimus.Mean(name='mean_squared_error')
    loss_nodes1 = [likelihoods, dimshuffle, error, main_loss]

    negone = optimus.Gain(name='negate')
    negone.weight.value = -1.0
    summer = optimus.Add(name='moia_sum')
    flatten = optimus.Sum('flatten', axis=1)
    dimshuffle2 = optimus.Dimshuffle('dimshuffle2', (0, 'x'))
    margin = optimus.RectifiedLinear(name='margin')
    weight = optimus.Multiply(name="margin_weight")
    margin_loss = optimus.Mean(name='margin_loss', axis=None)

    loss_nodes2 = [negone, summer, margin, flatten,
                   dimshuffle2, margin_loss, weight]
    total_loss = optimus.Add("total_loss")

    # 2. Define Edges
    base_edges = [
        (input_data, layer0.input),
        (layer0.output, layer1.input),
        (layer1.output, layer2.input),
        (layer2.output, layer3.input),
        (layer3.output, chord_estimator.input)]

    trainer_edges = optimus.ConnectionManager(
        base_edges + [
            (chord_estimator.output, likelihoods.input),
            (chord_idx, likelihoods.index),
            (likelihoods.output, dimshuffle.input),
            (dimshuffle.output, error.input_a),
            (target, error.input_b),
            (error.output, main_loss.input),
            # Margin loss
            (dimshuffle.output, negone.input),
            (negone.output, summer.input_list),
            (chord_estimator.output, summer.input_list),
            (summer.output, margin.input),
            (margin.output, flatten.input),
            (flatten.output, dimshuffle2.input),
            (dimshuffle2.output, weight.input_a),
            (target, weight.input_b),
            (weight.output, margin_loss.input),
            (margin_loss.output, total_loss.input_list),
            (main_loss.output, total_loss.input_list)])

    update_manager = optimus.ConnectionManager(
        map(lambda n: (learning_rate, n.weights), param_nodes) +
        map(lambda n: (learning_rate, n.bias), param_nodes))

    all_nodes = param_nodes + loss_nodes1 + loss_nodes2 + [total_loss]
    trainer = optimus.Graph(
        name=GRAPH_NAME,
        inputs=[input_data, target, chord_idx, learning_rate],
        nodes=all_nodes,
        connections=trainer_edges.connections,
        outputs=[total_loss.output],
        loss=total_loss.output,
        updates=update_manager.connections,
        verbose=True)

    for n in param_nodes:
        for p in n.params.values():
            optimus.random_init(p)

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

    predictor_edges = optimus.ConnectionManager(
        base_edges + [(chord_estimator.output, posterior)])

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

    return trainer, predictor
예제 #7
0
def main(args):
    # 1.1 Create Inputs
    input_data = optimus.Input(
        name='cqt',
        shape=(None, OCTAVE_DIM, TIME_DIM, PITCH_DIM))

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

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

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

    # 1.2 Create Nodes
    layer0 = optimus.Conv3D(
        name='layer0',
        input_shape=input_data.shape,
        weight_shape=(32, None, 5, 5),
        pool_shape=(2, 3),
        act_type='relu')

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

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

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

    chord_estimator = optimus.Softmax(
        name='chord_estimator',
        input_shape=layer3.output.shape,
        output_shape=(None, VOCAB,),
        act_type='sigmoid')

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

    log = optimus.Log(name='log')
    neg = optimus.Gain(name='gain')
    neg.weight = np.array(-1)

    energy = optimus.SelectIndex(name='selector')

    loss = optimus.Mean(name='total_loss')

    # 1.1 Create Losses
    chord_margin = optimus.Margin(
        name="chord_margin",
        mode='max')

    # 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, chord_estimator.input),
        (chord_estimator.output, log.input),
        (log.output, neg.input),
        (neg.output, energy.input),
        (chord_idx, energy.index),
        (energy.output, loss.input)])

    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, chord_estimator.weights),
        (learning_rate, chord_estimator.bias)])

    trainer = optimus.Graph(
        name=GRAPH_NAME,
        inputs=[input_data, chord_idx, learning_rate, margin],
        nodes=all_nodes,
        connections=trainer_edges.connections,
        outputs=[optimus.Graph.TOTAL_LOSS],
        loss=[loss.output],
        updates=update_manager.connections)

    for n in all_nodes:
        optimus.random_init(n.weights)
        optimus.random_init(n.bias)

    if args.init_param_file:
        print "Loading parameters: %s" % args.init_param_file
        trainer.load_param_values(args.init_param_file)

    for n in all_nodes[-2:]:
        optimus.random_init(n.weights)
        optimus.random_init(n.bias)

    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, chord_classifier.input),
        (chord_classifier.output, posterior)])

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

    # 3. Create Data
    print "Loading %s" % args.training_file
    stash = biggie.Stash(args.training_file)
    stream = D.create_stash_stream(
        stash, TIME_DIM, pitch_shift=0, vocab_dim=VOCAB, pool_size=25)

    if args.secondary_source:
        stash2 = biggie.Stash(args.secondary_source)
        stream2 = D.create_uniform_chord_stream(
            stash2, TIME_DIM, pitch_shift=0, vocab_dim=VOCAB, working_size=5)
        stream = S.mux([stream, stream2], [0.5, 0.5])

    stream = S.minibatch(stream, batch_size=BATCH_SIZE)

    print "Starting '%s'" % args.trial_name
    driver = optimus.Driver(
        graph=trainer,
        name=args.trial_name,
        output_directory=args.model_directory)

    hyperparams = {learning_rate.name: LEARNING_RATE,
                   margin.name: MARGIN}

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

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