utterworks / fast-bert

Super easy library for BERT based NLP models
Apache License 2.0
1.85k stars 342 forks source link

Error: name 'pos_weight' is not defined (multi-label classification) #267

Open zrajabi opened 3 years ago

zrajabi commented 3 years ago

I want to train multi-label classifier, however, I get the following error, although from what I have read, we should set pos_weight and set multi_label to True, Could you please help me what I should do?

NameError Traceback (most recent call last)

in () 93 94 args = parser.parse_args() ---> 95 train(args) in train(args) 70 multi_label=True, 71 ---> 72 logging_steps=10) 73 74 #learner.lr_find(start_lr=1e-5,optimizer_type='lamb') /home/ec2-user/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/fast_bert/learner_cls.py in from_pretrained_model(dataBunch, pretrained_path, output_dir, metrics, device, logger, finetuned_wgts_path, multi_gpu, is_fp16, loss_scale, warmup_steps, fp16_opt_level, grad_accumulation_steps, multi_label, max_grad_norm, adam_epsilon, logging_steps, freeze_transformer_layers, pos_weight, weight) 194 195 model = load_model( --> 196 dataBunch, pretrained_path, finetuned_wgts_path, device, multi_label 197 ) 198 /home/ec2-user/anaconda3/envs/pytorch_latest_p36/lib/python3.6/site-packages/fast_bert/learner_cls.py in load_model(dataBunch, pretrained_path, finetuned_wgts_path, device, multi_label) 144 config_class, model_class, _ = MODEL_CLASSES[model_type] 145 --> 146 model_class[1].pos_weight = pos_weight 147 model_class[1].weight = weight 148 NameError: name 'pos_weight' is not defined Here is my code: def train(args): if args.is_onepanel: args.out_dir = os.path.join("/onepanel/output/",args.out_dir) if not os.path.exists(args.out_dir): os.mkdir(args.out_dir) pos_weight = torch.ones([11]) device_cuda = torch.device("cuda") if torch.cuda.device_count() > 1: multi_gpu = True else: multi_gpu = False logger = logging.getLogger() databunch = BertDataBunch(".", ".", tokenizer=args.pretrained_model, train_file = TRAIN_DATA_FILE, label_file= label_file, val_file= VALIDATION_DATA_FILE, text_col='Tweet', label_col=labels, pos_weight = pos_weight, batch_size_per_gpu = 32, #args.batch_size, max_seq_length=512, multi_gpu = multi_gpu, multi_label=True, model_type='bert') metrics = [{'name': 'accuracy', 'function': accuracy}] learner = BertLearner.from_pretrained_model( databunch, pretrained_path=args.pretrained_model, metrics=metrics, device=device_cuda, logger=logger, finetuned_wgts_path=None, output_dir=args.out_dir, pos_weight = pos_weight, warmup_steps=200, multi_gpu = multi_gpu, is_fp16= True, multi_label=True, logging_steps=10) learner.fit(epochs=args.epochs, lr=2e-3, pos_weight = pos_weight, schedule_type="warmup_cosine_hard_restarts", optimizer_type="lamb") learner.save_model()
washcycle commented 3 years ago

Just applied a fix for this in #272