You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

0 Días
8 Hrs
52 Min
29 Seg

Entrenamiento y visualización de pérdida

10/24
Resources

How to initialize the training of a linear regression model?

Initializing the training of a linear regression model is a critical step in creating an efficient and accurate model. This process not only makes it easier to fit the model to our data but also to monitor and adjust performance on the fly. In this context, we will use PyTorch's Inference Mode to ensure efficient resource management, and Colab to run our code.

Here I will guide you on how to carry out this process effectively, controlling the loss process and visualizing the training evolution.

How to handle common errors when defining the inference mode?

To minimize resource usage during training, it is essential to properly implement PyTorch's Inference Mode. This is achieved by encapsulating the code under the context of torch.inference_mode() which disables any gradient computation, thus optimizing computational resources.

Python

with torch.inference_mode(): # Training code

How to visualize the training progress?

An essential step is to monitor the training progress. This can be accomplished by recording training and validation losses in lists and subsequently plotting this data.

# We initialize the lists to save the losstraining_loss = []test_loss = []
 # Code to save the lossestraining_loss.append(loss_train.detach().numpy())test_loss.append(loss_test.detach().numpy())

How to print information during training?

When setting up diagnostic information during training, you can decide how many times you want to receive updates. A common approach is to print the results every certain number of epochs, for example:

if epoch % 10 == 0: print(f "Epoch {epoch} | Training loss: {loss_train:.4f} | Test loss: {loss_test:.4f}").

How to plot the loss reduction throughout the training?

To observe how the losses decrease over time, it is recommended to use the matplotlib library. By plotting, you can clearly see the performance of the model and decide if you need to adjust the number of epochs.

import matplotlib.pyplot as plt
plt.plot(training_loss,  label='Training Loss')plt.plot(test_loss,  label='Test Loss')plt.ylabel('Loss')plt.xlabel('Epoch')plt.legend()plt.show()

How many epochs are needed?

The number of epochs is crucial to improve the model. Although in some initial configurations it may be sufficient to train with 100 epochs, it is often found that a larger number can continue to improve the accuracy of the model. Experimentation is recommended to find the optimal number.

# Increase epochs:for epoch in range(200): # Increase the range for a better fit # Training code

By following these steps, you will not only be able to effectively train a linear regression model, but you will also have a deeper and more visual understanding of the optimization process. Keep experimenting and adjust the parameters to achieve a more robust and accurate model - don't stop in your learning process!

Contributions 4

Questions 1

Sort by:

Want to see more contributions, questions and answers from the community?

Chicos los valores que se muestran a partir del minuto 4:22 son valores mal calculados. Con esos valores no obtendrán la forma de la gráfica mostrada en el video debido a que las diferencias entre las perdidas no son tan marcadas, la forma será más parecida a una linea recta en vez de la linea curva como nos muestra el profesor. Este problema surge debido a un error de indentación en la celda, revisen el notebook de ejemplo en la sección de recursos para tener una guía de donde debe ir cada línea de código. Espero les sirva!

Asi me sale con 300 épocas: Las perdidas llegan a su punto minimo y a la vez se cortan aproximadamente en el # epoca : 260, despues de ello deja de tener sentido seguir aumentando el # epocas. ![](https://static.platzi.com/media/user_upload/image-5a846ee4-f6c3-404a-acfb-5ae0aece7158.jpg)
![]()![](https://static.platzi.com/media/user_upload/grafica-f132ef38-5fde-4811-a268-c618b1910334.jpg)En mi caso después de la época 150 ya deja de mejorar tanto.
Me llama la atencion, el comportamiento de las funcion de perdida en training y test. Hay un espacio entre ellas. Personalmente, diria que hay presencia de underfiting. Para mejorar el modelo, necesitamos extender los epochs para que en algun momento detectar si hay convergencia del training loss y testing loss