Reputation: 2896
This is from the fastai library. So this function call:
md = ColumnarModelData(PATH, ColumnarDataset.from_data_frame(trn_df, cat_flds=cat_vars, y=trn_y),
ColumnarDataset.from_data_frame(val_df, cat_flds=cat_vars, y=val_y), bs=128, test_ds=test_ds)
vars(md)
Gives this result:
{'path': 'data/rossmann/',
'test_dl': <fastai.dataloader.DataLoader at 0x112c93d68>,
'trn_dl': <fastai.dataloader.DataLoader at 0x112c93e80>,
'val_dl': <fastai.dataloader.DataLoader at 0x112c93a20>}
But I can build the same result by doing:
md = {'path':PATH,
'test_dl':DataLoader(test_ds, batch_size=128, shuffle=False, num_workers=1),
'trn_dl':DataLoader(trn_df, batch_size=128, shuffle=False, num_workers=1),
'val_dl':DataLoader(val_df, batch_size=128*2, shuffle=False, num_workers=1)}
md
Which gives:
{'path': 'data/rossmann/',
'test_dl': <fastai.dataloader.DataLoader at 0x1c20e9cc88>,
'trn_dl': <fastai.dataloader.DataLoader at 0x1c20d5f8d0>,
'val_dl': <fastai.dataloader.DataLoader at 0x1c20d5f320>}
However they behave very differently when trying to use them in other functions. As in:
m = StructuredLearner(md, StructuredModel(to_gpu(model)), opt_fn=optim.Adam)
This runs fine when I use the initial method of md = ColumnarModelData()
but does not work when I build it on my own, gives this error:
AttributeError: 'dict' object has no attribute 'path'
What exactly is going wrong here?
Upvotes: 0
Views: 260
Reputation: 3491
You are converting a class to a dictionary using var(md) but md is an instance of a class rather than a dictionary. Classes can access their attributes using dot notation (for example md.path) but dictionaries cannot.
If you look at the Learner class (which StructuredLearner inherits from) you'll see these two lines:
self.data_,self.models,self.metrics = data,models,metrics
self.models_path = models_name if os.path.isabs(models_name) else os.path.join(self.data.path, models_name)
The key parts of that are:
self.data = data
and
self.data.path
So you can see it is trying to access the data (md) using dot notation.
If you really want to convert your dict to a class you can follow this: https://codeyarns.com/2017/02/27/how-to-convert-python-dict-to-class-object-with-fields/
or
Convert nested Python dict to object?
I'd recommend reading the docs, reading the library's code and creating your own small projects rather than trying to break something that is designed to work together into parts however it is up to you to figure out how you learn best.
Upvotes: 1