Repository: pykeen/pykeen
Issue: UnboundLocalError: local variable 'batch' referenced before assignment
Root cause: Validation Gap

### Discussed in https://github.com/pykeen/pykeen/discussions/826

<div type='discussions-op-text'>

<sup>Originally posted by **abhi8569** March 10, 2022</sup>
Hello All,

I am trying to run an ablation study but facing this error: 
```
[I 2022-03-10 02:12:47,948] A new study created in RDB with name: no-name-3edf8e5b-a2e4-4656-bb87-5b695ee18c89
/home/abhi8569/anaconda3/lib/python3.8/site-packages/optuna/distributions.py:560: UserWarning: The distribution is specified by [1, 40] and step=5, but the range is not divisible by `step`. It will be replaced by [1, 36].
  warnings.warn(
/home/abhi8569/anaconda3/lib/python3.8/site-packages/optuna/distributions.py:560: UserWarning: The distribution is specified by [32, 4000] and step=100, but the range is not divisible by `step`. It will be replaced by [32, 3932].
  warnings.warn(
No random seed is specified. Setting to 250223292.
[W 2022-03-10 02:12:51,001] Trial 0 failed because of the following error: UnboundLocalError("local variable 'batch' referenced before assignment")
Traceback (most recent call last):
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/optuna/study/_optimize.py", line 213, in _run_trial
    value_or_values = func(trial)
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/hpo/hpo.py", line 254, in __call__
    result = pipeline(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/pipeline/api.py", line 1200, in pipeline
    losses = training_loop_instance.train(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/training/training_loop.py", line 329, in train
    result = self._train(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/training/training_loop.py", line 466, in _train
    sub_batch_size, slice_size = self.sub_batch_and_slice(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/training/training_loop.py", line 894, in sub_batch_and_slice
    sub_batch_size, finished_search, supports_sub_batching = self._sub_batch_size_search(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/training/training_loop.py", line 973, in _sub_batch_size_search
    self._train(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/training/training_loop.py", line 636, in _train
    del batch
UnboundLocalError: local variable 'batch' referenced before assignment
Traceback (most recent call last):
  File "/home/abhi8569/Documents/Masters Thesis/Final Benchmarking/pykeen/ablation_study.py", line 310, in <module>
    ablation_pipeline(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/ablation/ablation.py", line 187, in ablation_pipeline
    _run_ablation_experiments(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/ablation/ablation.py", line 212, in _run_ablation_experiments
    hpo_pipeline_result = hpo_pipeline_from_path(rv_config_path)
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/hpo/hpo.py", line 472, in hpo_pipeline_from_path
    return hpo_pipeline_from_config(config, **kwargs)
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/hpo/hpo.py", line 477, in hpo_pipeline_from_config
    return hpo_pipeline(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/hpo/hpo.py", line 813, in hpo_pipeline
    study.optimize(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/optuna/study/study.py", line 400, in optimize
    _optimize(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/optuna/study/_optimize.py", line 66, in _optimize
    _optimize_sequential(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/optuna/study/_optimize.py", line 163, in _optimize_sequential
    trial = _run_trial(study, func, catch)
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/optuna/study/_optimize.py", line 264, in _run_trial
    raise func_err
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/optuna/study/_optimize.py", line 213, in _run_trial
    value_or_values = func(trial)
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/hpo/hpo.py", line 254, in __call__
    result = pipeline(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/pipeline/api.py", line 1200, in pipeline
    losses = training_loop_instance.train(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/training/training_loop.py", line 329, in train
    result = self._train(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/training/training_loop.py", line 466, in _train
    sub_batch_size, slice_size = self.sub_batch_and_slice(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/training/training_loop.py", line 894, in sub_batch_and_slice
    sub_batch_size, finished_search, supports_sub_batching = self._sub_batch_size_search(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/training/training_loop.py", line 973, in _sub_batch_size_search
    self._train(
  File "/home/abhi8569/anaconda3/lib/python3.8/site-packages/pykeen/training/training_loop.py", line 636, in _train
    del batch
UnboundLocalError: local variable 'batch' referenced before assignment

Process finished with exit code 1
```
Environment specifications are as follow:
OS: Pop OS 21.10
Python: 3.8.8
Pytorch: 1.10.2+cu113
Pykeen; 1.7.0

Code can be found [here](https://codeshare.io/km3Z3p )

Kindly let me know if there is any other information required to debug this issue.
Thank you</div>

I think in the file pykeen/training/training_loop.py at line 636, 
`del batch`
should be in the scope of for loop. I made the changes and I am not getting this error.

Hi @abhi8569 ,

thanks for looking into this! Would you mind opening a PR with your suggested change?

Hello @mberr,
I am sorry but I am not really familiar with VCS. I have never really made a pull request or worked with branches and all.

@abhi8569 , after looking into the code again, this should only happen if the training loader is empty, i.e., there is not a single batch. Could you check whether this is the case for you?

@abhi8569 , the error should be fixed now (more precisely, still error out, but provide information about recovering) with #860 being merged.