22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

We use magic, just like that:

%run -i data_preparation/v0.py

%run -i model_configuration/v1.py

%run -i model_training/v0.py

Since we’re using the -i option, it works exactly as if we had copied the code from

the files into a cell and executed it.

Jupyter’s Magic Commands

You probably noticed the somewhat unusual %%writefile and %run

commands above. These are built-in magic commands. [50] A magic is a kind of

shortcut that extends a notebook’s capabilities.

We are using the following two magics to better organize our code:

• %%writefile [51] : As its name says, it writes the contents of the cell to a

file, but it does not run it, so we need to use yet another magic.

• %run [52] : It runs the named file inside the notebook as a program—but

independent of the rest of the notebook, so we need to use the -i

option to make all variables available, from both the notebook and the

file (technically speaking, the file is executed in IPython’s namespace).

In a nutshell, a cell containing one of our three fundamental parts will be

written to a versioned file inside the folder corresponding to that part.

In the example above, we write the cell to the data_preparation folder,

name it v0.py, and then execute it using the %run -i magic.

Model Configuration

We have seen plenty of this part: from defining parameters b and w manually, then

wrapping them up using the Module class, to using layers in a Sequential model.

We have also defined a loss function and an optimizer for our particular linear

regression model.

For the purpose of organizing our code, we’ll include the following elements in the

model configuration part:

Putting It All Together | 117

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!