Getting error TypeError: 'float' object is not iterable when creating Dataloaders

dblock = DataBlock(blocks=TextBlock.from_df('Sequence', is_lm=True, tok=SentencePieceTokenizer(sp_model=sp_model), vocab=vocab),
                   get_x=ColReader('text'),
                   splitter=RandomSplitter())

dls = dblock.dataloaders(seqs, bs=64, seq_len=72)
--------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-ec664a1ca6de> in <module>
----> 1 dls = dblock.dataloaders(seqs, bs=64, seq_len=72)

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/data/block.py in dataloaders(self, source, path, verbose, **kwargs)
    113         dsets = self.datasets(source)
    114         kwargs = {**self.dls_kwargs, **kwargs, 'verbose': verbose}
--> 115         return dsets.dataloaders(path=path, after_item=self.item_tfms, after_batch=self.batch_tfms, **kwargs)
    116 
    117     _docs = dict(new="Create a new `DataBlock` with other `item_tfms` and `batch_tfms`",

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/data/core.py in dataloaders(self, bs, val_bs, shuffle_train, n, path, dl_type, dl_kwargs, device, **kwargs)
    211         if dl_type is None: dl_type = self._dl_type
    212         drop_last = kwargs.pop('drop_last', shuffle_train)
--> 213         dl = dl_type(self.subset(0), bs=bs, shuffle=shuffle_train, drop_last=drop_last, n=n, device=device,
    214                      **merge(kwargs, dl_kwargs[0]))
    215         dls = [dl] + [dl.new(self.subset(i), bs=(bs if val_bs is None else val_bs), shuffle=False, drop_last=False,

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/text/data.py in __init__(self, dataset, lens, cache, bs, seq_len, num_workers, **kwargs)
     75         self.seq_len = seq_len
     76         if lens is None: lens = _get_lengths(dataset)
---> 77         if lens is None: lens = [len(o) for o in self.items]
     78         self.lens = ReindexCollection(lens, idxs=self.items.idxs)
     79         # The "-1" is to allow for final label, we throw away the end that's less than bs

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/text/data.py in <listcomp>(.0)
     75         self.seq_len = seq_len
     76         if lens is None: lens = _get_lengths(dataset)
---> 77         if lens is None: lens = [len(o) for o in self.items]
     78         self.lens = ReindexCollection(lens, idxs=self.items.idxs)
     79         # The "-1" is to allow for final label, we throw away the end that's less than bs

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/xtras.py in <genexpr>(.0)
     84 class IterLen:
     85     "Base class to add iteration to anything supporting `__len__` and `__getitem__`"
---> 86     def __iter__(self): return (self[i] for i in range_of(self))
     87 
     88 # Cell

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/xtras.py in __getitem__(self, i)
     97 
     98     def _get(self, i): return self.tfm(self.coll[i])
---> 99     def __getitem__(self, i): return self._get(self.idxs[i])
    100     def __len__(self): return len(self.coll)
    101     def reindex(self, idxs): self.idxs = idxs

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/xtras.py in _get(self, i)
     96         if cache is not None: self._get = functools.lru_cache(maxsize=cache)(self._get)
     97 
---> 98     def _get(self, i): return self.tfm(self.coll[i])
     99     def __getitem__(self, i): return self._get(self.idxs[i])
    100     def __len__(self): return len(self.coll)

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/data/core.py in __getitem__(self, it)
    312 
    313     def __getitem__(self, it):
--> 314         res = tuple([tl[it] for tl in self.tls])
    315         return res if is_indexer(it) else list(zip(*res))
    316 

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/data/core.py in <listcomp>(.0)
    312 
    313     def __getitem__(self, it):
--> 314         res = tuple([tl[it] for tl in self.tls])
    315         return res if is_indexer(it) else list(zip(*res))
    316 

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/data/core.py in __getitem__(self, idx)
    278         res = super().__getitem__(idx)
    279         if self._after_item is None: return res
--> 280         return self._after_item(res) if is_indexer(idx) else res.map(self._after_item)
    281 
    282 # Cell

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/data/core.py in _after_item(self, o)
    240         return super()._new(items, tfms=self.tfms, do_setup=False, types=self.types, split_idx=split_idx, **kwargs)
    241     def subset(self, i): return self._new(self._get(self.splits[i]), split_idx=i)
--> 242     def _after_item(self, o): return self.tfms(o)
    243     def __repr__(self): return f"{self.__class__.__name__}: {self.items}\ntfms - {self.tfms.fs}"
    244     def __iter__(self): return (self[i] for i in range(len(self)))

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/transform.py in __call__(self, o)
    196         self.fs.append(t)
    197 
--> 198     def __call__(self, o): return compose_tfms(o, tfms=self.fs, split_idx=self.split_idx)
    199     def __repr__(self): return f"Pipeline: {' -> '.join([f.name for f in self.fs if f.name != 'noop'])}"
    200     def __getitem__(self,i): return self.fs[i]

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/transform.py in compose_tfms(x, tfms, is_enc, reverse, **kwargs)
    148     for f in tfms:
    149         if not is_enc: f = f.decode
--> 150         x = f(x, **kwargs)
    151     return x
    152 

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/transform.py in __call__(self, x, **kwargs)
     71     @property
     72     def name(self): return getattr(self, '_name', _get_name(self))
---> 73     def __call__(self, x, **kwargs): return self._call('encodes', x, **kwargs)
     74     def decode  (self, x, **kwargs): return self._call('decodes', x, **kwargs)
     75     def __repr__(self): return f'{self.name}:\nencodes: {self.encodes}decodes: {self.decodes}'

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/transform.py in _call(self, fn, x, split_idx, **kwargs)
     81     def _call(self, fn, x, split_idx=None, **kwargs):
     82         if split_idx!=self.split_idx and self.split_idx is not None: return x
---> 83         return self._do_call(getattr(self, fn), x, **kwargs)
     84 
     85     def _do_call(self, f, x, **kwargs):

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/transform.py in _do_call(self, f, x, **kwargs)
     87             if f is None: return x
     88             ret = f.returns_none(x) if hasattr(f,'returns_none') else None
---> 89             return retain_type(f(x, **kwargs), x, ret)
     90         res = tuple(self._do_call(f, x_, **kwargs) for x_ in x)
     91         return retain_type(res, x)

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastcore/dispatch.py in __call__(self, *args, **kwargs)
    115         elif self.inst is not None: f = MethodType(f, self.inst)
    116         elif self.owner is not None: f = MethodType(f, self.owner)
--> 117         return f(*args, **kwargs)
    118 
    119     def __get__(self, inst, owner):

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/text/data.py in encodes(self, o)
     46             self.o2i = defaultdict(int, {v:k for k,v in enumerate(self.vocab) if v != 'xxfake'})
     47 
---> 48     def encodes(self, o): return TensorText(tensor([self.o2i  [o_] for o_ in o]))
     49     def decodes(self, o): return L(self.vocab[o_] for o_ in o)
     50 

TypeError: 'float' object is not iterable
1 Like

I’m having the same issue with fastai==2.1.5 in Colab, but using TextDataLoaders.from_df() in my code. I created an issue on GitHub.


UPDATE: This appears to be an issue with fastcore and NOT fastai. I was able to fix the issue in my notebook by pinning earlier versions.

!pip install fastai==2.1.4
!pip install fastcore==1.3.1

—-
UPDATE #2: turns out it was a fastai issue after all. A change to tokenize_df was the culprit. This has been fixed in 2.1.6.

Hi neuradai

Thanks for the reply. I have done

!pip install fastai==2.1.4
!pip install fastcore==1.3.1
from fastai import *
from fastcore import *
from fastai.text.all import *

and rerun my code. now I am getting an error saying

RuntimeError: stack expects each tensor to be equal size, but got [26] at entry 0 and [164] at entry 1

When I run .summary() on my datablock I get

collating items in a batch
Error! It's not possible to collate your items in a batch
Could not collate the 0-th members of your tuples because got the following shapes
torch.Size([26]),torch.Size([164]),torch.Size([59]),torch.Size([21])

Do you know why this might be?

Thanks

Looks like you probably need to apply the padding transform to your datablock. Take a look at the pad_input and pad_input_chunk functions in fastai.text.data

There’s an open issue re: learn.summary() that talks about some sort of incompatibility with an update to pytorch. See below.

I’m still getting the same error both with 2.1.4 and 2.1.6

I had the same error and I fixed with:
!pip install fastai==2.1.8