Esempio n. 1
0
def test_shortest_path():
    nn, ngram = get_models()
    syms = nn.data.syms
    # trans = nn.Ys['1'].value
    trans = ngram.ngram
    fixed = {0: 'I', 2: 'V', 4: 'I'}
    shortest_path(trans, syms, fixed)
Esempio n. 2
0
def run_chord2vec():
    """
    To run the model.  To finishing the training early, set configs['max_iter'] to less.
    """
    configs = get_configs()
    configs['retrieve_model'] = False
    configs['max_iter'] = 1
    data = get_data(configs)
    nn, bigram = get_models(data, configs)
    print nn.plot_w1()
Esempio n. 3
0
def test_continue_to_end():
    nn, ngram = get_models()
    # fixed = {0: 'I', 2: 'V', 7: 'I'}
    # original_seq = ['I', 'IV', 'V']

    fixed = {0: 'C', 2: 'G', 7: 'C'}
    original_seq = ['C', 'F', 'G']
    new_seq, sym_inds = shortest_path(nn, fixed, 2, original_seq)
    print new_seq
    print sym_inds
Esempio n. 4
0
def retrieve_skipgram_and_ngram():
    """
    By default, loads cached model.  To train new model, set configs['retrieve_model']
    as in the run_chord2vec function
    :return: neural-net skipgram, bigram model
    """
    configs = get_configs()
    data = get_data(configs)
    nn, bigram = get_models(data, configs)

    # saves a plot of the 2D PCA of the chord vectors
    nn.plot_w1()
Esempio n. 5
0
def get_trans():
    configs = get_configs()
    data = get_data()
    nn, ngram = get_models()
    # if bigram nn, it's from t to t-1, then need to transpose?
    nn_trans_dict = nn.predict_identity()
    nn_trans = nn_trans_dict.values()[0]
    print '...nn_trans type', type(nn_trans), nn_trans.shape
    nn_trans = nn_trans_dict['1']
    print nn_trans_dict.keys()
    print '...nn_trans type', type(nn_trans), nn_trans.shape
    nn_trans = np.exp(nn_trans)
    ngram_trans = ngram.ngram
    return ngram_trans, nn_trans, ngram, nn, configs, data
Esempio n. 6
0
def predict_probs_seq():
    # want for each sym a prediction list of prob for all syms
    ON_LOG = True
    # seq = ['I', 'IV', 'I6', 'V']
    # seq = [u'I', u'vi', u'V7', u'I', u'V2', u'I6', u'V', u'V7', u'I', u'V6/vi', u'V6/5/vi', u'IV6', u'vii/o7', u'I',
    #        u'IV7', u'V2', u'I', u'V', u'V7', u'I', u'V', u'V', u'viio6', u'i', u'vii/o7/V', u'V', u'V7', u'i', u'i',
    #        u'V4/3', u'i6', u'V6/5/iv', u'iv', u'V6/5/V', u'V', u'V7', u'i', u'V', u'I6', u'I', u'IV6', u'viio', u'I',
    #        u'vi', u'V6/5/V', u'V7/V', u'V', u'I6', u'IV', u'V7', u'IV6/5', u'viio', u'I', u'ii6/5', u'V']
    seq = [u'IV6', u'vii/o7', u'I', u'IV7']
    model, ngram_model = get_models()
    processed_seq = PreprocessedSeq(seq, model.data.syms, model.data.window)
    probs, mean_score_total, probs_all = model.predict_reverse([processed_seq],
                                                               return_probs=True,
                                                               return_all=True)
    ngram_probs, ngram_probs_all = ngram_model.predict(seq, log=False,
                                                       return_all=True)
    assert len(seq) == len(ngram_probs_all)
    print 'len(probs)', len(probs)
    print 'len()', len(probs[0])
    print len(probs_all[0][0])
    print len(probs_all[0][0])
    print len(model.data.syms)

    syms = model.data.syms
    topn = 8
    # b/c assumes multiple sequences
    probs_all = probs_all[0]
    for i, sym in enumerate(seq):
        probs = probs_all[i]
        inds = np.argsort(probs)[::-1]

        ngram_probs = ngram_probs_all[i]
        ngram_inds = np.argsort(ngram_probs)[::-1]
        print '--- %s ---' % sym
        for j in range(topn):
            # ind for nn
            ind = inds[j]
            ngram_ind = ngram_inds[j]

            if not ON_LOG:
                nn_prob = np.exp(probs[ind])
                ngram_prob = ngram_probs[ngram_ind]
            else:
                nn_prob = probs[ind]
                ngram_prob = np.log(ngram_probs[ngram_ind])

            print '%s:  \t%.2f, \t%s: \5%.4f' % (syms[ind],
                                                 nn_prob,
                                                 syms[ngram_ind],
                                                 ngram_prob)
Esempio n. 7
0
        topn = 10
        similars = nn.most_similar(sym, topn=topn)

        for i, sim in enumerate(similars):
            print sim
            if i > topn/2:
                top_syms.append(sim[0])
            else:
                next_top_syms.append(sim[0])

    nn.plot_w1(query_syms + top_syms)
    nn.plot_w1(query_syms + next_top_syms)
    # syms = data.syms
    # print 'ngram counts'
    # print ngram.n_gram_counts[syms.index(query_syms[0]), :]

    query_syms_on_matrices(query_syms + top_syms, trans, trans_names,
                           data.syms, configs)
    query_syms_on_mat(query_syms+top_syms, nn_trans, data.syms, configs)

    query_syms_on_matrices(query_syms + next_top_syms, trans, trans_names,
                           data.syms, configs)

    query_syms_on_mat(query_syms+next_top_syms, nn_trans, data.syms, configs)

if __name__ == '__main__':
    # predict_probs_seq()
    # compare_transition_matrices()
    # query_syms_on_trans()
    get_models()