Coder Social home page Coder Social logo

Comments (6)

airaria avatar airaria commented on May 28, 2024

可能是因为dataloader返回的batch中的元素的顺序和BertModel接收的参数的顺序不一致。
Transformer 2.9中BertModel的forward参数顺序是

 def forward(
        self,
        input_ids=None,
        attention_mask=None,
        token_type_ids=None,
        position_ids=None,
        head_mask=None,
        inputs_embeds=None,
        encoder_hidden_states=None,
        encoder_attention_mask=None,
    )

你的dataloader返回的batch中元素的顺序是否与之一致?


建议自定义返回dict而不是tuple的dataset,使每个key和forward中参数名匹配,不容易发生此类对齐问题,例如:

class DictDataset(Dataset):
    def __init__(self,all_input_ids, all_input_mask, all_token_type_ids, all_labels):
        super(DictDataset, self).__init__()
        self.all_input_ids = all_input_ids
        self.all_input_mask = all_input_mask
        self.all_token_type_ids = all_token_type_ids
        self.all_labels = all_labels
    def __getitem__(self, index):
        input_ids = self.all_input_ids[index]
        input_mask = self.all_input_mask[index]
        token_type_ids = self.all_token_type_ids[index]
        labels = self.all_labels[index]
        return {'input_ids':input_ids,
                'attention_mask':input_mask,
                'token_type_ids': token_type_ids,
                'labels':labels}
    def __len__(self):
        return len(self.all_labels)

from textbrewer.

junrong1 avatar junrong1 commented on May 28, 2024

可能是因为dataloader返回的batch中的元素的顺序和BertModel接收的参数的顺序不一致。
Transformer 2.9中BertModel的forward参数顺序是

 def forward(
        self,
        input_ids=None,
        attention_mask=None,
        token_type_ids=None,
        position_ids=None,
        head_mask=None,
        inputs_embeds=None,
        encoder_hidden_states=None,
        encoder_attention_mask=None,
    )

你的dataloader返回的batch中元素的顺序是否与之一致?

建议自定义返回dict而不是tuple的dataset,使每个key和forward中参数名匹配,不容易发生此类对齐问题,例如:

class DictDataset(Dataset):
    def __init__(self,all_input_ids, all_input_mask, all_token_type_ids, all_labels):
        super(DictDataset, self).__init__()
        self.all_input_ids = all_input_ids
        self.all_input_mask = all_input_mask
        self.all_token_type_ids = all_token_type_ids
        self.all_labels = all_labels
    def __getitem__(self, index):
        input_ids = self.all_input_ids[index]
        input_mask = self.all_input_mask[index]
        token_type_ids = self.all_token_type_ids[index]
        labels = self.all_labels[index]
        return {'input_ids':input_ids,
                'attention_mask':input_mask,
                'token_type_ids': token_type_ids,
                'labels':labels}
    def __len__(self):
        return len(self.all_labels)

通过改变dataset解决了这个维度不匹配的问题,然后又出现了一个新的问题
TypeError: forward() got an unexpected keyword argument 'labels'

from textbrewer.

junrong1 avatar junrong1 commented on May 28, 2024

可能是因为dataloader返回的batch中的元素的顺序和BertModel接收的参数的顺序不一致。
Transformer 2.9中BertModel的forward参数顺序是

 def forward(
        self,
        input_ids=None,
        attention_mask=None,
        token_type_ids=None,
        position_ids=None,
        head_mask=None,
        inputs_embeds=None,
        encoder_hidden_states=None,
        encoder_attention_mask=None,
    )

你的dataloader返回的batch中元素的顺序是否与之一致?
建议自定义返回dict而不是tuple的dataset,使每个key和forward中参数名匹配,不容易发生此类对齐问题,例如:

