RUCAIBox / RecBole

A unified, comprehensive and efficient recommendation library
https://recbole.io/
MIT License
3.47k stars 615 forks source link

ValueError: `model_name` [DuoRec] is not the name of an existing model. #1975

Open XiaoLongtaoo opened 10 months ago

XiaoLongtaoo commented 10 months ago

目前在跑baseline---DuoRec,按照recbole的Customize Models指南以及作者相应的源码,实现了class DuoRec(SequentialRecommender)(主要是DuoRec模型定义以及配置文件这两部分)。但是运行 python run_baseline.py --model DuoRec -d my_dataset --config_files=props/finetune.yaml 其中run_baseline的实现与https://recbole.io/docs/developer_guide/customize_models.html 的 python run.py --embedding_size=64实现类似。 产生了下列报错: Traceback (most recent call last): File "/home/xiaolongtao/UniSRec/run_baseline.py", line 83, in <module> baseline_func(model=args.model, dataset=args.dataset, config_file_list=config_file_list) File "/home/xiaolongtao/UniSRec/run_baseline.py", line 19, in run_baseline config = Config(model=model_name, dataset=dataset, config_file_list=config_file_list) File "/home/xiaolongtao/anaconda3/envs/UniSRec/lib/python3.9/site-packages/recbole/config/configurator.py", line 88, in __init__ self.model, self.model_class, self.dataset = self._get_model_and_dataset( File "/home/xiaolongtao/anaconda3/envs/UniSRec/lib/python3.9/site-packages/recbole/config/configurator.py", line 216, in _get_model_and_dataset final_model_class = get_model(final_model) File "/home/xiaolongtao/anaconda3/envs/UniSRec/lib/python3.9/site-packages/recbole/utils/utils.py", line 81, in get_model raise ValueError( ValueError: model_name [DuoRec] is not the name of an existing model. 请问这里哪里出了问题呢,模型以及相应的参数配置都实现了,而且也确保了Config()model是有值的。

XiaoLongtaoo commented 10 months ago

上面的问题我发现是由于你们Config的设置上似乎出现了点问题,这里的config = Config(model=DuoRec, dataset=dataset, config_file_list=config_file_list) DuoRec不能以model_name='DuoRec'的参数形式传进去,否则将会报错,对于自定义的模型不能通过传model=model_name来调用,只能直接以model=Customize model name这个形式调用。但是这里出现了另一个问题,请问这里的sem_aug是哪里出现的呢,我看DuoRec的source code,发现他们的实现似乎除了模型以及配置文件外,其余都与recbole的默认文件保持一致。如果我哪里出现了错误,劳烦指正我一下,谢谢!O(∩_∩)O

Traceback (most recent call last):
  File "/home/xiaolongtao/UniSRec/run_baseline.py", line 96, in <module>
    baseline_func(model=args.model, dataset=args.dataset, config_file_list=config_file_list)
  File "/home/xiaolongtao/UniSRec/run_baseline.py", line 64, in run_baseline
    best_valid_score, best_valid_result = trainer.fit(
  File "/home/xiaolongtao/anaconda3/envs/UniSRec/lib/python3.9/site-packages/recbole/trainer/trainer.py", line 439, in fit
    train_loss = self._train_epoch(
  File "/home/xiaolongtao/anaconda3/envs/UniSRec/lib/python3.9/site-packages/recbole/trainer/trainer.py", line 245, in _train_epoch
    losses = loss_func(interaction)
  File "/home/xiaolongtao/UniSRec/baselines/duorec.py", line 190, in calculate_loss
    sem_aug, sem_aug_lengths = interaction['sem_aug'], interaction['sem_aug_lengths']
  File "/home/xiaolongtao/anaconda3/envs/UniSRec/lib/python3.9/site-packages/recbole/data/interaction.py", line 135, in __getitem__
    return self.interaction[index]
KeyError: 'sem_aug'
TayTroye commented 9 months ago

@XiaoLongtaoo 你好! 这里的sem_augsoure code写在dataloader的duorec_aug用于数据增强的部分

XiaoLongtaoo commented 9 months ago

@XiaoLongtaoo 你好! 这里的sem_augsoure code写在dataloader的duorec_aug用于数据增强的部分

我找到了这个数据增强办法,但是这里仍然存在一个问题,当我将它用在我自己的dataloader中时,代码如下所示:

    def duorec_aug(self, cur_data, index, interaction):
        cur_same_target = self.same_target_index[index]
        null_index = []
        sample_pos = []
        for i, targets in enumerate(cur_same_target):
            # in case there is no same-target sequence
            # don't know why this happens since the filtering has been applied
            if len(targets) == 0:
                sample_pos.append(-1)
                null_index.append(i)
            else:
                sample_pos.append(np.random.choice(targets))
        sem_pos_seqs = self.static_item_id_list[sample_pos] 
        sem_pos_lengths = self.static_item_length[sample_pos]
        if null_index:
            sem_pos_seqs[null_index] = cur_data['item_id_list'][null_index]
            sem_pos_lengths[null_index] = cur_data['item_length'][null_index]

        cur_data.update(Interaction({'sem_aug': sem_pos_seqs, 'sem_aug_lengths': sem_pos_lengths}))
        return cur_data

训练时所用的dataloader代码如下:

class CustomizedTrainDataLoader(TrainDataLoader):
    def __init__(self, config, dataset, sampler, shuffle=False):
        super().__init__(config, dataset, sampler, shuffle=shuffle)
        self.transform = construct_transform(config)
        self.model = config['model']

        "special parameters for DuoRec"
        if self.model == 'DuoRec':
            self.same_target_index = dataset.same_target_index
            self.static_item_id_list = dataset.static_item_id_list
            self.static_item_length = dataset.static_item_length
            # self.static_item_id_list = self.dataset.inter_feat['item_id_list']
            # self.static_item_length = self.dataset.inter_feat['item_length']
        """"""

    def _next_batch_data(self):
        cur_data = super()._next_batch_data()
        # index = cur_data.index()
        index = slice(self.pr - self.step, self.pr)
    """"""
        if self.model == 'DuoRec':
            cur_data = self.duorec_aug(self, cur_data, index)
            return cur_data
    """"""
        else:    
            transformed_data = self.transform(self, cur_data)
            return transformed_data

    def duorec_aug(self, cur_data, index, interaction):
        cur_same_target = self.same_target_index[index]
        null_index = []
        sample_pos = []
        for i, targets in enumerate(cur_same_target):
            # in case there is no same-target sequence
            # don't know why this happens since the filtering has been applied
            if len(targets) == 0:
                sample_pos.append(-1)
                null_index.append(i)
            else:
                sample_pos.append(np.random.choice(targets))
        sem_pos_seqs = self.static_item_id_list[sample_pos] 
        sem_pos_lengths = self.static_item_length[sample_pos]
        if null_index:
            sem_pos_seqs[null_index] = cur_data['item_id_list'][null_index]
            sem_pos_lengths[null_index] = cur_data['item_length'][null_index]

        cur_data.update(Interaction({'sem_aug': sem_pos_seqs, 'sem_aug_lengths': sem_pos_lengths}))
        return cur_data

请问为什么读取训练数据时仍然不会更新,数据中还是没有出现sem_aug, sem_aug_lengths这两个属性,请问这是哪里出现了问题呢?感谢!

sunnystarh commented 2 months ago

同问,请问这个问题解决了吗