Hi, I am very new to use FastAI, I have a question that how to calculate the expected target batch size? I am currently using TextLoader for my dataset.
The codes are shown below:
tweet = TextDataLoaders.from_df(df_train, bs=128, path=‘.’, valid_pct=0.1,text_cols = [‘text’])
learn = language_model_learner(tweet, AWD_LSTM, metrics=[accuracy, Perplexity()], wd=0.1).to_fp16()
learn.fine_tune(10)
However, it shows the error message like:
0.00% [0/1 00:00<?]
epoch train_loss valid_loss accuracy perplexity time
0.00% [0/92 00:00<?]
ValueError Traceback (most recent call last)
Cell In[79], line 1
----> 1 learn.fine_tune(10)
File ~/miniconda3/lib/python3.10/site-packages/fastai/callback/schedule.py:165, in fine_tune(self, epochs, base_lr, freeze_epochs, lr_mult, pct_start, div, **kwargs)
163 “Fine tune with Learner.freeze
for freeze_epochs
, then with Learner.unfreeze
for epochs
, using discriminative LR.”
164 self.freeze()
→ 165 self.fit_one_cycle(freeze_epochs, slice(base_lr), pct_start=0.99, **kwargs)
166 base_lr /= 2
167 self.unfreeze()
File ~/miniconda3/lib/python3.10/site-packages/fastai/callback/schedule.py:119, in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt, start_epoch)
116 lr_max = np.array([h[‘lr’] for h in self.opt.hypers])
117 scheds = {‘lr’: combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
118 ‘mom’: combined_cos(pct_start, *(self.moms if moms is None else moms))}
→ 119 self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd, start_epoch=start_epoch)
File ~/miniconda3/lib/python3.10/site-packages/fastai/learner.py:264, in Learner.fit(self, n_epoch, lr, wd, cbs, reset_opt, start_epoch)
262 self.opt.set_hypers(lr=self.lr if lr is None else lr)
263 self.n_epoch = n_epoch
→ 264 self._with_events(self._do_fit, ‘fit’, CancelFitException, self._end_cleanup)
File ~/miniconda3/lib/python3.10/site-packages/fastai/learner.py:199, in Learner.with_events(self, f, event_type, ex, final)
198 def with_events(self, f, event_type, ex, final=noop):
→ 199 try: self(f’before{event_type}'); f()
200 except ex: self(f’after_cancel{event_type}‘)
201 self(f’after_{event_type}’); final()
File ~/miniconda3/lib/python3.10/site-packages/fastai/learner.py:253, in Learner._do_fit(self)
251 for epoch in range(self.n_epoch):
252 self.epoch=epoch
→ 253 self._with_events(self._do_epoch, ‘epoch’, CancelEpochException)
File ~/miniconda3/lib/python3.10/site-packages/fastai/learner.py:199, in Learner.with_events(self, f, event_type, ex, final)
198 def with_events(self, f, event_type, ex, final=noop):
→ 199 try: self(f’before{event_type}'); f()
200 except ex: self(f’after_cancel{event_type}‘)
201 self(f’after_{event_type}’); final()
File ~/miniconda3/lib/python3.10/site-packages/fastai/learner.py:247, in Learner._do_epoch(self)
246 def _do_epoch(self):
→ 247 self._do_epoch_train()
248 self._do_epoch_validate()
File ~/miniconda3/lib/python3.10/site-packages/fastai/learner.py:239, in Learner._do_epoch_train(self)
237 def _do_epoch_train(self):
238 self.dl = self.dls.train
→ 239 self._with_events(self.all_batches, ‘train’, CancelTrainException)
File ~/miniconda3/lib/python3.10/site-packages/fastai/learner.py:199, in Learner.with_events(self, f, event_type, ex, final)
198 def with_events(self, f, event_type, ex, final=noop):
→ 199 try: self(f’before{event_type}'); f()
200 except ex: self(f’after_cancel{event_type}‘)
201 self(f’after_{event_type}’); final()
File ~/miniconda3/lib/python3.10/site-packages/fastai/learner.py:205, in Learner.all_batches(self)
203 def all_batches(self):
204 self.n_iter = len(self.dl)
→ 205 for o in enumerate(self.dl): self.one_batch(*o)
File ~/miniconda3/lib/python3.10/site-packages/fastai/learner.py:235, in Learner.one_batch(self, i, b)
233 b = self._set_device(b)
234 self._split(b)
→ 235 self._with_events(self._do_one_batch, ‘batch’, CancelBatchException)
File ~/miniconda3/lib/python3.10/site-packages/fastai/learner.py:199, in Learner.with_events(self, f, event_type, ex, final)
198 def with_events(self, f, event_type, ex, final=noop):
→ 199 try: self(f’before{event_type}'); f()
200 except ex: self(f’after_cancel{event_type}‘)
201 self(f’after_{event_type}’); final()
File ~/miniconda3/lib/python3.10/site-packages/fastai/learner.py:219, in Learner._do_one_batch(self)
217 self(‘after_pred’)
218 if len(self.yb):
→ 219 self.loss_grad = self.loss_func(self.pred, *self.yb)
220 self.loss = self.loss_grad.clone()
221 self(‘after_loss’)
File ~/miniconda3/lib/python3.10/site-packages/fastai/losses.py:54, in BaseLoss.call(self, inp, targ, **kwargs)
52 if targ.dtype in [torch.int8, torch.int16, torch.int32]: targ = targ.long()
53 if self.flatten: inp = inp.view(-1,inp.shape[-1]) if self.is_2d else inp.view(-1)
—> 54 return self.func.call(inp, targ.view(-1) if self.flatten else targ, **kwargs)
File ~/miniconda3/lib/python3.10/site-packages/torch/nn/modules/module.py:1501, in Module._call_impl(self, *args, **kwargs)
1496 # If we don’t have any hooks, we want to skip the rest of the logic in
1497 # this function, and just call forward.
1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1499 or _global_backward_pre_hooks or _global_backward_hooks
1500 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1501 return forward_call(*args, **kwargs)
1502 # Do not call functions when jit is used
1503 full_backward_hooks, non_full_backward_hooks = [], []
File ~/miniconda3/lib/python3.10/site-packages/torch/nn/modules/loss.py:1174, in CrossEntropyLoss.forward(self, input, target)
1173 def forward(self, input: Tensor, target: Tensor) → Tensor:
→ 1174 return F.cross_entropy(input, target, weight=self.weight,
1175 ignore_index=self.ignore_index, reduction=self.reduction,
1176 label_smoothing=self.label_smoothing)
File ~/miniconda3/lib/python3.10/site-packages/torch/nn/functional.py:3015, in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
2949 r""“This criterion computes the cross entropy loss between input logits and target.
2950
2951 See :class:~torch.nn.CrossEntropyLoss
for details.
(…)
3012 >>> loss.backward()
3013 “””
3014 if has_torch_function_variadic(input, target, weight):
→ 3015 return handle_torch_function(
3016 cross_entropy,
3017 (input, target, weight),
3018 input,
3019 target,
3020 weight=weight,
3021 size_average=size_average,
3022 ignore_index=ignore_index,
3023 reduce=reduce,
3024 reduction=reduction,
3025 label_smoothing=label_smoothing,
3026 )
3027 if size_average is not None or reduce is not None:
3028 reduction = _Reduction.legacy_get_string(size_average, reduce)
File ~/miniconda3/lib/python3.10/site-packages/torch/overrides.py:1551, in handle_torch_function(public_api, relevant_args, *args, **kwargs)
1545 warnings.warn("Defining your __torch_function__ as a plain method is deprecated and " 1546 "will be an error in future, please define it as a classmethod.", 1547 DeprecationWarning) 1549 # Use
public_apiinstead of
implementation` so torch_function
1550 # implementations can do equality/identity comparisons.
→ 1551 result = torch_func_method(public_api, types, args, kwargs)
1553 if result is not NotImplemented:
1554 return result
File ~/miniconda3/lib/python3.10/site-packages/fastai/torch_core.py:382, in TensorBase.torch_function(cls, func, types, args, kwargs)
380 if cls.debug and func.name not in (‘str’,‘repr’): print(func, types, args, kwargs)
381 if _torch_handled(args, cls._opt, func): types = (torch.Tensor,)
→ 382 res = super().torch_function(func, types, args, ifnone(kwargs, {}))
383 dict_objs = _find_args(args) if args else _find_args(list(kwargs.values()))
384 if issubclass(type(res),TensorBase) and dict_objs: res.set_meta(dict_objs[0],as_copy=True)
File ~/miniconda3/lib/python3.10/site-packages/torch/_tensor.py:1295, in Tensor.torch_function(cls, func, types, args, kwargs)
1292 return NotImplemented
1294 with _C.DisableTorchFunctionSubclass():
→ 1295 ret = func(*args, **kwargs)
1296 if func in get_default_nowrap_functions():
1297 return ret
File ~/miniconda3/lib/python3.10/site-packages/torch/nn/functional.py:3029, in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
3027 if size_average is not None or reduce is not None:
3028 reduction = _Reduction.legacy_get_string(size_average, reduce)
→ 3029 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
ValueError: Expected input batch_size (6912) to match target batch_size (128).
I try to install the old version of FastAI but it didn’t succeed cause I want to use the function of TextDataBunch, and I checked the batch size as:
Could you please help me with how to solve the problem? Thank you!