def test_save_and_load_model(self): model = TensorRec(n_components=10) model.fit(self.interactions, self.user_features, self.item_features, epochs=10) predictions = model.predict(user_features=self.user_features, item_features=self.item_features) ranks = model.predict_rank(user_features=self.user_features, item_features=self.item_features) model.save_model(directory_path=self.test_dir) # Check that, after saving, the same predictions come back predictions_after_save = model.predict( user_features=self.user_features, item_features=self.item_features) ranks_after_save = model.predict_rank(user_features=self.user_features, item_features=self.item_features) self.assertTrue((predictions == predictions_after_save).all()) self.assertTrue((ranks == ranks_after_save).all()) # Blow away the session set_session(None) tf.reset_default_graph() # Reload the model, predict, and check for equal predictions new_model = TensorRec.load_model(directory_path=self.test_dir) new_predictions = new_model.predict(user_features=self.user_features, item_features=self.item_features) new_ranks = new_model.predict_rank(user_features=self.user_features, item_features=self.item_features) self.assertTrue((predictions == new_predictions).all()) self.assertTrue((ranks == new_ranks).all())
def test_save_and_load_model(self): model = TensorRec(n_components=10) model.fit(self.interactions, self.user_features, self.item_features, epochs=10) predictions = model.predict(user_features=self.user_features, item_features=self.item_features) ranks = model.predict_rank(user_features=self.user_features, item_features=self.item_features) model.save_model(directory_path=self.test_dir) # Check that, after saving, the same predictions come back predictions_after_save = model.predict(user_features=self.user_features, item_features=self.item_features) ranks_after_save = model.predict_rank(user_features=self.user_features, item_features=self.item_features) self.assertTrue((predictions == predictions_after_save).all()) self.assertTrue((ranks == ranks_after_save).all()) # Blow away the session set_session(None) tf.reset_default_graph() # Reload the model, predict, and check for equal predictions new_model = TensorRec.load_model(directory_path=self.test_dir) new_predictions = new_model.predict(user_features=self.user_features, item_features=self.item_features) new_ranks = new_model.predict_rank(user_features=self.user_features, item_features=self.item_features) self.assertTrue((predictions == new_predictions).all()) self.assertTrue((ranks == new_ranks).all())
def test_basic_usage(self): # Build the model with default parameters model = TensorRec() # Generate some dummy data interactions, user_features, item_features = generate_dummy_data( num_users=100, num_items=150, interaction_density=.05) # Fit the model model.fit(interactions, user_features, item_features, epochs=5, verbose=True) # Predict scores for user 75 on items 100, 101, and 102 predictions = model.predict(user_ids=[75, 75, 75], item_ids=[100, 101, 102], user_features=user_features, item_features=item_features) # Calculate and print the recall at 10 r_at_k = recall_at_k(model, interactions, k=10, user_features=user_features, item_features=item_features) print(np.mean(r_at_k)) self.assertIsNotNone(predictions)
def test_fit_predict_unbiased(self): model = TensorRec(n_components=10, biased=False) model.fit(self.interactions, self.user_features, self.item_features, epochs=10) predictions = model.predict(user_features=self.user_features, item_features=self.item_features) self.assertEqual(predictions.shape, (self.user_features.shape[0], self.item_features.shape[0]))
def test_predict(self): interactions, user_features, item_features = generate_dummy_data( num_users=10, num_items=20, interaction_density=.5) model = TensorRec(n_components=10) model.fit(interactions, user_features, item_features, epochs=10) predictions = model.predict(user_features=user_features, item_features=item_features) self.assertEqual(predictions.shape, (user_features.shape[0], item_features.shape[0]))
def test_predict(self): interactions, user_features, item_features = generate_dummy_data( num_users=10, num_items=10, interaction_density=.5) model = TensorRec(n_components=10) model.fit(interactions, user_features, item_features, epochs=10) predictions = model.predict(user_ids=[1, 2, 3], item_ids=[4, 5, 6], user_features=user_features, item_features=item_features) self.assertEqual(len(predictions), 3)
def test_predict_fail_unfit(self): model = TensorRec() with self.assertRaises(ModelNotFitException): model.predict(self.user_features, self.item_features) with self.assertRaises(ModelNotFitException): model.predict_rank(self.user_features, self.item_features) with self.assertRaises(ModelNotFitException): model.predict_user_representation(self.user_features) with self.assertRaises(ModelNotFitException): model.predict_item_representation(self.item_features) with self.assertRaises(ModelNotFitException): model.predict_user_attention_representation(self.user_features) with self.assertRaises(ModelNotFitException): model.predict_similar_items(self.item_features, item_ids=[1], n_similar=5) with self.assertRaises(ModelNotFitException): model.predict_item_bias(self.item_features) with self.assertRaises(ModelNotFitException): model.predict_user_bias(self.user_features)
def test_save_and_load_model_same_session(self): model = TensorRec(n_components=10) model.fit(self.interactions, self.user_features, self.item_features, epochs=10) predictions = model.predict(user_features=self.user_features, item_features=self.item_features) ranks = model.predict_rank(user_features=self.user_features, item_features=self.item_features) model.save_model(directory_path=self.test_dir) # Reload the model, predict, and check for equal predictions new_model = TensorRec.load_model(directory_path=self.test_dir) new_predictions = new_model.predict(user_features=self.user_features, item_features=self.item_features) new_ranks = new_model.predict_rank(user_features=self.user_features, item_features=self.item_features) self.assertEqual(predictions.all(), new_predictions.all()) self.assertEqual(ranks.all(), new_ranks.all())
def test_movie_lens_fit_wmrb(self): """ This test checks whether the movielens getter works and that the resulting data is viable for fitting/testing a TensorRec model. """ train_interactions, test_interactions, user_features, item_features = self.movielens_100k model = TensorRec(loss_graph=wmrb_loss) model.fit(interactions=train_interactions, user_features=user_features, item_features=item_features) predictions = model.predict(user_features=user_features, item_features=item_features) self.assertIsNotNone(predictions)
def test_save_and_load_model_same_session(self): model = TensorRec(n_components=10) model.fit(self.interactions, self.user_features, self.item_features, epochs=10) predictions = model.predict(user_features=self.user_features, item_features=self.item_features) ranks = model.predict_rank(user_features=self.user_features, item_features=self.item_features) model.save_model(directory_path=self.test_dir) # Reload the model, predict, and check for equal predictions new_model = TensorRec.load_model(directory_path=self.test_dir) new_predictions = new_model.predict(user_features=self.user_features, item_features=self.item_features) new_ranks = new_model.predict_rank(user_features=self.user_features, item_features=self.item_features) self.assertTrue((predictions == new_predictions).all()) self.assertTrue((ranks == new_ranks).all())
def test_movie_lens_fit(self): """ This test checks whether the movielens getter works and that the resulting data is viable for fitting/testing a TensorRec model. """ train_interactions, test_interactions, user_features, item_features, _ = self.movielens_100k model = TensorRec() model.fit(interactions=train_interactions, user_features=user_features, item_features=item_features, epochs=5) predictions = model.predict(user_features=user_features, item_features=item_features) self.assertIsNotNone(predictions)