from IPython.display import display, HTML, Markdown
import os
import yaml
with open("../../../mkdocs.yml", "r") as f:
load_config = yaml.safe_load(f)
repo_url = load_config["repo_url"].replace("https://github.com/", "")
binder_url = load_config["binder_url"]
relative_file_path = "integrations/Pytorch/basic_tutorial.ipynb"
display(HTML(f"""<a href="https://colab.research.google.com/github/{repo_url}/{relative_file_path}" target="_blank">
<img alt="Open In Colab" src="https://colab.research.google.com/assets/colab-badge.svg"/>
</a>"""))
display(Markdown("[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/SubhadityaMukherjee/openml_docs/HEAD?labpath=Scikit-learn%2Fdatasets_tutorial)"))
!pip install openml-pytorch
PyTorch sequential classification model example¶
An example of a sequential network that classifies digit images used as an OpenML flow. We use sub networks here in order to show the that network hierarchies can be achieved with ease.
import torch.optim
import openml
import openml_pytorch
import openml_pytorch.layers
import openml_pytorch.config
import logging
import torch.nn as nn
import torch.nn.functional as F
from openml import OpenMLTask
import torchvision.models as models
# Enable logging in order to observe the progress while running the example.
openml.config.logger.setLevel(logging.DEBUG)
openml_pytorch.config.logger.setLevel(logging.DEBUG)
# Load the pre-trained ResNet model
model = models.resnet18(pretrained=True, progress=True)
# Modify the last fully connected layer to the required number of classes
num_classes = 20 # For the dataset we are using
in_features = model.fc.in_features
model.fc = nn.Linear(in_features, num_classes)
# Optional: If you're fine-tuning, you may want to freeze the pre-trained layers
for param in model.parameters():
param.requires_grad = False
# If you want to train the last layer only (the newly added layer)
for param in model.fc.parameters():
param.requires_grad = True
# Setting an appropriate optimizer
def custom_optimizer_gen(model: torch.nn.Module, task: OpenMLTask) -> torch.optim.Optimizer:
return torch.optim.Adam(model.fc.parameters())
openml_pytorch.config.optimizer_gen = custom_optimizer_gen
# Download the OpenML task for the Meta_Album_PNU_Micro dataset.
task = openml.tasks.get_task(361152)
############################################################################
# Run the model on the task (requires an API key).m
run = openml.runs.run_model_on_task(model, task, avoid_duplicate_runs=False)
import openml
print(run.flow)
# Publish the experiment on OpenML (optional, requires an API key).
run.publish()
print('URL for run: %s/run/%d' % (openml.config.server, run.run_id))