class DictDataset(Dataset):
    def __init__(self,all_input_ids, all_input_mask, all_token_type_ids, all_labels):
        super(DictDataset, self).__init__()
        self.all_input_ids = all_input_ids
        self.all_input_mask = all_input_mask
        self.all_token_type_ids = all_token_type_ids
        self.all_labels = all_labels
    def __getitem__(self, index):
        input_ids = self.all_input_ids[index]
        input_mask = self.all_input_mask[index]
        token_type_ids = self.all_token_type_ids[index]
        labels = self.all_labels[index]
        return {'input_ids':input_ids,
                'attention_mask':input_mask,
                'token_type_ids': token_type_ids,
                'labels':labels}
    def __len__(self):
        return len(self.all_labels)

通过改变dataset解决了这个维度不匹配的问题,然后又出现了一个新的问题
TypeError: forward() got an unexpected keyword argument 'labels'

Traceback (most recent call last):
File "/Users/ray.yao/opt/anaconda3/envs/text-align/lib/python3.8/site-packages/textbrewer/distiller_utils.py", line 265, in get_outputs_from_batch
results_T = auto_forward(model_T,batch,args)
File "/Users/ray.yao/opt/anaconda3/envs/text-align/lib/python3.8/site-packages/textbrewer/distiller_utils.py", line 287, in auto_forward
results = model(**batch, **args)
File "/Users/ray.yao/opt/anaconda3/envs/text-align/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
TypeError: forward() got an unexpected keyword argument 'labels'
python-BaseException

完整的报错信息

from textbrewer.

junrong1 avatar junrong1 commented on May 28, 2024

可能是因为dataloader返回的batch中的元素的顺序和BertModel接收的参数的顺序不一致。
Transformer 2.9中BertModel的forward参数顺序是

 def forward(
        self,
        input_ids=None,
        attention_mask=None,
        token_type_ids=None,
        position_ids=None,
        head_mask=None,
        inputs_embeds=None,
        encoder_hidden_states=None,
        encoder_attention_mask=None,
    )

你的dataloader返回的batch中元素的顺序是否与之一致?
建议自定义返回dict而不是tuple的dataset,使每个key和forward中参数名匹配,不容易发生此类对齐问题,例如:

class DictDataset(Dataset):
    def __init__(self,all_input_ids, all_input_mask, all_token_type_ids, all_labels):
        super(DictDataset, self).__init__()
        self.all_input_ids = all_input_ids
        self.all_input_mask = all_input_mask
        self.all_token_type_ids = all_token_type_ids
        self.all_labels = all_labels
    def __getitem__(self, index):
        input_ids = self.all_input_ids[index]
        input_mask = self.all_input_mask[index]
        token_type_ids = self.all_token_type_ids[index]
        labels = self.all_labels[index]
        return {'input_ids':input_ids,
                'attention_mask':input_mask,
                'token_type_ids': token_type_ids,
                'labels':labels}
    def __len__(self):
        return len(self.all_labels)

通过改变dataset解决了这个维度不匹配的问题,然后又出现了一个新的问题
TypeError: forward() got an unexpected keyword argument 'labels'

Traceback (most recent call last):
File "/Users/ray.yao/opt/anaconda3/envs/text-align/lib/python3.8/site-packages/textbrewer/distiller_utils.py", line 265, in get_outputs_from_batch
results_T = auto_forward(model_T,batch,args)
File "/Users/ray.yao/opt/anaconda3/envs/text-align/lib/python3.8/site-packages/textbrewer/distiller_utils.py", line 287, in auto_forward
results = model(**batch, **args)
File "/Users/ray.yao/opt/anaconda3/envs/text-align/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
TypeError: forward() got an unexpected keyword argument 'labels'
python-BaseException

完整的报错信息

我目前用的transformer的版本是2.9.0

from textbrewer.

airaria avatar airaria commented on May 28, 2024

DictDataset仅做示例,其返回的字典的键值根据实际使用的模型自行修改

from textbrewer.

DeqianBai avatar DeqianBai commented on May 28, 2024

这个改变dataset是怎么改变的,我也遇到这个问题了,谢谢!

from textbrewer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.