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.

Model Training

We start by instantiating the StepByStep class with the corresponding arguments.

Next, we set its loaders using the appropriately named method set_loaders().

Then, we set up an interface with TensorBoard and name our experiment classy

(what else could it be?!).

Notebook Cell 2.1.1

1 sbs = StepByStep(model, loss_fn, optimizer)

2 sbs.set_loaders(train_loader, val_loader)

3 sbs.set_tensorboard('classy')

One important thing to notice is that the model attribute of the sbs object is the

same object as the model variable created in the model configuration. It is not a

copy! We can easily verify this:

print(sbs.model == model)

print(sbs.model)

Output

True

Sequential(

(0): Linear(in_features=1, out_features=1, bias=True)

)

As expected, the equality holds. If we print the model itself, we get our simple one

input-one output model.

Let’s train the model now, using the same 200 epochs as before:

Notebook Cell 2.1.2

1 sbs.train(n_epochs=200)

Done! It is trained! Really? Really! Let’s check it out:

Classy Pipeline | 197

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

Saved successfully!

Ooh no, something went wrong!