Repository: deeppavlov/DeepPavlov

Issue:
Error while training model with config "ner_conll2003_pos"
I'm trying to train a NER model using "train_model(configs.ner.ner_conll2003_pos)" on Colab. There are only three things I've changed in original ner_conll2003_pos.json file: number of epochs = 1, DOWNLOADS_PATH and MODELS_PATH. After I start, it terminates with this error:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-5-683c58afa1f4> in <module>()
      1 from deeppavlov import configs, train_model
----> 2 ner_model = train_model(configs.ner.ner_conll2003_pos)

/usr/local/lib/python3.6/dist-packages/deeppavlov/__init__.py in train_model(config, download, recursive)
     29     # TODO: make better
     30     def train_model(config: [str, Path, dict], download: bool = False, recursive: bool = False) -> Chainer:
---> 31         train_evaluate_model_from_config(config, download=download, recursive=recursive)
     32         return build_model(config, load_trained=True)
     33 

/usr/local/lib/python3.6/dist-packages/deeppavlov/core/commands/train.py in train_evaluate_model_from_config(config, iterator, to_train, evaluation_targets, to_validate, download, start_epoch_num, recursive)
    119 
    120     if to_train:
--> 121         trainer.train(iterator)
    122 
    123     res = {}

/usr/local/lib/python3.6/dist-packages/deeppavlov/core/trainers/nn_trainer.py in train(self, iterator)
    292         if callable(getattr(self._chainer, 'train_on_batch', None)):
    293             try:
--> 294                 self.train_on_batches(iterator)
    295             except KeyboardInterrupt:
    296                 log.info('Stopped training')

/usr/local/lib/python3.6/dist-packages/deeppavlov/core/trainers/nn_trainer.py in train_on_batches(self, iterator)
    232         self.start_time = time.time()
    233         if self.validate_first:
--> 234             self._validate(iterator)
    235 
    236         while True:

/usr/local/lib/python3.6/dist-packages/deeppavlov/core/trainers/nn_trainer.py in _validate(self, iterator, tensorboard_tag, tensorboard_index)
    142         self._send_event(event_name='before_validation')
    143         report = self.test(iterator.gen_batches(self.batch_size, data_type='valid', shuffle=False),
--> 144                            start_time=self.start_time)
    145 
    146         report['epochs_done'] = self.epoch

/usr/local/lib/python3.6/dist-packages/deeppavlov/core/trainers/fit_trainer.py in test(self, data, metrics, start_time, show_examples)
    204         for x, y_true in data:
    205             examples += len(x)
--> 206             y_predicted = list(self._chainer.compute(list(x), list(y_true), targets=expected_outputs))
    207             if len(expected_outputs) == 1:
    208                 y_predicted = [y_predicted]

/usr/local/lib/python3.6/dist-packages/deeppavlov/core/common/chainer.py in compute(self, x, y, targets)
    141             in_params += self.in_y
    142 
--> 143         return self._compute(*args, pipe=pipe, param_names=in_params, targets=targets)
    144 
    145     def __call__(self, *args):

/usr/local/lib/python3.6/dist-packages/deeppavlov/core/common/chainer.py in _compute(***failed resolving arguments***)
    167                 res = component(**dict(zip(in_keys, x)))
    168             else:
--> 169                 res = component(*x)
    170             if len(out_params) == 1:
    171                 mem[out_params[0]] = res

/usr/local/lib/python3.6/dist-packages/deeppavlov/models/preprocessors/one_hotter.py in __call__(self, batch, **kwargs)
     68                 one_hotted_utt = np.sum(one_hotted_utt, axis=0)
     69 
---> 70             one_hotted_batch.append(one_hotted_utt)
     71 
     72         if self._pad_zeros:

UnboundLocalError: local variable 'one_hotted_utt' referenced before assignment
-----------------------------------------------------------------------------------------------

How can I fix this? 


Discussion:

Resolving patch:
diff --git a/deeppavlov/models/preprocessors/one_hotter.py b/deeppavlov/models/preprocessors/one_hotter.py
--- a/deeppavlov/models/preprocessors/one_hotter.py
+++ b/deeppavlov/models/preprocessors/one_hotter.py
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from typing import List, Union
+from typing import List, Union, Iterable
 
 import numpy as np
 
@@ -56,7 +56,7 @@ def __call__(self, batch: List[List[int]], **kwargs) -> Union[List[List[np.ndarr
         one_hotted_batch = []
 
         for utt in batch:
-            if isinstance(utt, list):
+            if isinstance(utt, Iterable):
                 one_hotted_utt = self._to_one_hot(utt, self._depth)
             elif isinstance(utt, int):
                 if self._pad_zeros or self.single_vector:
@@ -77,5 +77,5 @@ def __call__(self, batch: List[List[int]], **kwargs) -> Union[List[List[np.ndarr
     def _to_one_hot(x, n):
         b = np.zeros([len(x), n], dtype=np.float32)
         for q, tok in enumerate(x):
-            b[q, tok] = 1
+            b[q, int(tok)] = 1
         return b


Test patch: