def test2_create(self): set_log_level(3) opt = MatrixMarketOptions().get_default_option() opt.input.main = self.mm_path opt.input.uid = self.uid_path opt.input.iid = self.iid_path mm = MatrixMarket(opt) mm.create() self.temp_files.append(opt.data.path) self.assertTrue(True) db = mm.handle self.assertEqual(sorted(db.keys()), sorted(['vali', 'idmap', 'rowwise', 'colwise'])) header = mm.get_header() self.assertEqual(header['num_nnz'], 5) self.assertEqual(header['num_users'], 5) self.assertEqual(header['num_items'], 3) data = [(u, kk, vv) for u, kk, vv in mm.iterate()] self.assertEqual(len(data), 5) self.assertEqual([int(kk) for _, kk, _ in data], [0, 0, 2, 1, 1]) self.assertEqual(data[2], (2, 2, 1.0)) data = [(u, kk, vv) for u, kk, vv in mm.iterate(axis='colwise')] self.assertEqual([int(kk) for _, kk, _ in data], [0, 1, 3, 4, 2])
def test01_most_similar(self): set_log_level(2) data_opt = self.get_ml100k_mm_opt() opt = ALSOption().get_default_option() opt.d = 20 opt.num_workers = 1 als = ALS(opt, data_opt=data_opt) als.initialize() als.train() pals = ParALS(als) random_keys = [ k for k, _ in als.most_similar('49.Star_Wars_(1977)', topk=128) ] random_indexes = als.get_index_pool(random_keys) naive = [als.most_similar(k, topk=10) for k in random_keys] topks0 = [[k for k, _ in result] for result in naive] scores0 = np.array([[v for _, v in result] for result in naive]) self.assertEqual(scores0.shape, ( 128, 10, ), msg='check even size') scores0 = scores0.reshape(len(naive), 10) pals.num_workers = 1 topks1, scores1 = pals.most_similar(random_keys, topk=10, repr=True) topks2, scores2 = pals.most_similar(random_indexes, topk=10, repr=True) for a, b in combinations([topks0, topks1, topks2], 2): self.assertEqual(a, b) for a, b in combinations([scores0, scores1, scores2], 2): self.assertTrue(np.allclose(a, b))
def test10_fast_most_similar(self): set_log_level(1) opt = CFROption().get_default_option() data_opt = StreamOptions().get_default_option() data_opt.data.sppmi = {"windows": 5, "k": 10} data_opt.data.internal_data_type = "matrix" data_opt.input.main = self.ml_100k + 'stream' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.value_prepro = aux.Option({'name': 'OneBased'}) c = CFR(opt, data_opt=data_opt) c.initialize() c.train() keys = [x for x, _ in c.most_similar('49.Star_Wars_(1977)', 10)] start_t = time.time() for i in range(100): for key in keys: c.most_similar(key) elapsed_a = time.time() - start_t c.normalize(group='item') start_t = time.time() for i in range(100): for key in keys: c.most_similar(key) elapsed_b = time.time() - start_t self.assertTrue(elapsed_a > elapsed_b)
def test05_topk_MT(self): set_log_level(2) data_opt = self.get_ml100k_mm_opt() opt = ALSOption().get_default_option() opt.d = 20 opt.num_workers = 1 als = ALS(opt, data_opt=data_opt) als.initialize() als.train() als.build_userid_map() all_keys = als._idmanager.userids start_t = time.time() naive = als.topk_recommendation(all_keys, topk=5) naive_elapsed = time.time() - start_t pals = ParALS(als) pals.num_workers = 4 start_t = time.time() qkeys1, topks1, scores1 = pals.topk_recommendation(all_keys, topk=5, repr=True) par_elapsed = time.time() - start_t self.assertEqual(len(qkeys1), len(naive)) for q, t in zip(qkeys1, topks1): self.assertEqual(naive[q], t) self.assertTrue(naive_elapsed > par_elapsed * 1.5)
def test9_compact_serialization(self): set_log_level(1) opt = CFROption().get_default_option() data_opt = StreamOptions().get_default_option() data_opt.data.sppmi = {"windows": 5, "k": 10} data_opt.data.internal_data_type = "matrix" data_opt.input.main = self.ml_100k + 'stream' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.value_prepro = aux.Option({'name': 'OneBased'}) c = CFR(opt, data_opt=data_opt) c.initialize() c.train() ret_a = [x for x, _ in c.most_similar('49.Star_Wars_(1977)')] self.assertIn('180.Return_of_the_Jedi_(1983)', ret_a) c.save('model.bin', with_userid_map=False) c = CFR(opt) c.load('model.bin', data_fields=['I', '_idmanager']) ret_a = [x for x, _ in c.most_similar('49.Star_Wars_(1977)')] self.assertIn('180.Return_of_the_Jedi_(1983)', ret_a) self.assertFalse(hasattr(c, 'U')) c.normalize(group='item') ret_a = [x for x, _ in c.most_similar('49.Star_Wars_(1977)')] self.assertIn('180.Return_of_the_Jedi_(1983)', ret_a)
def _test10_fast_most_similar(self, cls, opt): set_log_level(1) data_opt = MatrixMarketOptions().get_default_option() data_opt.input.main = self.ml_100k + 'main' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.value_prepro = aux.Option({'name': 'OneBased'}) c = cls(opt, data_opt=data_opt) c.initialize() c.train() keys = [x for x, _ in c.most_similar('49.Star_Wars_(1977)', topk=100)] start_t = time.time() for i in range(100): for key in keys: c.most_similar(key) elapsed_a = time.time() - start_t c.normalize(group='item') start_t = time.time() for i in range(100): for key in keys: c.most_similar(key) elapsed_b = time.time() - start_t self.assertTrue(elapsed_a > elapsed_b)
def _test6_topk(self, cls, opt): set_log_level(2) data_opt = MatrixMarketOptions().get_default_option() data_opt.input.main = self.ml_100k + 'main' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.value_prepro = aux.Option({'name': 'OneBased'}) c = cls(opt, data_opt=data_opt) c.initialize() c.train() self.assertTrue(len(c.topk_recommendation('1', 10)), 10) ret_a = [ x for x, _ in c.most_similar('180.Return_of_the_Jedi_(1983)', topk=100) ] self.assertIn('49.Star_Wars_(1977)', ret_a) c.normalize() ret_b = [ x for x, _ in c.most_similar('180.Return_of_the_Jedi_(1983)', topk=100) ] self.assertIn('49.Star_Wars_(1977)', ret_b) self.assertEqual(ret_a[:10], ret_b[:10])
def _test9_compact_serialization(self, cls, opt): set_log_level(1) data_opt = MatrixMarketOptions().get_default_option() data_opt.input.main = self.ml_100k + 'main' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.value_prepro = aux.Option({'name': 'OneBased'}) c = cls(opt, data_opt=data_opt) c.initialize() c.train() ret_a = [ x for x, _ in c.most_similar('180.Return_of_the_Jedi_(1983)', topk=100) ] self.assertIn('49.Star_Wars_(1977)', ret_a) c.save('model.bin', with_userid_map=False) c = cls(opt) c.load('model.bin', data_fields=['Q', '_idmanager']) ret_a = [ x for x, _ in c.most_similar('180.Return_of_the_Jedi_(1983)', topk=100) ] self.assertIn('49.Star_Wars_(1977)', ret_a) self.assertFalse(hasattr(c, 'P')) c.normalize(group='item') ret_a = [ x for x, _ in c.most_similar('180.Return_of_the_Jedi_(1983)', topk=100) ] self.assertIn('49.Star_Wars_(1977)', ret_a)
def _test8_serialization(self, cls, opt): set_log_level(1) data_opt = MatrixMarketOptions().get_default_option() data_opt.input.main = self.ml_100k + 'main' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.value_prepro = aux.Option({'name': 'OneBased'}) c = cls(opt, data_opt=data_opt) c.initialize() c.train() ret_a = [ x for x, _ in c.most_similar('180.Return_of_the_Jedi_(1983)', topk=100) ] self.assertIn('49.Star_Wars_(1977)', ret_a) c.save('model.bin') c.load('model.bin') os.remove('model.bin') ret_a = [ x for x, _ in c.most_similar('180.Return_of_the_Jedi_(1983)', topk=100) ] self.assertIn('49.Star_Wars_(1977)', ret_a)
def test02_most_similar(self): num_cpu = psutil.cpu_count() if num_cpu < 2: return set_log_level(1) als = ALS() mp = MockParallel(als) R = 1000000 Q = self.get_factors(1000000, 12) indexes = np.random.choice(range(R), 1024).astype(np.int32) pool = np.array([], dtype=np.int32) elapsed = [] results = [] for num_workers in [1] + [i * 2 for i in range(1, num_cpu + 1) if i * 2 < num_cpu][:3]: mp.num_workers = num_workers start_t = time.time() ret = mp._most_similar('item', indexes, Q, 10, pool, -1, True) elapsed.append(time.time() - start_t) results.append(ret) for i in range(1, len(elapsed)): self.assertTrue(elapsed[i - 1] > elapsed[i] * 1.2) self.assertTrue(np.allclose(results[i - 1][0], results[i][0], atol=1e-07)) self.assertTrue(np.allclose(results[i - 1][1], results[i][1], atol=1e-07))
def test01_most_similar(self): set_log_level(1) model = self.load_text8_model() index = HnswIndex(model.L0.shape[1]) model.normalize('item') for f in model.L0: index.add_data(f) index.build(n_threads=4) index.save('n2.bin') par = ParW2V(model) model.opt.num_workers = 1 all_keys = model._idmanager.itemids[::][:10000] start_t = time.time() [model.most_similar(k, topk=10) for k in all_keys] naive_elapsed = time.time() - start_t par.num_workers = 4 start_t = time.time() par.most_similar(all_keys, topk=10, repr=True) par_elapsed = time.time() - start_t start_t = time.time() par.set_hnsw_index('n2.bin', 'item') par.most_similar(all_keys, topk=10, repr=True) ann_elapsed = time.time() - start_t self.assertTrue(naive_elapsed > par_elapsed * 1.5 > ann_elapsed * 5.0, msg=f'{naive_elapsed} > {par_elapsed} > {ann_elapsed}') index.unload() os.remove('n2.bin')
def test1_early_stopping(self): set_log_level(2) algo = MockAlgo() algo.initialize() algo.set_losses([1.0 + i / 1.0 for i in range(100)]) algo.opt.early_stopping_rounds = 5 algo.train() self.assertEqual(algo.last_iteration, 5)
def test03_pool(self): set_log_level(1) als = ALS() mp = MockParallel(als) Q = self.get_factors(128, 5) indexes = np.array([0, 1, 2, 3, 4], dtype=np.int32) pool = np.array([5, 6, 7], dtype=np.int32) topks, scores = mp._most_similar(indexes, Q, 10, pool) self.assertTrue(set(topks[::].reshape(10 * 5)), set([5, 6, 7, -1]))
def test01_most_similar(self): set_log_level(1) als = ALS() mp = MockParallel(als) Q = self.get_factors(128, 5) indexes = np.array([0, 1, 2, 3, 4], dtype=np.int32) pool = np.array([], dtype=np.int32) topks1, scores1 = mp._most_similar(indexes, Q, 10, pool) topks2, scores2 = self.get_most_similar(indexes, Q, 10) self.assertTrue(np.allclose(topks1, topks2)) self.assertTrue(np.allclose(scores1, scores2))
def _test4_train(self, cls, opt): set_log_level(3) data_opt = MatrixMarketOptions().get_default_option() data_opt.input.main = self.ml_100k + 'main' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.value_prepro = aux.Option({'name': 'OneBased'}) c = cls(opt, data_opt=data_opt) c.initialize() c.train() self.assertTrue(True)
def test04_topk(self): set_log_level(1) als = ALS() mp = MockParallel(als) P = self.get_factors(512, 5) Q = self.get_factors(128, 5) q_indexes = np.array([312, 313, 314, 315, 316], dtype=np.int32) pool = np.array([], dtype=np.int32) topks1, scores1 = mp._topk_recommendation(q_indexes, P, Q, 10, pool) topks2, scores2 = self.get_topk(q_indexes, P, Q, 10) self.assertTrue(np.allclose(topks1, topks2)) self.assertTrue(np.allclose(scores1, scores2))
def _test3_init(self, cls, opt): set_log_level(3) data_opt = MatrixMarketOptions().get_default_option() data_opt.input.main = self.ml_100k + 'main' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.path = './ml100k.h5py' c = cls(opt, data_opt=data_opt) self.assertTrue(True) c.init_factors() self.assertEqual(c.P.shape, (943, 20)) self.assertEqual(c.Q.shape, (1682, 20))
def _test7_train_ml_20m(self, cls, opt): set_log_level(3) data_opt = MatrixMarketOptions().get_default_option() data_opt.input.main = self.ml_20m + 'main' data_opt.input.uid = self.ml_20m + 'uid' data_opt.input.iid = self.ml_20m + 'iid' data_opt.data.path = './ml20m.h5py' data_opt.data.use_cache = True c = cls(opt, data_opt=data_opt) c.initialize() c.train() self.assertTrue(True)
def test2_most_similar(self): set_log_level(2) opt = ALSOption().get_default_option() data_opt = MatrixMarketOptions().get_default_option() data_opt.input.main = self.ml_100k + 'main' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' als = ALS(opt, data_opt=data_opt) als.initialize() als.train() q1, q2, q3 = '49.Star_Wars_(1977)', '180.Return_of_the_Jedi_(1983)', '171.Empire_Strikes_Back,_The_(1980)' self._test_most_similar(als, q1, q2, q3)
def example1(): log.set_log_level(log.DEBUG) als_option = ALSOption().get_default_option() als_option.validation = aux.Option({'topk': 10}) data_option = MatrixMarketOptions().get_default_option() data_option.input.main = '../tests/ext/ml-100k/main' data_option.input.iid = '../tests/ext/ml-100k/iid' als = ALS(als_option, data_opt=data_option) als.initialize() als.train() print('MovieLens 100k metrics for validations\n%s' % json.dumps(als.get_validation_results(), indent=2)) print('Similar movies to Star_Wars_(1977)') for rank, (movie_name, score) in enumerate(als.most_similar('49.Star_Wars_(1977)')): print(f'{rank + 1:02d}. {score:.3f} {movie_name}') print('Run hyper parameter optimization for val_ndcg...') als.opt.num_workers = 4 als.opt.evaluation_period = 10 als.opt.optimize = aux.Option({ 'loss': 'val_ndcg', 'max_trials': 100, 'deployment': True, 'start_with_default_parameters': True, 'space': { 'd': ['randint', ['d', 10, 128]], 'reg_u': ['uniform', ['reg_u', 0.1, 1.0]], 'reg_i': ['uniform', ['reg_i', 0.1, 1.0]], 'alpha': ['randint', ['alpha', 1, 10]], } }) log.set_log_level(log.INFO) als.opt.model_path = './example1.ml100k.als.optimize.bin' print(json.dumps({'alpha': als.opt.alpha, 'd': als.opt.d, 'reg_u': als.opt.reg_u, 'reg_i': als.opt.reg_i}, indent=2)) als.optimize() als.load('./example1.ml100k.als.optimize.bin') print('Similar movies to Star_Wars_(1977)') for rank, (movie_name, score) in enumerate(als.most_similar('49.Star_Wars_(1977)')): print(f'{rank + 1:02d}. {score:.3f} {movie_name}') optimization_res = als.get_optimization_data() best_parameters = optimization_res['best_parameters'] print(json.dumps(optimization_res['best'], indent=2)) print(json.dumps({'alpha': int(best_parameters['alpha']), 'd': int(best_parameters['d']), 'reg_u': best_parameters['reg_u'], 'reg_i': best_parameters['reg_i']}, indent=2))
def test4_train(self): set_log_level(3) opt = CFROption().get_default_option() data_opt = StreamOptions().get_default_option() data_opt.data.sppmi = {"windows": 5, "k": 10} data_opt.data.internal_data_type = "matrix" data_opt.input.main = self.ml_100k + 'stream' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.value_prepro = aux.Option({'name': 'OneBased'}) c = CFR(opt, data_opt=data_opt) c.initialize() c.train() self.assertTrue(True)
def _test5_validation(self, cls, opt, ndcg=0.06, map=0.04): set_log_level(2) data_opt = MatrixMarketOptions().get_default_option() data_opt.input.main = self.ml_100k + 'main' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.value_prepro = aux.Option({'name': 'OneBased'}) c = cls(opt, data_opt=data_opt) c.initialize() c.train() results = c.get_validation_results() self.assertTrue(results['ndcg'] > ndcg, msg='NDCG Test') self.assertTrue(results['map'] > map, msg='MAP Test')
def example2(): log.set_log_level(log.INFO) als_option = ALSOption().get_default_option() data_option = MatrixMarketOptions().get_default_option() data_option.input.main = '../tests/ext/ml-20m/main' data_option.input.iid = '../tests/ext/ml-20m/iid' data_option.data.path = './ml20m.h5py' data_option.data.use_cache = True als = ALS(als_option, data_opt=data_option) als.initialize() als.train() als.normalize('item') als.build_itemid_map() print( 'Make item recommendation on als.ml20m.par.top10.tsv with Paralell(Thread=4)' ) par = ParALS(als) par.num_workers = 4 all_items = als._idmanager.itemids start_t = time.time() with open('als.ml20m.par.top10.tsv', 'w') as fout: for idx in range(0, len(all_items), 128): topks, _ = par.most_similar(all_items[idx:idx + 128], repr=True) for q, p in zip(all_items[idx:idx + 128], topks): fout.write('%s\t%s\n' % (q, '\t'.join(p))) print('took: %.3f secs' % (time.time() - start_t)) from n2 import HnswIndex index = HnswIndex(als.Q.shape[1]) for f in als.Q: index.add_data(f) index.build(n_threads=4) index.save('ml20m.n2.index') index.unload() print( 'Make item recommendation on als.ml20m.par.top10.tsv with Ann(Thread=1)' ) par.set_hnsw_index('ml20m.n2.index', 'item') par.num_workers = 4 start_t = time.time() with open('als.ml20m.ann.top10.tsv', 'w') as fout: for idx in range(0, len(all_items), 128): topks, _ = par.most_similar(all_items[idx:idx + 128], repr=True) for q, p in zip(all_items[idx:idx + 128], topks): fout.write('%s\t%s\n' % (q, '\t'.join(p))) print('took: %.3f secs' % (time.time() - start_t))
def test3_init(self): set_log_level(3) opt = CFROption().get_default_option() opt.d = 20 data_opt = StreamOptions().get_default_option() data_opt.data.sppmi = {"windows": 5, "k": 10} data_opt.data.internal_data_type = "matrix" data_opt.input.main = self.ml_100k + 'stream' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.path = './ml100k.h5py' c = CFR(opt, data_opt=data_opt) self.assertTrue(True) c.initialize() self.assertEqual(c.U.shape, (943, 20)) self.assertEqual(c.I.shape, (1682, 20))
def test04_text8_most_similar(self): set_log_level(1) model = self.load_text8_model() par = ParW2V(model) model.opt.num_workers = 1 all_keys = model._idmanager.itemids[::][:10000] start_t = time.time() [model.most_similar(k, topk=10) for k in all_keys] naive_elapsed = time.time() - start_t par.num_workers = 4 start_t = time.time() par.most_similar(all_keys, topk=10, repr=True) par_elapsed = time.time() - start_t self.assertTrue(naive_elapsed > par_elapsed * 3.0)
def test07_topk_pool(self): set_log_level(2) data_opt = self.get_ml100k_mm_opt() opt = BPRMFOption().get_default_option() opt.d = 20 opt.num_workers = 1 model = BPRMF(opt, data_opt=data_opt) model.initialize() model.train() par = ParBPRMF(model) pool = np.array([i for i in range(5)], dtype=np.int32) model.build_userid_map() all_keys = model._idmanager.userids[::][:10] naive = model.topk_recommendation(all_keys, topk=10, pool=pool) qkeys1, topks1, scores1 = par.topk_recommendation(all_keys, topk=10, pool=pool, repr=True) for q, t in zip(qkeys1, topks1): self.assertEqual(naive[q], t)
def test4_optimize(self): set_log_level(2) opt = ALSOption().get_default_option() opt.d = 5 opt.num_workers = 2 opt.model_path = 'als.bin' opt.validation = aux.Option({'topk': 10}) optimize_option = aux.Option({ 'loss': 'val_rmse', 'max_trials': 10, 'deployment': True, 'start_with_default_parameters': True, 'space': { 'd': ['randint', ['d', 10, 20]], 'reg_u': ['uniform', ['reg_u', 0.1, 0.3]], 'reg_i': ['uniform', ['reg_i', 0.1, 0.3]], 'alpha': ['randint', ['alpha', 8, 10]] } }) opt.optimize = optimize_option opt.evaluation_period = 1 opt.tensorboard = aux.Option({'root': './tb', 'name': 'als'}) data_opt = MatrixMarketOptions().get_default_option() data_opt.input.main = self.ml_100k + 'main' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.value_prepro = aux.Option({'name': 'OneBased'}) als = ALS(opt, data_opt=data_opt) als.init_factors() als.train() default_result = als.get_validation_results() als.optimize() base_loss = default_result['rmse'] # val_rmse optimize_loss = als.get_optimization_data()['best']['val_rmse'] self.assertTrue(base_loss > optimize_loss) als.load('als.bin') loss = als.get_validation_results() self.assertAlmostEqual(loss['rmse'], optimize_loss) os.remove('als.bin')
def test00_tensorboard(self): set_log_level(2) opt = ALSOption().get_default_option() opt.d = 5 opt.validation = aux.Option({'topk': 10}) opt.tensorboard = aux.Option({'root': './tb', 'name': 'als'}) data_opt = MatrixMarketOptions().get_default_option() data_opt.input.main = self.ml_100k + 'main' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.value_prepro = aux.Option({'name': 'OneBased'}) als = ALS(opt, data_opt=data_opt) als.initialize() als.train() results = als.get_validation_results() self.assertTrue(results['ndcg'] > 0.025) self.assertTrue(results['map'] > 0.015)
def test5_validation(self, ndcg=0.06, map=0.04): set_log_level(3) opt = CFROption().get_default_option() opt.validation = aux.Option({'topk': 10}) opt.tensorboard = aux.Option({'root': './tb', 'name': 'cfr'}) data_opt = StreamOptions().get_default_option() data_opt.data.validation.name = "sample" data_opt.data.sppmi = {"windows": 5, "k": 10} data_opt.data.internal_data_type = "matrix" data_opt.input.main = self.ml_100k + 'stream' data_opt.input.uid = self.ml_100k + 'uid' data_opt.input.iid = self.ml_100k + 'iid' data_opt.data.value_prepro = aux.Option({'name': 'OneBased'}) c = CFR(opt, data_opt=data_opt) c.initialize() c.train() results = c.get_validation_results() self.assertTrue(results['ndcg'] > ndcg) self.assertTrue(results['map'] > map)
def test02_most_similar(self): set_log_level(1) als = ALS() mp = MockParallel(als) R = 1000000 Q = self.get_factors(1000000, 12) indexes = np.random.choice(range(R), 1024).astype(np.int32) pool = np.array([], dtype=np.int32) elapsed = [] results = [] for num_workers in [1, 2, 4, 8]: mp.num_workers = num_workers start_t = time.time() ret = mp._most_similar(indexes, Q, 10, pool) elapsed.append(time.time() - start_t) results.append(ret) for i in range(1, len(elapsed)): self.assertTrue(elapsed[i - 1] > elapsed[i] * 1.5) self.assertTrue(np.allclose(results[i - 1][0], results[i][0])) self.assertTrue(np.allclose(results[i - 1][1], results[i][1]))