def test_optimizer_by_mock(X: InteractionMatrix, target_epoch: int) -> None: X_train, X_val = rowwise_train_test_split(X) evaluator = Evaluator(X_val, 0) optimizer = MockOptimizer( X_train, evaluator, fixed_params=dict(X_test=X_val.toarray(), target_epoch=target_epoch), logger=getLogger("IGNORE"), ) config, _ = optimizer.optimize(n_trials=1, random_seed=42) assert config["max_epoch"] == target_epoch
def test_optimizer_by_mock(X: sps.csr_matrix) -> None: X_train, X_val = rowwise_train_test_split(X, test_ratio=0.5, random_seed=0) evaluator = Evaluator(X_val, 0) optimizer = MockOptimizer(X_train, evaluator, logger=None, fixed_params=dict(X_test=X_val)) config, _ = optimizer.optimize(n_trials=40, random_seed=42) assert config["p1"] >= 0.9 assert (config["reg"] >= 0.99) and (config["reg"] <= 1.01) assert (config["I1"] >= 100) and (config["I1"] <= 102) assert (config["I2"] >= 500) and (config["I2"] <= 502) assert config["flag"] in ["foo", "bar"]
def test_cb2cf(X: InteractionMatrix, profile: ProfileMatrix) -> None: """Fit IALS & let mlp overfit. Args: X (InteractionMatrix): user_item interaction matrix profile (ProfileMatrix): profile """ try: from irspack.user_cold_start.cb2cf import CB2IALSOptimizer except: pytest.skip("Failed to import jax.") raise X_cf_train_all, X_val = rowwise_train_test_split(X_cf, test_ratio=0.5, random_seed=0) evaluator = Evaluator(X_val, 0) optim = CB2IALSOptimizer( X_cf_train_all, evaluator, profile, ) cb2cfrec, t, mlp_config = optim.search_all( 20, cf_fixed_params=dict(n_components=5, alpha=0, reg=1e-3, max_cg_steps=30), random_seed=0, ) vec_reconstruction = cb2cfrec.mlp.predict( profile.astype(np.float32).toarray()) vec_target = cb2cfrec.cf_rec.get_user_embedding() residual = ( (vec_reconstruction - vec_target)**2).sum() / (vec_target**2).sum() assert residual <= 1e-1
def test_metrics_ColdUser(U: int, I: int, U_test: int) -> None: rns = np.random.RandomState(42) uvec = rns.randn(U + U_test, 3) ivec = rns.randn(I, 3) true_score = uvec.dot(ivec.T) # + rns.randn(U, I) X = sps.csr_matrix((true_score > 0).astype(np.float64)) X_train = X[:U] X_val = X[U:] X_val_learn, X_val_target = rowwise_train_test_split(X_val, random_seed=0) X_train_all = sps.vstack([X_train, X_val_learn]) hot_evaluator = Evaluator( sps.csr_matrix(X_val_target), offset=U, cutoff=I // 2, n_threads=2 ) rec = P3alphaRecommender(X_train_all) rec.learn() hot_score = hot_evaluator.get_score(rec) with pytest.warns(UserWarning): cold_evaluator = EvaluatorWithColdUser( X_val_learn.tocsc(), X_val_target, cutoff=I // 2, mb_size=5 ) # csc matrix input should raise warning about # memory ordering, as csc-csc matrix product will be csc, # hence col-major matrix when made dense. cold_score = cold_evaluator.get_score(rec) shuffle_index = np.arange(X_val_learn.shape[0]) rns.shuffle(shuffle_index) cold_evaluator_shuffled = EvaluatorWithColdUser( X_val_learn[shuffle_index], X_val_target[shuffle_index], cutoff=I // 2 ) cold_score_shuffled = cold_evaluator_shuffled.get_score(rec) for key in cold_score: assert cold_score_shuffled[key] == pytest.approx(cold_score[key]) for key in hot_score: assert hot_score[key] == pytest.approx(cold_score[key], abs=1e-8)