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

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

piece of code that’s going to be used repeatedly into its own function: the minibatchinner loop!The inner loop depends on three elements:• the device where data is being sent• a data loader to draw mini-batches from• a step function, returning the corresponding lossTaking these elements as inputs and using them to perform the inner loop, we’llend up with a function like this:Helper Function #21 def mini_batch(device, data_loader, step_fn):2 mini_batch_losses = []3 for x_batch, y_batch in data_loader:4 x_batch = x_batch.to(device)5 y_batch = y_batch.to(device)67 mini_batch_loss = step_fn(x_batch, y_batch)8 mini_batch_losses.append(mini_batch_loss)910 loss = np.mean(mini_batch_losses)11 return lossIn the last section, we realized that we were executing five times more updates(the train_step_fn() function) per epoch due to the mini-batch inner loop. Before,1,000 epochs meant 1,000 updates. Now, we only need 200 epochs to perform thesame 1,000 updates.What does our training loop look like now? It’s very lean!Run - Data Preparation V1, Model Configuration V1%run -i data_preparation/v1.py%run -i model_configuration/v1.pyDataLoader | 143

Define - Model Training V31 %%writefile model_training/v3.py23 # Defines number of epochs4 n_epochs = 20056 losses = []78 for epoch in range(n_epochs):9 # inner loop10 loss = mini_batch(device, train_loader, train_step_fn) 111 losses.append(loss)1 Performing mini-batch gradient descentRun - Model Training V3%run -i model_training/v3.pyAfter updating the model training part, our current state ofdevelopment is:• Data Preparation V1• Model Configuration V1• Model Training V3Let’s inspect the model’s state:# Checks model's parametersprint(model.state_dict())OutputOrderedDict([('0.weight', tensor([[1.9687]], device='cuda:0')),('0.bias', tensor([1.0236], device='cuda:0'))])So far, we’ve focused on the training data only. We built a dataset and a data loader144 | Chapter 2: Rethinking the Training Loop

Define - Model Training V3

1 %%writefile model_training/v3.py

2

3 # Defines number of epochs

4 n_epochs = 200

5

6 losses = []

7

8 for epoch in range(n_epochs):

9 # inner loop

10 loss = mini_batch(device, train_loader, train_step_fn) 1

11 losses.append(loss)

1 Performing mini-batch gradient descent

Run - Model Training V3

%run -i model_training/v3.py

After updating the model training part, our current state of

development is:

• Data Preparation V1

• Model Configuration V1

• Model Training V3

Let’s inspect the model’s state:

# Checks model's parameters

print(model.state_dict())

Output

OrderedDict([('0.weight', tensor([[1.9687]], device='cuda:0')),

('0.bias', tensor([1.0236], device='cuda:0'))])

So far, we’ve focused on the training data only. We built a dataset and a data loader

144 | Chapter 2: Rethinking the Training Loop

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

Saved successfully!

Ooh no, something went wrong!