jeremy
(Jeremy Howard)
November 12, 2017, 5:08am
1
Thanks to the tip from @smortezavi , here’s a great quick intro to all the bits of Python and libraries you’ll need for this course: https://github.com/kuleshov/cs228-material/blob/master/tutorials/python/cs228-python-tutorial.ipynb
If anyone has questions or thoughts about Python stuff we cover, feel free to reply in this thread!
11 Likes
jakcycsl
(Chan Sooi Loong)
November 22, 2017, 1:34am
3
Still a beginner in Python and OOP, I have some questions on inheritance and OOP.
Looking at the code,
class ColumnarModelData(ModelData):
def __init__(self, path, trn_ds, val_ds, bs):
super().__init__(path, DataLoader(trn_ds, bs, shuffle=True, num_workers=1),
DataLoader(val_ds, bs*2, shuffle=False, num_workers=1))
The super() instantiate the parent class ModelData, but from the ModelData code snippet below, it doesn’t have DataLoader method. How does ModelData call the method? Is it when from .dataloader import DataLoader?
Summary
class ModelData():
def __init__(self, path, trn_dl, val_dl, test_dl=None):
self.path,self.trn_dl,self.val_dl,self.test_dl = path,trn_dl,val_dl,test_dl
@classmethod
def from_dls(cls, path,trn_dl,val_dl,test_dl=None):
trn_dl,val_dl = ModelDataLoader(trn_dl),ModelDataLoader(val_dl)
if test_dl: test_dl = ModelDataLoader(test_dl)
return cls(path, trn_dl, val_dl, test_dl)
@property
def is_reg(self): return self.trn_ds.is_reg
@property
def trn_ds(self): return self.trn_dl.dataset
@property
def val_ds(self): return self.val_dl.dataset
@property
def test_ds(self): return self.test_dl.dataset
@property
def trn_y(self): return self.trn_ds.y
@property
def val_y(self): return self.val_ds.y
lgvaz
(Lucas Goulart Vazquez)
November 22, 2017, 1:49am
4
When using super you are just “calling” a method from the parent class (in this case init).
DataLoader is just an argument being passed to this method, you can see that the base class expects to receive trn_dl and val_dl , so we’re actually doing:
trn_dl=DataLoader(trn_ds, bs, shuffle=True, num_workers=1)
val_dl=DataLoader(val_ds, bs*2, shuffle=False, num_workers=1)
1 Like
jakcycsl
(Chan Sooi Loong)
November 22, 2017, 2:10am
5
Got it. Thanks a lot for the explanation!