Putting It All Together

Combine all the processes together, and see the final output.

Linear regression using PyTorch complete steps

We have covered a lot of ground so far. From coding a linear regression in Numpy using gradient descent to transforming it into a PyTorch model, this is all done step-by-step.

It is time to put it all together and organize our code into three fundamental parts, namely:

  • Data preparation (not data generation)

  • Model configuration

  • Model training

Let us tackle these three parts in order.

Data preparation

To be honest, there isn’t much data preparation at this point. After generating our data points in the data generation lesson, the only preparation step performed so far was transforming Numpy arrays into PyTorch tensors, which can be seen below:

Press + to interact
%%writefile data_preparation/v0.py
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# Our data was in Numpy arrays, but we need to transform them
# into PyTorch's Tensors and then we send them to the
# chosen device
x_train_tensor = torch.as_tensor(x_train).float().to(device)
y_train_tensor = torch.as_tensor(y_train).float().to(device)

Then, the following command is needed to run the named file in the data preparation folder:

Press + to interact
%run -i data_preparation/v0.py

This part will get much more interesting in the next chapter when we get to use Dataset ...

Access this course and 1400+ top-rated courses and projects.