utterworks / fast-bert

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

RuntimeError: The size of tensor a (5) must match the size of tensor b (210916) at non-singleton dimension 1 #231

Closed subhashiniy closed 4 years ago

subhashiniy commented 4 years ago

I am getting this error on running the code -

./envname/lib/python3.6/site-packages/torch/nn/parallel/_functions.py:61: UserWarning: Was asked to gather along dimension 0, but all input tensors were scalars; will instead unsqueeze and return a vector. warnings.warn('Was asked to gather along dimension 0, but all ' /pytorch/torch/csrc/utils/python_argparser.cpp:756: UserWarning: This overload of add is deprecated: add(Number alpha, Tensor other) Consider using one of the following signatures instead: add(Tensor other, , Number alpha) Traceback (most recent call last):████████████████████████████████████████████████████████████████████| 100.00% [13183/13183 50:49<00:00]19:11:06] File "trainClassifier.py", line 75, in learner.fit(args.num_train_epochs, args.learning_rate, validate=True) File "./envname/lib/python3.6/site-packages/fast_bert/learner_cls.py", line 422, in fit results = self.validate() File "./envname/lib/python3.6/site-packages/fast_bert/learner_cls.py", line 544, in validate all_logits, all_labels File "./envname/lib/python3.6/site-packages/fast_bert/metrics.py", line 38, in accuracy_thresh return ((y_pred > thresh) == y_true.bool()).float().mean().item() File "./envname/lib/python3.6/site-packages/torch/tensor.py", line 28, in wrapped return f(args, **kwargs) RuntimeError: The size of tensor a (5) must match the size of tensor b (210916) at non-singleton dimension 1

Code is -

`from pathlib import Path
import torch
from box import Box
import collections
import sys
import datetime
from fast_bert.data_cls import BertDataBunch, InputExample, InputFeatures, MultiLabelTextProcessor, convert_examples_to_features
from fast_bert.learner_cls import BertLearner
from fast_bert.metrics import accuracy_thresh, fbeta, roc_auc
import logging
logger = logging.getLogger()
device = torch.device('cuda')
torch.cuda.empty_cache()
DATA_PATH = Path('./')
LABEL_PATH = Path('./')

MODEL_PATH=Path('./model_new/')
LOG_PATH=Path('./logs/')
MODEL_PATH.mkdir(exist_ok=True)

model_state_dict = None
BERT_PRETRAINED_PATH = Path('./')
FINETUNED_PATH = None
LOG_PATH.mkdir(exist_ok=True)
OUTPUT_PATH = MODEL_PATH/'output'
OUTPUT_PATH.mkdir(exist_ok=True)
args = Box({
    "run_text": "classification with freezable layers",
    "log_path": LOG_PATH,
    "data_dir": DATA_PATH,
    "bert_model": BERT_PRETRAINED_PATH,
    "output_dir": OUTPUT_PATH,
    "max_seq_length": 512,
    "train_batch_size": 4,
    "eval_batch_size": 4,
    "learning_rate": 5e-5,
    "num_train_epochs": 1,
    "fp16": False,
    "fp16_opt_level": "O1",
    "weight_decay": 0.0,
    "adam_epsilon": 1e-8,
    "max_grad_norm": 1.0,
    "warmup_steps": 500,
    "logging_steps": 50,
    "model_name": 'distilbert-base-uncased',
    "model_type": 'distilbert'
        })

if torch.cuda.device_count() > 1:
    args.multi_gpu = True
else:
    args.multi_gpu = False

multiLabel = False
label_cols = ['NUMERIC', 'DESCRIPTION', 'ENTITY', 'LOCATION', 'PERSON']
databunch = BertDataBunch(args['data_dir'], LABEL_PATH, args.model_name,
                          train_file='train.csv', val_file='val.csv',
                          text_col="text", label_col='label',
                          batch_size_per_gpu=args['train_batch_size'], max_seq_length=args['max_seq_length'],
                          multi_gpu=args.multi_gpu, multi_label=False, model_type=args.model_type)
metrics = []
metrics.append({'name': 'accuracy_thresh', 'function': accuracy_thresh})
metrics.append({'name': 'roc_auc', 'function': roc_auc})
metrics.append({'name': 'fbeta', 'function': fbeta})
learner = BertLearner.from_pretrained_model(databunch, args.model_name, metrics=metrics,
                                            device=device, logger=logger, output_dir=args.output_dir, 
                                            finetuned_wgts_path=FINETUNED_PATH, warmup_steps=args.warmup_steps,
                                            multi_gpu=args.multi_gpu, is_fp16=args.fp16, 
                                            multi_label=multiLabel, logging_steps=10)
learner.fit(args.num_train_epochs, args.learning_rate, validate=True)
learner.validate()
learner.save_model()
`

Environment: Version: 1.7.1