Learn.show_results show the same examples all the time for validation set

Hi everyone, I’m new to fastai so maybe I’m missing something obvious, but so far I havent found answer either in documentation, sourcecode or in this forums.

If I run:

learn.show_results(ds_type = fai.basics.DatasetType.Train)

I get different result (different examples) every time.

But if I use Valid or leave ds_type as default (which is Valid) I get the same examples over and over.

I traced the reason to DataBunch.one_batch() which produces the same batch for validation on every run (but different ones for train) but that’s as far as I managed to follow the trail until got completely lost in the code and cant figure out why it works that way.

Can someone help out with figuring out what I have to change in one_batch() or in some function called by it to change this behaviour? The reason for this is that when I show results of my model it keeps showing the same triplets of images (its a autoencoder so I have input Image, reconstructed Image and copy of input image shown) every time when I want to see how it behaves on other examples without having to go through hoops of predicting with model and generating images etc…

I’m using version ‘1.0.54’.

Thank You in advance


The training dataloader is suffled at every epoch, which means it’s shuffled each time you call one_batch, but the validation dataloader is not.

1 Like

Thank You for the response - Thanks to You I’ve found that the initialisation is done in DataBunch.create() and I’ve monkey patched my version of create with following line changed:

 dls = [DataLoader(d, b, shuffle=s, drop_last=s, num_workers=num_workers, **dl_kwargs) for d,b,s in
       zip(datasets, (bs,val_bs,val_bs,val_bs), (True,True,True,True)) if d is not None]

This fixed my problem. Now learn.show_result() gives me different batch every time

From what I gather its impossible to change sampler (random vs sequential sampling) after the creation of dataloader (thats at least what error message tells me - probably possible to circumvent the default behaviour but my python-fu is not strong enough) so If I want to use learn.show_results() and see different batch every time its not gonna be as easy as providing new parameter for show_results and change its behaviour depending on the flag value because the dataloader is already created.

There have to be easier approach to this (easier than changing the create method od Databanch) - is there a simple way I’m missing to see the results of model on examples from validation set but change the batch I’m looking at (eaither randomly or deterministically by say providing index of batch). Am I missing something obvious here?

You can’t change suffle/sampler of a DataLoader after creation but fastai has a neat monkey-patch to let you do so: just type

data.valid_dl = data.valid_dl.new(shuffle=True)

when you want to shuffle the validation set. Then

data.valid_dl = data.valid_dl.new(shuffle=False)

to return on the default behavior.


Thank You :slight_smile: