Model Training & Evaluations
Now, we built a training loop for 50 epochs. In the provided code snippet, the model processes mini batches of training data and compute loss and update the parameters.
Python3
num_epochs = 50 train_hist = [] test_hist = [] # Training loop for epoch in range (num_epochs): total_loss = 0.0 # Training model.train() for batch_X, batch_y in train_loader: batch_X, batch_y = batch_X.to(device), batch_y.to(device) predictions = model(batch_X) loss = loss_fn(predictions, batch_y) optimizer.zero_grad() loss.backward() optimizer.step() total_loss + = loss.item() # Calculate average training loss and accuracy average_loss = total_loss / len (train_loader) train_hist.append(average_loss) # Validation on test data model. eval () with torch.no_grad(): total_test_loss = 0.0 for batch_X_test, batch_y_test in test_loader: batch_X_test, batch_y_test = batch_X_test.to(device), batch_y_test.to(device) predictions_test = model(batch_X_test) test_loss = loss_fn(predictions_test, batch_y_test) total_test_loss + = test_loss.item() # Calculate average test loss and accuracy average_test_loss = total_test_loss / len (test_loader) test_hist.append(average_test_loss) if (epoch + 1 ) % 10 = = 0 : print (f 'Epoch [{epoch+1}/{num_epochs}] - Training Loss: {average_loss:.4f}, Test Loss: {average_test_loss:.4f}' ) |
Output:
Epoch [10/50] - Training Loss: 0.0000, Test Loss: 0.0002
Epoch [20/50] - Training Loss: 0.0000, Test Loss: 0.0002
Epoch [30/50] - Training Loss: 0.0000, Test Loss: 0.0002
Epoch [40/50] - Training Loss: 0.0000, Test Loss: 0.0002
Epoch [50/50] - Training Loss: 0.0000, Test Loss: 0.0002
Plotting the Learning Curve
We have plotted the learning curve to track the progress and give us an idea, how much time time and training is required by the model to understand the patterns.
Python3
x = np.linspace( 1 ,num_epochs,num_epochs) plt.plot(x,train_hist,scalex = True , label = "Training loss" ) plt.plot(x, test_hist, label = "Test loss" ) plt.legend() plt.show() |
Output:
Time Series Forecasting using Pytorch
Time series forecasting plays a major role in data analysis, with applications ranging from anticipating stock market trends to forecasting weather patterns. In this article, we’ll dive into the field of time series forecasting using PyTorch and LSTM (Long Short-Term Memory) neural networks. We’ll uncover the critical preprocessing procedures that underpin the accuracy of our forecasts along the way.
Table of Content
- Time Series Forecasting
- Implementation of Time Series Forecasting:
- Step 1: Import the necessary libraries
- Step2: Loading the Dataset
- Step 3: Data Preprocessing
- Step 4: Define LSTM class model
- Step 5: Creating Data Loader for batch training
- Step 6: Model Training & Evaluations
- Step 7: Forecasting