Beispiel #1
0
    def test_th(self, activate_k, k_set):
        self.logger.info("Model testing thresholding starts")
        self.logger.info("Activation k(={}) in embeddings(={})".format(
            activate_k, self.args.m))
        test_k_activate = activate_k_2D(self.test_embed,
                                        k=activate_k,
                                        session=self.sess)  # [ntest, args.m]
        train_k_activate = activate_k_2D(self.train_embed,
                                         k=activate_k,
                                         session=self.sess)  # [ntrain, args.m]
        self.regen_session()

        if not hasattr(self, 'te_tr_distance') and not hasattr(
                self, 'te_te_distance'):
            self.regen_session()
            self.te_tr_distance = self.sess.run(
                pairwise_distance_euclid_v2(
                    tf.convert_to_tensor(self.test_embed, dtype=tf.float32),
                    tf.convert_to_tensor(self.train_embed, dtype=tf.float32)))
            self.te_te_distance = self.sess.run(
                pairwise_distance_euclid(
                    tf.convert_to_tensor(self.test_embed, dtype=tf.float32)))

        performance = evaluate_hash_te_tr(train_hash_key=train_k_activate, test_hash_key=test_k_activate,\
                                        te_tr_distance=self.te_tr_distance, te_te_distance=self.te_te_distance,\
                                        te_tr_query_key=test_k_activate, te_tr_query_value=self.test_embed,\
                                        te_te_query_key=test_k_activate, te_te_query_value=self.test_embed,\
                                        train_label=self.train_label, test_label=self.test_label,\
                                        ncls_train=self.ncls_train, ncls_test=self.ncls_test,\
                                        activate_k=activate_k, k_set=k_set, logger=self.logger)

        self.logger.info("Model testing thresholding ends")
        return performance
Beispiel #2
0
def test1():
    '''
    test for activate_k_1D,
    test for activate_k_2D

    Results -
	arr : [3 6 5 4]
	activated arr : [0 1 1 0]
	arr : [[3 6 5 4]
	 [2 1 3 4]]
	activated arr : [[0 1 1 0]
	 [0 0 1 1]]
	activated arr : [[0 1 1 0]
	 [0 0 1 1]]
	arr :  [[3 6 5 4]
	 [2 1 3 4]]
    '''
    arr = np.array([3, 6, 5, 4])
    print("arr :", arr)
    print("activated arr :", activate_k_1D(arr, 2))

    arr = np.array([[3, 6, 5, 4], [2, 1, 3, 4]])
    print("arr :", arr)
    print("activated arr :", activate_k_2D(arr, 2))

    with tf.Session() as sess:
        print("activated arr :", activate_k_2D(arr, 2, sess))
        print("arr : ", arr)
Beispiel #3
0
def test2():
    # test for activate_k_2D
    arr = np.array([[3, 6, 5, 4], [2, 1, 3, 4]])
    print("arr :", arr)
    print("activated arr :", activate_k_2D(arr, 2))

    # test for activate_k_2D_fast
    with tf.Session() as sess:
        print("activated arr :", activate_k_2D(arr, 2, sess))
        print("arr : ", arr)
Beispiel #4
0
    def test_vq(self, activate_k, k_set):
        self.logger.info("Model testing vq starts")
        self.logger.info("Activation k(={}) in buckets(={})".format(
            activate_k, self.args.m))
        if not hasattr(self, 'train_kmc') and not hasattr(self, 'test_kmc'):
            self.test_kmc = KMeansClustering(self.test_embed, self.args.m)
            self.train_kmc = KMeansClustering(self.train_embed, self.args.m)
            self.regen_session()
        if not hasattr(self, 'te_tr_distance') and not hasattr(
                self, 'te_te_distance'):
            self.te_tr_distance = self.sess.run(
                pairwise_distance_euclid_v2(
                    tf.convert_to_tensor(self.test_embed, dtype=tf.float32),
                    tf.convert_to_tensor(self.train_embed, dtype=tf.float32)))
            self.te_te_distance = self.sess.run(
                pairwise_distance_euclid(
                    tf.convert_to_tensor(self.test_embed, dtype=tf.float32)))
            self.regen_session()

        te_te_query_value = self.test_kmc.k_hash(
            self.test_embed, self.sess)  # [ntest, args.m] center test
        te_tr_query_value = self.train_kmc.k_hash(
            self.test_embed, self.sess)  # [ntest, args.m] center train
        self.regen_session()

        te_te_query_key = activate_k_2D(te_te_query_value,
                                        k=activate_k,
                                        session=self.sess)  # [ntest, args.m]
        test_hash_key = te_te_query_key

        te_tr_query_key = activate_k_2D(te_tr_query_value,
                                        k=activate_k,
                                        session=self.sess)  # [ntest, args.m]
        train_hash_key = activate_k_2D(self.train_kmc.k_hash(
            self.train_embed, self.sess),
                                       k=activate_k,
                                       session=self.sess)  # [ntrain, args.m]
        self.regen_session()

        performance = evaluate_hash_te_tr(train_hash_key=train_hash_key, test_hash_key=test_hash_key,\
                                        te_tr_distance=self.te_tr_distance, te_te_distance=self.te_te_distance,\
                                        te_tr_query_key=te_tr_query_key, te_tr_query_value=te_tr_query_value,\
                                        te_te_query_key=te_te_query_key, te_te_query_value=te_te_query_value,\
                                        train_label=self.train_label, test_label=self.test_label,\
                                        ncls_train=self.ncls_train, ncls_test=self.ncls_test,\
                                        activate_k=activate_k, k_set=k_set, logger=self.logger)

        self.logger.info("Model testing vq ends")
        return performance
    def test_hash_metric(self, activate_k, k_set):
        self.logger.info("Model testing k hash starts")
        self.logger.info("Activation k(={}) in buckets(={})".format(
            activate_k, self.args.d))

        self.regen_session()
        test_k_activate = activate_k_2D(self.test_k_hash,
                                        k=activate_k,
                                        session=self.sess)  # [ntest, args.d]
        if not hasattr(self, 'te_te_distance'):
            self.regen_session()
            self.te_te_distance = pairwise_distance_euclid_efficient(
                input1=self.test_embed,
                input2=self.test_embed,
                session=self.sess,
                batch_size=128)
            self.logger.info(
                "Calculating pairwise distance from test embeddings")

        performance = evaluate_hash_te(test_hash_key=test_k_activate, te_te_distance=self.te_te_distance,\
                                          te_te_query_key=test_k_activate, te_te_query_value=self.test_k_hash,\
                                          test_label=self.test_label, ncls_test=self.ncls_test,\
                                          activate_k=activate_k, k_set=k_set, logger=self.logger)

        self.logger.info("Model testing k hash ends")
        return performance
Beispiel #6
0
    def test_vq(self, activate_k, k_set):
        self.logger.info("Model testing vq starts")
        self.logger.info("Activation k(={}) in buckets(={})".format(
            activate_k, self.args.m))
        if not hasattr(self, 'test_kmc'):
            self.regen_session()
            self.test_kmc = KMeansClustering(self.test_embed, self.args.m)
        if not hasattr(self, 'te_te_distance'):
            self.regen_session()
            self.te_te_distance = pairwise_distance_euclid_efficient(
                input1=self.test_embed,
                input2=self.test_embed,
                session=self.sess,
                batch_size=128)
            self.logger.info(
                "Calculating pairwise distance from test embeddings")

        te_te_query_value = self.test_kmc.k_hash(
            self.test_embed, self.sess)  # [ntest, args.d] center test
        te_te_query_key = activate_k_2D(te_te_query_value,
                                        k=activate_k,
                                        session=self.sess)  # [ntest, args.d]
        test_hash_key = te_te_query_key
        self.regen_session()

        performance = evaluate_hash_te(test_hash_key=test_hash_key, te_te_distance=self.te_te_distance,\
                                          te_te_query_key=te_te_query_key, te_te_query_value=te_te_query_value,\
                                          test_label=self.test_label, ncls_test=self.ncls_test,\
                                          activate_k=activate_k, k_set=k_set, logger=self.logger)

        self.logger.info("Model testing vq ends")
        return performance