Esempio n. 1
0
def train():

    # ----------- Load data -----------
    # load data
    dict = cPickle.load(open('pre_load.p', 'rb'))
    tr_X, tr_y, tr_na_list, te_X, te_y, te_na_list = dict['tr_X'], dict[
        'tr_y'], dict['tr_na_list'], dict['te_X'], dict['te_y'], dict[
            'te_na_list']

    tr_positive = np.take(tr_X, np.where(tr_y == 1)[0])
    tr_positive = [t / np.max(t) for t in tr_positive]
    tr_positive = [
        librosa.feature.stack_memory(t.transpose(), n_steps=sh_order)
        for t in tr_positive
    ]
    tr_negative = np.take(tr_X, np.where(tr_y == 0)[0])
    tr_negative = [t / np.max(t) for t in tr_negative]
    tr_negative = [
        librosa.feature.stack_memory(t.transpose(), n_steps=sh_order)
        for t in tr_negative
    ]

    # # # ----------- Do training seperate bases for each file-----------
    # nmf_model=NMF(rank_p, norm_W=1,  iterations=500, update_func = "kl", verbose=True)
    # W_positive=[]
    # for f in tr_positive:
    #     [W,H,error]=nmf_model.process(f.transpose())
    #     W_positive.append(W)
    #
    # nmf_model=NMF(rank_p, norm_W=1,  iterations=500, update_func = "kl", verbose=True)
    # W_negative=[]
    # for f in tr_negative:
    #     [W,H,error]=nmf_model.process(f.transpose())
    #     W_negative.append(W)

    tr_positive = np.hstack(tr_positive)
    tr_negative = np.hstack(tr_negative)
    train_data = np.hstack((tr_positive, tr_negative))
    print >> sys.stderr, train_data.shape
    #
    # # # ----------- Do training overcomplete dictionary -----------
    # p = decomposition.PCA(whiten=True, n_components= 0.99)
    # pca_data=p.fit_transform(train_data)

    #   #   num=500
    # num_dim=pca_data.shape[1]
    # num_training_samples=pca_data.shape[0]
    # km = spherical_kmeans.OSKmeans(num,num_dim)
    # print "Learning k-means: "+ str(num)
    # for _ in range(1000):
    #    print _
    #    for index in range(num_training_samples):
    #        km.update(pca_data[index,:])
    # codebook=km.centroids
    # cPickle.dump( [codebook, p], open( W_name, 'wb' ), protocol=cPickle.HIGHEST_PROTOCOL )

    # # # ----------- Do training -----------
    if type == '0_1':
        print >> sys.stderr, "NMF on positive examples"
        nmf_model = NMF(rank_p,
                        norm_W=1,
                        iterations=200,
                        update_func="kls",
                        verbose=False)
        [W_positive, H, error] = nmf_model.process(tr_positive, lam=lam)
        # # # a_H=np.ones(rank_n+rank_p)
        # # # b_H=np.ones(rank_n+rank_p)
        # # # [error, W_positive, H_gap] = gap_vbem(tr_positive, rank_n+rank_p, a_H, b_H, iterations=100, verbose=True)
        # #
        print >> sys.stderr, "NMF on negative examples"
        nmf_model = NMF(rank_n,
                        norm_W=1,
                        iterations=200,
                        update_func="kls",
                        verbose=False)
        [W_negative, H, error] = nmf_model.process(tr_negative, lam=lam)
        # # # [error, W_negative, H_gap] = gap_vbem(tr_negative, rank_n+rank_p, a_H, b_H, iterations=100, verbose=True)
        cPickle.dump([W_positive, W_negative],
                     open(W_name, 'wb'),
                     protocol=cPickle.HIGHEST_PROTOCOL)
    elif type == '01':
        # # -------- Train with masking ----------
        print >> sys.stderr, "masked NMF on training files"
        mask = np.zeros((rank_p, tr_negative.shape[1]))
        V = np.hstack((tr_negative, tr_positive))
        H0 = np.random.rand(rank_n + rank_p, V.shape[1]) + eps
        H0[-mask.shape[0]:, :mask.shape[1]] = mask
        nmf_model = NMF(rank_n + rank_p,
                        norm_W=1,
                        iterations=200,
                        update_func="kls",
                        verbose=False)
        [W, H, error] = nmf_model.process(V, H0=H0, lam=lam)
        print >> sys.stderr, error
        # # a_H=np.ones(rank_n+rankwork/bird_backup/W/W_mel_01_kl_50p_50_9folds.n.p_p)
        # # b_H=np.ones(rank_n+rank_p)
        # # [error, W_gap, H_gap] = gap_vbem(V, rank_n+rank_p, a_H, b_H, H0, iterations=100, verbose=False)
        #
        cPickle.dump(W, open(W_name, 'wb'), protocol=cPickle.HIGHEST_PROTOCOL)
    else:
        raise ValueError('Dictionary type not recognized')

    print >> sys.stderr, "Dictionary " + W_name + " finished!"
