def test_model_dkn(deeprec_resource_path): data_path = os.path.join(deeprec_resource_path, "dkn") yaml_file = os.path.join(data_path, r"dkn.yaml") train_file = os.path.join(data_path, r"train_mind_demo.txt") valid_file = os.path.join(data_path, r"valid_mind_demo.txt") test_file = os.path.join(data_path, r"test_mind_demo.txt") news_feature_file = os.path.join(data_path, r"doc_feature.txt") user_history_file = os.path.join(data_path, r"user_history.txt") wordEmb_file = os.path.join(data_path, r"word_embeddings_100.npy") entityEmb_file = os.path.join(data_path, r"TransE_entity2vec_100.npy") contextEmb_file = os.path.join(data_path, r"TransE_context2vec_100.npy") download_deeprec_resources( "https://recodatasets.z20.web.core.windows.net/deeprec/", data_path, "mind-demo.zip", ) hparams = prepare_hparams( yaml_file, news_feature_file=news_feature_file, user_history_file=user_history_file, wordEmb_file=wordEmb_file, entityEmb_file=entityEmb_file, contextEmb_file=contextEmb_file, epochs=1, learning_rate=0.0001, ) input_creator = DKNTextIterator model = DKN(hparams, input_creator) assert isinstance(model.fit(train_file, valid_file), BaseModel) assert model.run_eval(valid_file) is not None
def test_npa_component_definition(mind_resource_path): wordEmb_file = os.path.join(mind_resource_path, "utils", "embedding.npy") userDict_file = os.path.join(mind_resource_path, "utils", "uid2index.pkl") wordDict_file = os.path.join(mind_resource_path, "utils", "word_dict.pkl") yaml_file = os.path.join(mind_resource_path, "utils", r"npa.yaml") if not os.path.exists(yaml_file): download_deeprec_resources( r"https://recodatasets.z20.web.core.windows.net/newsrec/", os.path.join(mind_resource_path, "utils"), "MINDdemo_utils.zip", ) hparams = prepare_hparams( yaml_file, wordEmb_file=wordEmb_file, wordDict_file=wordDict_file, userDict_file=userDict_file, epochs=1, ) iterator = MINDIterator model = NPAModel(hparams, iterator) assert model.model is not None assert model.scorer is not None assert model.loss is not None assert model.train_optimizer is not None
def test_prepare_hparams(must_exist_attributes, deeprec_resource_path): wordEmb_file = os.path.join(deeprec_resource_path, "mind", "utils", "embedding.npy") userDict_file = os.path.join( deeprec_resource_path, "mind", "utils", "uid2index.pkl" ) wordDict_file = os.path.join( deeprec_resource_path, "mind", "utils", "word_dict.pkl" ) yaml_file = os.path.join(deeprec_resource_path, "mind", "utils", r"nrms.yaml") if not os.path.exists(yaml_file): download_deeprec_resources( r"https://recodatasets.z20.web.core.windows.net/newsrec/", os.path.join(deeprec_resource_path, "mind", "utils"), "MINDdemo_utils.zip", ) hparams = prepare_hparams( yaml_file, wordEmb_file=wordEmb_file, wordDict_file=wordDict_file, userDict_file=userDict_file, epochs=1, ) assert hasattr(hparams, must_exist_attributes)
def test_load_yaml_file(deeprec_resource_path): yaml_file = os.path.join(deeprec_resource_path, "mind", "utils", r"nrms.yaml") if not os.path.exists(yaml_file): download_deeprec_resources( "https://recodatasets.z20.web.core.windows.net/newsrec/", os.path.join(deeprec_resource_path, "mind", "utils"), "MINDdemo_utils.zip", ) config = load_yaml(yaml_file) assert config is not None
def test_prepare_hparams(deeprec_resource_path, must_exist_attributes): data_path = os.path.join(deeprec_resource_path, "xdeepfm") yaml_file = os.path.join(data_path, "xDeepFM.yaml") if not os.path.exists(yaml_file): download_deeprec_resources( "https://recodatasets.z20.web.core.windows.net/deeprec/", data_path, "xdeepfmresources.zip", ) hparams = prepare_hparams(yaml_file) assert hasattr(hparams, must_exist_attributes)
def test_DKN_iterator(deeprec_resource_path): data_path = os.path.join(deeprec_resource_path, "dkn") data_file = os.path.join(data_path, r"train_mind_demo.txt") news_feature_file = os.path.join(data_path, r"doc_feature.txt") user_history_file = os.path.join(data_path, r"user_history.txt") wordEmb_file = os.path.join(data_path, "word_embeddings_100.npy") entityEmb_file = os.path.join(data_path, "TransE_entity2vec_100.npy") contextEmb_file = os.path.join(data_path, "TransE_context2vec_100.npy") yaml_file = os.path.join(data_path, "dkn.yaml") download_deeprec_resources( "https://recodatasets.z20.web.core.windows.net/deeprec/", data_path, "mind-demo.zip", ) hparams = prepare_hparams( yaml_file, news_feature_file=news_feature_file, user_history_file=user_history_file, wordEmb_file="", entityEmb_file="", contextEmb_file="", ) iterator = DKNTextIterator(hparams, tf.Graph()) assert iterator is not None for res, impression, data_size in iterator.load_data_from_file(data_file): assert isinstance(res, dict) # test DKN item2item iterator hparams = prepare_hparams( yaml_file, news_feature_file=news_feature_file, wordEmb_file=wordEmb_file, entityEmb_file=entityEmb_file, contextEmb_file=contextEmb_file, epochs=1, is_clip_norm=True, max_grad_norm=0.5, his_size=20, MODEL_DIR=os.path.join(data_path, "save_models"), use_entity=True, use_context=True, ) hparams.neg_num = 9 iterator_item2item = DKNItem2itemTextIterator(hparams, tf.Graph()) assert iterator_item2item is not None test_round = 3 for res, impression, data_size in iterator_item2item.load_data_from_file( os.path.join(data_path, "doc_list.txt")): assert isinstance(res, dict) test_round -= 1 if test_round <= 0: break
def test_load_yaml_file(deeprec_resource_path): data_path = os.path.join(deeprec_resource_path, "xdeepfm") yaml_file = os.path.join(data_path, "xDeepFM.yaml") if not os.path.exists(yaml_file): download_deeprec_resources( "https://recodatasets.z20.web.core.windows.net/deeprec/", data_path, "xdeepfmresources.zip", ) config = load_yaml(yaml_file) assert config is not None
def test_FFM_iterator(deeprec_resource_path): data_path = os.path.join(deeprec_resource_path, "xdeepfm") yaml_file = os.path.join(data_path, "xDeepFM.yaml") data_file = os.path.join(data_path, "sample_FFM_data.txt") if not os.path.exists(yaml_file): download_deeprec_resources( "https://recodatasets.z20.web.core.windows.net/deeprec/", data_path, "xdeepfmresources.zip", ) hparams = prepare_hparams(yaml_file) iterator = FFMTextIterator(hparams, tf.Graph()) assert iterator is not None for res in iterator.load_data_from_file(data_file): assert isinstance(res, tuple)
def test_xdeepfm_component_definition(deeprec_resource_path): data_path = os.path.join(deeprec_resource_path, "xdeepfm") yaml_file = os.path.join(data_path, "xDeepFM.yaml") if not os.path.exists(yaml_file): download_deeprec_resources( "https://recodatasets.z20.web.core.windows.net/deeprec/", data_path, "xdeepfmresources.zip", ) hparams = prepare_hparams(yaml_file) model = XDeepFMModel(hparams, FFMTextIterator) assert model.logit is not None assert model.update is not None assert model.iterator is not None
def test_model_xdeepfm(deeprec_resource_path): data_path = os.path.join(deeprec_resource_path, "xdeepfm") yaml_file = os.path.join(data_path, "xDeepFM.yaml") data_file = os.path.join(data_path, "sample_FFM_data.txt") output_file = os.path.join(data_path, "output.txt") if not os.path.exists(yaml_file): download_deeprec_resources( "https://recodatasets.z20.web.core.windows.net/deeprec/", data_path, "xdeepfmresources.zip", ) hparams = prepare_hparams(yaml_file, learning_rate=0.01) assert hparams is not None input_creator = FFMTextIterator model = XDeepFMModel(hparams, input_creator) assert model.run_eval(data_file) is not None assert isinstance(model.fit(data_file, data_file), BaseModel) assert model.predict(data_file, output_file) is not None
def dkn_files(deeprec_resource_path): data_path = os.path.join(deeprec_resource_path, "dkn") yaml_file = os.path.join(data_path, "dkn.yaml") news_feature_file = os.path.join(data_path, r"doc_feature.txt") user_history_file = os.path.join(data_path, r"user_history.txt") wordEmb_file = os.path.join(data_path, r"word_embeddings_100.npy") entityEmb_file = os.path.join(data_path, r"TransE_entity2vec_100.npy") contextEmb_file = os.path.join(data_path, r"TransE_context2vec_100.npy") download_deeprec_resources( "https://recodatasets.z20.web.core.windows.net/deeprec/", data_path, "mind-demo.zip", ) return ( data_path, yaml_file, news_feature_file, user_history_file, wordEmb_file, entityEmb_file, contextEmb_file, )
def test_model_naml(mind_resource_path): train_news_file = os.path.join(mind_resource_path, "train", r"news.tsv") train_behaviors_file = os.path.join(mind_resource_path, "train", r"behaviors.tsv") valid_news_file = os.path.join(mind_resource_path, "valid", r"news.tsv") valid_behaviors_file = os.path.join(mind_resource_path, "valid", r"behaviors.tsv") wordEmb_file = os.path.join(mind_resource_path, "utils", "embedding_all.npy") userDict_file = os.path.join(mind_resource_path, "utils", "uid2index.pkl") wordDict_file = os.path.join(mind_resource_path, "utils", "word_dict_all.pkl") vertDict_file = os.path.join(mind_resource_path, "utils", "vert_dict.pkl") subvertDict_file = os.path.join(mind_resource_path, "utils", "subvert_dict.pkl") yaml_file = os.path.join(mind_resource_path, "utils", r"naml.yaml") if not os.path.exists(train_news_file): download_deeprec_resources( r"https://recodatasets.z20.web.core.windows.net/newsrec/", os.path.join(mind_resource_path, "train"), "MINDdemo_train.zip", ) if not os.path.exists(valid_news_file): download_deeprec_resources( r"https://recodatasets.z20.web.core.windows.net/newsrec/", os.path.join(mind_resource_path, "valid"), "MINDdemo_dev.zip", ) if not os.path.exists(yaml_file): download_deeprec_resources( r"https://recodatasets.z20.web.core.windows.net/newsrec/", os.path.join(mind_resource_path, "utils"), "MINDdemo_utils.zip", ) hparams = prepare_hparams( yaml_file, wordEmb_file=wordEmb_file, wordDict_file=wordDict_file, userDict_file=userDict_file, vertDict_file=vertDict_file, subvertDict_file=subvertDict_file, epochs=1, ) iterator = MINDAllIterator model = NAMLModel(hparams, iterator) assert model.run_eval(valid_news_file, valid_behaviors_file) is not None assert isinstance( model.fit(train_news_file, train_behaviors_file, valid_news_file, valid_behaviors_file), BaseModel, )
def test_news_iterator(mind_resource_path): train_news_file = os.path.join(mind_resource_path, "train", r"news.tsv") train_behaviors_file = os.path.join(mind_resource_path, "train", r"behaviors.tsv") valid_news_file = os.path.join(mind_resource_path, "valid", r"news.tsv") valid_behaviors_file = os.path.join(mind_resource_path, "valid", r"behaviors.tsv") wordEmb_file = os.path.join(mind_resource_path, "utils", "embedding.npy") userDict_file = os.path.join(mind_resource_path, "utils", "uid2index.pkl") wordDict_file = os.path.join(mind_resource_path, "utils", "word_dict.pkl") yaml_file = os.path.join(mind_resource_path, "utils", r"nrms.yaml") if not os.path.exists(train_news_file): download_deeprec_resources( r"https://recodatasets.z20.web.core.windows.net/newsrec/", os.path.join(mind_resource_path, "train"), "MINDdemo_train.zip", ) if not os.path.exists(valid_news_file): download_deeprec_resources( r"https://recodatasets.z20.web.core.windows.net/newsrec/", os.path.join(mind_resource_path, "valid"), "MINDdemo_dev.zip", ) if not os.path.exists(yaml_file): download_deeprec_resources( r"https://recodatasets.z20.web.core.windows.net/newsrec/", os.path.join(mind_resource_path, "utils"), "MINDdemo_utils.zip", ) hparams = prepare_hparams( yaml_file, wordEmb_file=wordEmb_file, wordDict_file=wordDict_file, userDict_file=userDict_file, epochs=1, ) train_iterator = MINDIterator(hparams, hparams.npratio) test_iterator = MINDIterator(hparams, -1) assert train_iterator is not None for res in train_iterator.load_data_from_file(train_news_file, train_behaviors_file): assert isinstance(res, dict) assert len(res) == 5 break assert test_iterator is not None for res in test_iterator.load_data_from_file(valid_news_file, valid_behaviors_file): assert isinstance(res, dict) assert len(res) == 5 break