Show_batch(): "'Tensor' object has no attribute 'show_batch'"

Hi. Searched around for an answer to this before posting…

I’m trying to create a simple DataBunch for data that’s randomly generated on the fly. Even though I use the DataBunch.create() method on a valid PyTorch Dataset, when I call show_batch() I get the error “‘Tensor’ object has no attribute ‘show_batch’”…

from torch.utils import data

class RandomDataSet(data.Dataset):
  'PyTorch Dataset of randomly-generated data'
  def __init__(self,length=100):
      'Initialization'
      self.length = length

  def __len__(self):
    'Denotes the total number of samples. But ours is effectively infinite, so this is a dummy op'
    return self.length

  def __getitem__(self, index):
    'Generates one sample of data. Ignore the index b/c we generate new random data each time'
    x = np.random.uniform(size=1).astype(np.float32)
    y = x**2 + 5*x**3     # some function
    x, y =  map(torch.tensor, (x, y))
    return x, y
  
train_ds = RandomDataSet(10000)
valid_ds = RandomDataSet(1000)

data = DataBunch.create(train_ds, valid_ds, bs=20)

data.show_batch()

The full response is:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-115-920307f1526b> in <module>()
     23 data = DataBunch.create(train_ds, valid_ds, bs=20)
     24 
---> 25 data.show_batch()

/usr/local/lib/python3.6/dist-packages/fastai/basic_data.py in show_batch(self, rows, ds_type, **kwargs)
    121         if rows is None: rows = int(math.sqrt(len(b_idx)))
    122         ds = dl.dataset
--> 123         ds[0][0].show_batch(b_idx, rows, ds, **kwargs)
    124 
    125     def alt_show_batch(data, rows:int=None, ds_type:DatasetType=DatasetType.Train, **kwargs)->None:

AttributeError: 'Tensor' object has no attribute 'show_batch'

(I don’t understand why it would call show_batch() on a Dataset since AFAIK the only methods one defines are init, len and getitem. …?)

So…what does one need to do to fix this?

Thanks. BTW, apart from the above error, the DataBunch works fine and I can define a Learner, and run the lr_finder, and train with fit_one_cycle(), etc…

PS- If greater context is necessary, this is at the bottom of this Google Colab Notebook.

1 Like

You need to use fastai object to use all the fastai functionalities. The library doesn’t know how to show you a batch since you’re throwing it random object. I’m not going to detail where this behavior is implemented since it’s going to change later today as I’m refactoring things. Hoping to have the docs up to date by tonight or tomorrow!

It’s still going to be able to process the raw data and train properly however, yes.

1 Like

Great. Thanks! Once you do, I will gladly try to create some kind of show_batch() method for audio data, unless someone else has already written this.
Maybe a waveform graph with a “Play Audio” button underneath…