Esempio n. 2
0
    def run_nmf(self, tr_positive, tr_negative):
        '''Extract a dictionary via NMF given a method chosen in config file
        Args:
           tr_positive: a numpy array containing all the positive examples 
           tr_negative: a numpy array containing all the negative examples
        Output:
            NONE, the dictionary is saved in a file
        '''
        print(tr_positive.shape)
        if self.type == '0_1':

            print("NMF on positive examples")
            nmf_model = NMF(self.rank_1,
                            norm_W=1,
                            iterations=self.iterations,
                            update_func=self.update_func,
                            verbose=True)
            [W_positive, H, error] = nmf_model.process(tr_positive)

            print("NMF on negative examples")
            nmf_model = NMF(self.rank_0,
                            norm_W=1,
                            iterations=self.iterations,
                            update_func=self.update_func,
                            verbose=True)
            [W_negative, H, error] = nmf_model.process(tr_negative)

            print("Saved dictionary to " + self.W_name)
            W = np.hstack((W_positive, W_negative))
            cPickle.dump([W_positive, W_negative],
                         open(self.W_name, 'wb'),
                         protocol=cPickle.HIGHEST_PROTOCOL)

        elif self.type == 'unsupervised':

            print("Unsupervised NMF")
            V = np.hstack((tr_negative, tr_positive))
            nmf_model = NMF(self.rank_0 + self.rank_1,
                            norm_W=1,
                            iterations=self.iterations,
                            update_func=self.update_func,
                            verbose=True)
            [W, H, error] = nmf_model.process(V)

            print("Saved dictionary to " + self.W_name)
            cPickle.dump(W,
                         open(self.W_name, 'wb'),
                         protocol=cPickle.HIGHEST_PROTOCOL)

        elif self.type == '01':
            # # -------- Train with masking ----------
            print("Masked NMF on training files")
            V = np.hstack((tr_negative, tr_positive))

            mask = np.zeros((self.rank_1, tr_negative.shape[1]))
            H0 = np.random.rand(self.rank_0 + self.rank_1, V.shape[1]) + eps
            H0[-mask.shape[0]:, :mask.shape[1]] = mask

            nmf_model = NMF(self.rank_0 + self.rank_1,
                            norm_W=1,
                            iterations=self.iterations,
                            update_func=self.update_func,
                            verbose=True)
            [W, H, error] = nmf_model.process(V, H0=H0)

            print("Saved dictionary to " + self.W_name)
            cPickle.dump(W,
                         open(self.W_name, 'wb'),
                         protocol=cPickle.HIGHEST_PROTOCOL)

        elif self.type == '01_orth':
            # # -------- Train with masking ----------
            print("masked NMF on training files")
            V = np.hstack((tr_negative, tr_positive))

            mask = np.zeros((self.rank_1, tr_negative.shape[1]))
            H0 = np.random.rand(self.rank_0 + self.rank_1, V.shape[1]) + eps
            H0[-mask.shape[0]:, :mask.shape[1]] = mask

            nmf_model = NMF(self.rank_0 + self.rank_1,
                            norm_W=1,
                            rankW0=self.rank_0,
                            rankW1=self.rank_1,
                            len_V0=tr_negative.shape[1],
                            iterations=self.iterations,
                            update_func=self.update_func,
                            verbose=False)
            print(self.lam_orth)
            [W, H, error] = nmf_model.process(V, H0=H0, lam_orth=self.lam_orth)

            print("Saved dictionary to " + self.W_name)
            cPickle.dump(W,
                         open(self.W_name, 'wb'),
                         protocol=cPickle.HIGHEST_PROTOCOL)
        else:
            raise ValueError('Dictionary type not recognized')

        return W