def setUp(self): self.k = 40 self.recommender = FMRecommender(p=3, k=self.k) self.recommender.initialize()
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s') logging.root.setLevel(level=logging.INFO) logger.info('running %s' % ' '.join(sys.argv)) if len(sys.argv) < 2: print(globals()['__doc__'] % locals()) sys.exit(1) logging.info('converting data into FluRS input object') data = fetch_movielens(data_home=sys.argv[1], size='1m') logging.info('initialize recommendation model and evaluation module') rec = FMRecommender( p=sum(data.contexts.values()), # number of dimensions of input vector k=40, l2_reg_w0=2., l2_reg_w=8., l2_reg_V=16., learn_rate=.004) rec.initialize() evaluator = Evaluator(rec, data.can_repeat) n_batch_train = int(data.n_sample * 0.2) # 20% for pre-training to avoid cold-start n_batch_test = int(data.n_sample * 0.1) # 10% for evaluation of pre-training batch_tail = n_batch_train + n_batch_test # pre-train # 20% for batch training | 10% for batch evaluate # after the batch training, 10% samples are used for incremental updating
class FMRecommenderTestCase(TestCase): def setUp(self): self.k = 40 self.recommender = FMRecommender(p=3, k=self.k) self.recommender.initialize() def test_register_user(self): self.recommender.register(User(0)) self.assertEqual(self.recommender.n_user, 1) def test_register_item(self): self.recommender.register(Item(0)) self.assertEqual(self.recommender.n_item, 1) def test_update(self): self.recommender.register(User(0)) self.recommender.register(Item(0)) self.recommender.update(Event(User(0), Item(0), 1)) self.assertEqual(self.recommender.n_user, 1) self.assertEqual(self.recommender.n_item, 1) def test_score(self): self.recommender.register(User(0)) self.recommender.register(Item(0)) self.recommender.update(Event(User(0), Item(0), 1)) score = self.recommender.score(User(0), np.array([0]), np.array([0])) self.assertTrue(score >= 0.)
with open('h.pickle', 'wb') as f: pickle.dump(h, f) with open('data.pickle', 'wb') as f: pickle.dump(data, f) if os.path.exists('model.pickle'): print('Using pickled model') with open('model.pickle', 'rb') as f: rec = pickle.load(f) else: print('Training...') rec = FMRecommender( p=sum(data.contexts.values()), # number of dimensions of input vector k=60, l2_reg_w0=2., l2_reg_w=8., l2_reg_V=16., learn_rate=.004) rec.initialize() n_batch_train = int(data.n_sample * 0.9) evaluator = Evaluator(rec, data.can_repeat) evaluator.debug = True evaluator.fit(data.samples[:n_batch_train], data.samples[n_batch_train:], n_epoch=4) print('Done training!') with open('model.pickle', 'wb') as f: pickle.dump(rec, f)