Git Product home page Git Product logo

Comments (13)

real-brilliant avatar real-brilliant commented on August 11, 2024

不需要分类个数 在'bert.py'的‘MyPro() - get_labels’里,直接把'return [0, 1]'改成你的类别名列表

from bert_chinese_pytorch.

li-cheng12 avatar li-cheng12 commented on August 11, 2024

我也能遇到了类似的问题,我把return[0,1]改成我的类别列表后,报了这个错,RuntimeError: CUDA error: device-side assert triggered。去网上查了下,是说类别的数量必须在0到n_classes之间,但是n_classes(分类个数)不知道在什么地方设置。我的类别数量有4000+个,请问有遇到类似的问题吗

from bert_chinese_pytorch.

badbubble avatar badbubble commented on August 11, 2024

@licheng-pro
如果你数据中的label已经转成了label id:

def get_labels(self):
    return [str(i) for i in range(n_classes)]

from bert_chinese_pytorch.

li-cheng12 avatar li-cheng12 commented on August 11, 2024

@ETCartman 没有转成label id,
image
并且当我换成cpu后,报了这个错。RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed. at /pytorch/aten/src/THNN/generic/ClassNLLCriterion.c:93

from bert_chinese_pytorch.

badbubble avatar badbubble commented on August 11, 2024

@licheng-pro 需要return是lable id,而不是label!
try this:

def get_labels(self, label_path):
    fo = open(label_path, 'r', encoding='utf-8')
    lines = fo.readlines()
    label_to_id ={}
    for i, line in enumerate(lines):
        label_to_id[line.strip()] = i
    print(label_to_id)
    fo.close()
    return [str(i) for i in range(len(label_to_id))]

or just:

def get_labels(self, label_path):
    fo = open(label_path, 'r', encoding='utf-8')
    lines = fo.readlines()
    fo.close()
    return [str(i) for i in range(len(lines))]

from bert_chinese_pytorch.

li-cheng12 avatar li-cheng12 commented on August 11, 2024

@ETCartman
convert_examples_to_features方法里的这个地方才是把label变成label id吧,getlabels方法返回的应该就是label吧?
image

from bert_chinese_pytorch.

badbubble avatar badbubble commented on August 11, 2024

@licheng-pro 刚去看了下代码确实是:stuck_out_tongue:,不过我做2k个类别没有任何问题。
RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed. at /pytorch/aten/src/THNN/generic/ClassNLLCriterion.c:93
我个人感觉这个错误肯定是label出了问题,比如label_path中的数据有重复, 你可以
return list(set(label_list))
试一下

from bert_chinese_pytorch.

li-cheng12 avatar li-cheng12 commented on August 11, 2024

我已经解决了,多谢了,老哥 @ETCartman

from bert_chinese_pytorch.

liuyijiang1994 avatar liuyijiang1994 commented on August 11, 2024

不过现在好像在

model = BertForSequenceClassification.from_pretrained(args.bert_model,
                                                          cache_dir=PYTORCH_PRETRAINED_BERT_CACHE / 'distributed_{}'.format(
                                                              args.local_rank))

这一行会报如下的错误:

File "/root/anaconda3/envs/liu37/lib/python3.7/site-packages/pytorch_pretrained_bert/modeling.py", line 581, in from_pretrained
   model = cls(config, *inputs, **kwargs)
TypeError: __init__() missing 1 required positional argument: 'num_labels'

from bert_chinese_pytorch.

Zhaohaoran1997 avatar Zhaohaoran1997 commented on August 11, 2024

不过现在好像在

model = BertForSequenceClassification.from_pretrained(args.bert_model,
                                                          cache_dir=PYTORCH_PRETRAINED_BERT_CACHE / 'distributed_{}'.format(
                                                              args.local_rank))

这一行会报如下的错误:

File "/root/anaconda3/envs/liu37/lib/python3.7/site-packages/pytorch_pretrained_bert/modeling.py", line 581, in from_pretrained
   model = cls(config, *inputs, **kwargs)
TypeError: __init__() missing 1 required positional argument: 'num_labels'

我也是这个问题,请问你解决了吗?

from bert_chinese_pytorch.

liuyijiang1994 avatar liuyijiang1994 commented on August 11, 2024

@Zhaohaoran1997 在最后加上这个参数就可以

model = BertForSequenceClassification.from_pretrained(args.bert_model,
                                                          cache_dir=PYTORCH_PRETRAINED_BERT_CACHE / 'distributed_{}'.format(
                                                              args.local_rank), num_labels=len(label_list))

也可以参考我fork的代码:https://github.com/liuyijiang1994/bert_senta

from bert_chinese_pytorch.

Zhaohaoran1997 avatar Zhaohaoran1997 commented on August 11, 2024

@Zhaohaoran1997 在最后加上这个参数就可以

model = BertForSequenceClassification.from_pretrained(args.bert_model,
                                                          cache_dir=PYTORCH_PRETRAINED_BERT_CACHE / 'distributed_{}'.format(
                                                              args.local_rank), num_labels=len(label_list))

也可以参考我fork的代码:https://github.com/liuyijiang1994/bert_senta

我改了num_labels之后程序被kill了,请问是我写得有bug还是机器性能不足?

04/22/2019 15:58:31 - INFO - pytorch_pretrained_bert.modeling - Weights of BertForSequenceClassification not initialized from pretrained model: ['classifier.bias', 'classifier.weight'] 04/22/2019 15:58:31 - INFO - pytorch_pretrained_bert.modeling - Weights from pretrained model not used in BertForSequenceClassification: ['cls.predictions.bias', 'cls.predictions.transform.dense.weight', 'cls.predictions.transform.dense.bias', 'cls.predictions.decoder.weight', 'cls.seq_relationship.weight', 'cls.seq_relationship.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.transform.LayerNorm.bias'] 04/22/2019 15:58:34 - INFO - __main__ - ***** Running training ***** 04/22/2019 15:58:34 - INFO - __main__ - Num examples = 43425 04/22/2019 15:58:34 - INFO - __main__ - Batch size = 128 04/22/2019 15:58:34 - INFO - __main__ - Num steps = 3392 Epoch: 0%| | 0/10 [00:00<?, ?it/s]已杀死ion: 0%| | 0/340 [00:00<?, ?it/s]

from bert_chinese_pytorch.

liuyijiang1994 avatar liuyijiang1994 commented on August 11, 2024

@Zhaohaoran1997 看上去运行的时候已经是正常的了 也许是性能的问题

from bert_chinese_pytorch.

Related Issues (9)

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.