Blogs / Getting Started with Google Colab for Training Deep Learning Models

Getting Started with Google Colab for Training Deep Learning Models

August 17, 2024

آشنایی با Google Colab برای آموزش مدل‌های یادگیری عمیق

Introduction

With rapid advances in deep learning and large-scale data processing, using powerful computational tools to train deep learning models has become essential. Google Colab, a web-based computational environment provided by Google, is known as a free platform for writing and running machine learning and deep learning code. This article explores how to use Google Colab to train deep learning models, its advantages and features, the steps you should follow, and practical tips for getting the most out of this tool.

1. Overview of Google Colab

Google Colab (Colaboratory) is an online development environment that allows users to run Python code directly in their web browser without installing complex software or expensive hardware. Key features include:
  • Web-based computation: Write and execute Python code in your browser.
  • Free GPU and TPU access: Colab lets you use graphics processing units (GPU) and tensor processing units (TPU) at no cost.
  • Integration with Google Drive: Easily load and save files from your Google Drive.
  • Built-in ML libraries: Pre-installed popular deep learning libraries such as TensorFlow, Keras, and PyTorch.

2. Setting Up Google Colab

To get started with Google Colab, follow these steps:

2.1. Sign in to Colab

  1. Go to the Google Colab website.
  2. Sign in with your Google account.

2.2. Create a New Notebook

  1. Click “New Notebook” to create a fresh Colab notebook.
  2. Give your notebook a descriptive name for easier management.

2.3. Configure the Runtime

  1. Under the “Runtime” menu, select “Change runtime type.”
  2. In “Hardware accelerator,” choose “GPU” or “TPU,” then click “Save.”

3. Uploading and Preprocessing Data

Before training your deep learning models, upload and preprocess your data.

3.1. Mounting Google Drive

  1. Use the following code to mount your Drive:
    from google.colab import drive
    drive.mount('/content/drive')
  2. Follow the authentication link and enter the code to access your Drive files.

3.2. Loading Data from Online Sources

If your data is available online, you can load it directly:
import pandas as pd
url = 'https://example.com/data.csv'
data = pd.read_csv(url)

3.3. Data Preprocessing

Common preprocessing steps include:
  • Cleaning: Remove missing values and correct errors.
  • Scaling: Normalize features so the model can learn effectively.
  • Train-Test Split: Divide data into training and test sets:
    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

4. Training Deep Learning Models

In this section, we’ll cover model training using Google Colab.

4.1. Choosing a Framework

Colab comes with TensorFlow, Keras, and PyTorch pre-installed. You can use any of these libraries to build and train your models.

4.2. Training with TensorFlow and Keras

  1. Build the model:
    import tensorflow as tf
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense
    
    model = Sequential([
        Dense(64, activation='relu', input_shape=(input_dim,)),
        Dense(32, activation='relu'),
        Dense(num_classes, activation='softmax')
    ])
  2. Compile the model:
    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
  3. Train the model:
    history = model.fit(X_train, y_train,
                            epochs=10,
                            validation_split=0.2)
  4. Evaluate the model:
    test_loss, test_acc = model.evaluate(X_test, y_test)
    print(f'Test Accuracy: {test_acc}')

4.3. Training with PyTorch

  1. Define the model:
    import torch
    import torch.nn as nn
    import torch.optim as optim
    
    class SimpleNN(nn.Module):
        def __init__(self):
            super(SimpleNN, self).__init__()
            self.fc1 = nn.Linear(input_dim, 64)
            self.fc2 = nn.Linear(64, 32)
            self.fc3 = nn.Linear(32, num_classes)
    
        def forward(self, x):
            x = torch.relu(self.fc1(x))
            x = torch.relu(self.fc2(x))
            return self.fc3(x)
    
    model = SimpleNN()
  2. Loss and optimizer:
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=0.001)
  3. Train the model:
    for epoch in range(10):
        model.train()
        optimizer.zero_grad()
        outputs = model(X_train)
        loss = criterion(outputs, y_train)
        loss.backward()
        optimizer.step()
  4. Evaluate the model:
    model.eval()
    with torch.no_grad():
        outputs = model(X_test)
        _, predicted = torch.max(outputs, 1)
        accuracy = (predicted == y_test).sum().item() / y_test.size(0)
    print(f'Test Accuracy: {accuracy}')

5. Saving and Loading Models

After training, you may want to save and later reload your models.

5.1. Saving

  • TensorFlow/Keras:
    model.save('my_model.h5')
  • PyTorch:
    torch.save(model.state_dict(), 'my_model.pth')

5.2. Loading

  • TensorFlow/Keras:
    from tensorflow.keras.models import load_model
    model = load_model('my_model.h5')
  • PyTorch:
    model = SimpleNN()
    model.load_state_dict(torch.load('my_model.pth'))

6. Sharing and Collaboration

Colab allows you to share notebooks and collaborate in real time:
  1. Click the “Share” button at the top right of the notebook.
  2. Adjust access settings and share the link with collaborators.

Conclusion

Google Colab is a powerful, free platform for training deep learning models, offering features such as a web-based environment, free GPU/TPU access, Google Drive integration, and pre-installed ML libraries. Colab streamlines model building, training, evaluation, and sharing—making it an ideal choice for data scientists and researchers working with complex datasets and deep learning workflows.