Python tips and questions

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

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

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

Got it. Thanks a lot for the explanation!