Nan loss in the validation set when training on custom dataset

Hello, I am trying to do font images classifier on custom dataset on kaggle and i came across a problem every time i try to to train which having a nan loss on the validation set
here’s my code:


KeyError: Caught KeyError in DataLoader worker process 0.

Original Traceback (most recent call last):
File “/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/”, line 185, in _worker_loop
data = fetcher.fetch(index)
File “/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/”, line 34, in fetch
data = next(self.dataset_iter)
File “/opt/conda/lib/python3.7/site-packages/fastai/data/”, line 107, in create_batches
yield from map(self.do_batch, self.chunkify(res))
File “/opt/conda/lib/python3.7/site-packages/fastcore/”, line 353, in chunked
res = list(itertools.islice(it, chunk_sz))
File “/opt/conda/lib/python3.7/site-packages/fastai/data/”, line 120, in do_item
try: return self.after_item(self.create_item(s))
File “/opt/conda/lib/python3.7/site-packages/fastai/data/”, line 126, in create_item
def create_item(self, s): return next( if s is None else self.dataset[s]
File “/opt/conda/lib/python3.7/site-packages/fastai/data/”, line 293, in getitem
res = tuple([tl[it] for tl in self.tls])
File “/opt/conda/lib/python3.7/site-packages/fastai/data/”, line 293, in
res = tuple([tl[it] for tl in self.tls])
File “/opt/conda/lib/python3.7/site-packages/fastai/data/”, line 270, in getitem
return self._after_item(res) if is_indexer(idx) else
File “/opt/conda/lib/python3.7/site-packages/fastai/data/”, line 232, in _after_item
def _after_item(self, o): return self.tfms(o)
File “/opt/conda/lib/python3.7/site-packages/fastcore/”, line 203, in call
def call(self, o): return compose_tfms(o, tfms=self.fs, split_idx=self.split_idx)
File “/opt/conda/lib/python3.7/site-packages/fastcore/”, line 155, in compose_tfms
x = f(x, **kwargs)
File “/opt/conda/lib/python3.7/site-packages/fastcore/”, line 72, in call
def call(self, x, **kwargs): return self._call(‘encodes’, x, **kwargs)
File “/opt/conda/lib/python3.7/site-packages/fastcore/”, line 82, in _call
return self._do_call(getattr(self, fn), x, **kwargs)
File “/opt/conda/lib/python3.7/site-packages/fastcore/”, line 88, in _do_call
return retain_type(f(x, **kwargs), x, ret)
File “/opt/conda/lib/python3.7/site-packages/fastcore/”, line 99, in call
return f(*args, **kwargs)
File “/opt/conda/lib/python3.7/site-packages/fastai/data/”, line 241, in encodes
def encodes(self, o): return TensorCategory(self.vocab.o2i[o])
KeyError: ‘sarabun-bolditalic’

my first guess is that the validation set doesn’t have the same classes as the train set because each one is in a different folder

Try running .summary on your datablock – this should help with diagnosing if it’s an issue with the data itself.

See the second section in the tutorial below to see this explained more fully: