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
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
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
def iXc3_fc_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='data', shape=(None, 1, n_in, 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.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') 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.Affine( name='chord_classifier', input_shape=layer2.output.shape, output_shape=(None, VOCAB), act_type='softmax') prior = optimus.Multiply("prior", weight_shape=(1, 157), broadcast=[0]) prior.weight.value = np.ones([1, 157]) param_nodes = [layer0, layer1, layer2, chord_classifier] misc_nodes = [prior] # 1.1 Create Loss nll = optimus.NegativeLogLikelihoodLoss(name='negative_log_likelihood') total_loss = optimus.Output(name='total_loss') # features = optimus.Output(name='features') 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), (chord_classifier.output, prior.input), (prior.output, posterior)] trainer_edges = optimus.ConnectionManager( base_edges + dropout_edges + [ (chord_classifier.output, nll.likelihoods), (chord_idx, nll.index), (nll.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 + [nll], 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
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
def test_pairwise(n_in): input_data = optimus.Input( name='cqt', shape=(None, n_in)) input_data_2 = optimus.Input( name='cqt_2', shape=(None, n_in)) score = optimus.Input( name='score', shape=(None,)) margin = optimus.Input( name='margin', shape=None) inputs = [input_data, input_data_2, score, margin] # 1.2 Create Loss # --------------- # same = y*D # diff = (1 - y) * hwr(margin - D) # total = ave(same_cost + diff_cost) distance = optimus.SquaredEuclidean(name='euclidean') cost_sim = optimus.Product(name="cost_sim") neg_distance = optimus.Multiply( name='neg_distance', weight_shape=None) neg_distance.weight.value = -1.0 margin_sum = optimus.Add(name="margin_sum", num_inputs=2) hwr = optimus.RectifiedLinear(name="hwr") pos_one = optimus.Constant( name='pos_one', shape=None) pos_one.data.value = 1.0 neg_score = optimus.Multiply( name='neg_score', weight_shape=None) neg_score.weight.value = -1.0 diff_selector = optimus.Add("diff_selector", num_inputs=2) cost_diff = optimus.Product(name='cost_diff') total_cost = optimus.Add('total_cost', num_inputs=2) loss = optimus.Mean(name='loss') loss_nodes = [distance, cost_sim, neg_distance, margin_sum, hwr, pos_one, neg_score, diff_selector, cost_diff, total_cost, loss] # Graph outputs total_loss = optimus.Output(name='total_loss') dist_sim = optimus.Output(name='dist_sim') dist_diff = optimus.Output(name='dist_diff') dist = optimus.Output(name='dist') # 2. Define Edges trainer_edges = optimus.ConnectionManager([ (input_data, distance.input_a), (input_data_2, distance.input_b), (distance.output, dist), # Sim terms (score, cost_sim.input_a), (distance.output, cost_sim.input_b), (cost_sim.output, dist_sim), (cost_sim.output, total_cost.input_0), # Diff terms (distance.output, neg_distance.input), (margin, margin_sum.input_0), (neg_distance.output, margin_sum.input_1), (margin_sum.output, hwr.input), (score, neg_score.input), (pos_one.output, diff_selector.input_0), (neg_score.output, diff_selector.input_1), (diff_selector.output, cost_diff.input_a), (hwr.output, cost_diff.input_b), (cost_diff.output, dist_diff), (cost_diff.output, total_cost.input_1), # Combined (total_cost.output, loss.input), (loss.output, total_loss)]) trainer = optimus.Graph( name=GRAPH_NAME, inputs=inputs, nodes=loss_nodes, connections=trainer_edges.connections, outputs=[total_loss, dist, dist_sim, dist_diff], verbose=True) return trainer
def iX_c3f2_oY(n_in, n_out, size='large'): # Kernel shapes k0, k1, k2, k3 = dict( small=(10, 20, 40, 96), med=(12, 24, 48, 128), large=(16, 32, 64, 192), xlarge=(20, 40, 80, 256), xxlarge=(24, 48, 96, 512))[size] # Input dimensions 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] # Pool shapes 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, 192)) input_data_2 = optimus.Input( name='cqt_2', shape=(None, 1, n_in, 192)) score = optimus.Input( name='score', shape=(None,)) learning_rate = optimus.Input( name='learning_rate', shape=None) sim_margin = optimus.Input( name='sim_margin', shape=None) diff_margin = optimus.Input( name='diff_margin', shape=None) inputs = [input_data, input_data_2, score, learning_rate, sim_margin, diff_margin] # 1.2 Create Nodes logscale = optimus.Log("logscale", 1.0) layer0 = optimus.Conv3D( name='layer0', input_shape=input_data.shape, weight_shape=(k0, None, n0, 13), pool_shape=(p0, 2), act_type='tanh') layer1 = optimus.Conv3D( name='layer1', input_shape=layer0.output.shape, weight_shape=(k1, None, n1, 11), pool_shape=(p1, 2), act_type='tanh') layer2 = optimus.Conv3D( name='layer2', input_shape=layer1.output.shape, weight_shape=(k2, None, n2, 9), pool_shape=(p2, 2), act_type='tanh') layer3 = optimus.Affine( name='layer3', input_shape=layer2.output.shape, output_shape=(None, k3), act_type='tanh') layer4 = optimus.Affine( name='layer4', input_shape=layer3.output.shape, output_shape=(None, n_out), act_type='linear') param_nodes = [layer0, layer1, layer2, layer3, layer4] # 1.1 Create cloned nodes logscale_2 = logscale.clone("logscale_2") layer0_2 = layer0.clone('layer0_2') layer1_2 = layer1.clone('layer1_2') layer2_2 = layer2.clone('layer2_2') layer3_2 = layer3.clone('layer3_2') layer4_2 = layer4.clone('layer4_2') param_nodes_2 = [layer0_2, layer1_2, layer2_2, layer3_2, layer4_2] # 1.2 Create Loss # --------------- # sim_cost = y*hwr(D - sim_margin)^2 # diff_cost = (1 - y) * hwr(diff_margin - D)^2 # total = ave(sim_cost + diff_cost) sqdistance = optimus.SquaredEuclidean(name='euclidean') distance = optimus.Sqrt(name='sqrt') # Sim terms sim_margin_sum = optimus.Add(name="sim_margin_sum", num_inputs=2) sim_hwr = optimus.RectifiedLinear(name="sim_hwr") sim_sqhwr = optimus.Power(name='sim_sqhwr', exponent=2.0) sim_cost = optimus.Product(name="sim_cost") # Diff terms neg_distance = optimus.Multiply(name='neg_distance', weight_shape=None) neg_distance.weight.value = -1.0 diff_margin_sum = optimus.Add(name="diff_margin_sum", num_inputs=2) diff_hwr = optimus.RectifiedLinear(name="diff_hwr") diff_sqhwr = optimus.Power(name='diff_sqhwr', exponent=2.0) pos_one = optimus.Constant(name='pos_one', shape=None) pos_one.data.value = 1.0 neg_score = optimus.Multiply(name='neg_score', weight_shape=None) neg_score.weight.value = -1.0 diff_selector = optimus.Add("diff_selector", num_inputs=2) diff_cost = optimus.Product(name='diff_cost') total_cost = optimus.Add('total_cost', num_inputs=2) loss = optimus.Mean(name='loss') loss_nodes = [sqdistance, distance, sim_margin_sum, sim_hwr, sim_sqhwr, sim_cost, neg_distance, diff_margin_sum, diff_hwr, diff_sqhwr, pos_one, neg_score, diff_selector, diff_cost, total_cost, loss] # Graph outputs total_loss = optimus.Output(name='total_loss') embedding = optimus.Output(name='embedding') embedding_2 = optimus.Output(name='embedding_2') # 2. Define Edges base_edges = [ (input_data, logscale.input), (logscale.output, layer0.input), (layer0.output, layer1.input), (layer1.output, layer2.input), (layer2.output, layer3.input), (layer3.output, layer4.input), (layer4.output, embedding)] base_edges_2 = [ (input_data_2, logscale_2.input), (logscale_2.output, layer0_2.input), (layer0_2.output, layer1_2.input), (layer1_2.output, layer2_2.input), (layer2_2.output, layer3_2.input), (layer3_2.output, layer4_2.input), (layer4_2.output, embedding_2)] cost_edges = [ (layer4.output, sqdistance.input_a), (layer4_2.output, sqdistance.input_b), (sqdistance.output, distance.input), # Sim terms (score, sim_cost.input_a), (distance.output, sim_margin_sum.input_0), (sim_margin, sim_margin_sum.input_1), (sim_margin_sum.output, sim_hwr.input), (sim_hwr.output, sim_sqhwr.input), (sim_sqhwr.output, sim_cost.input_b), (sim_cost.output, total_cost.input_0), # Diff terms # - margin term (distance.output, neg_distance.input), (diff_margin, diff_margin_sum.input_0), (neg_distance.output, diff_margin_sum.input_1), (diff_margin_sum.output, diff_hwr.input), (diff_hwr.output, diff_sqhwr.input), # - score selector (score, neg_score.input), (pos_one.output, diff_selector.input_0), (neg_score.output, diff_selector.input_1), # - product (diff_selector.output, diff_cost.input_a), (diff_sqhwr.output, diff_cost.input_b), (diff_cost.output, total_cost.input_1)] trainer_edges = optimus.ConnectionManager( base_edges + base_edges_2 + cost_edges + [ # Combined (total_cost.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)) param_init(param_nodes) misc_nodes = [logscale, logscale_2] trainer = optimus.Graph( name=GRAPH_NAME, inputs=inputs, nodes=param_nodes + param_nodes_2 + loss_nodes + misc_nodes, connections=trainer_edges.connections, outputs=[total_loss, embedding, embedding_2], loss=total_loss, updates=update_manager.connections, verbose=False) pw_cost = optimus.Output(name='pw_cost') zerofilt_edges = optimus.ConnectionManager( base_edges + base_edges_2 + cost_edges + [ # Combined (total_cost.output, pw_cost)]) zerofilter = optimus.Graph( name=GRAPH_NAME + "_zerofilter", inputs=[input_data, input_data_2, score, sim_margin, diff_margin], nodes=param_nodes + param_nodes_2 + loss_nodes[:-1] + misc_nodes, connections=zerofilt_edges.connections, outputs=[pw_cost, embedding, embedding_2], verbose=False) predictor = optimus.Graph( name=GRAPH_NAME, inputs=[input_data], nodes=param_nodes + [logscale], connections=optimus.ConnectionManager(base_edges).connections, outputs=[embedding], verbose=False) return trainer, predictor, zerofilter