Skip to content
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
Requirement already satisfied: openml-pytorch in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages/openml_pytorch-0.0.5-py3.9.egg (0.0.5)
Requirement already satisfied: openml in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from openml-pytorch) (0.13.1)
Requirement already satisfied: torch<2.2.0,>=1.4.0 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from openml-pytorch) (2.1.2)
Requirement already satisfied: onnx in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from openml-pytorch) (1.16.0)
Requirement already satisfied: torchvision in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from openml-pytorch) (0.16.2)
Requirement already satisfied: typing-extensions in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from torch<2.2.0,>=1.4.0->openml-pytorch) (4.11.0)
Requirement already satisfied: sympy in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from torch<2.2.0,>=1.4.0->openml-pytorch) (1.12)
Requirement already satisfied: filelock in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from torch<2.2.0,>=1.4.0->openml-pytorch) (3.12.0)
Requirement already satisfied: jinja2 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from torch<2.2.0,>=1.4.0->openml-pytorch) (3.1.3)
Requirement already satisfied: networkx in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from torch<2.2.0,>=1.4.0->openml-pytorch) (3.2.1)
Requirement already satisfied: fsspec in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from torch<2.2.0,>=1.4.0->openml-pytorch) (2023.6.0)
Requirement already satisfied: protobuf>=3.20.2 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from onnx->openml-pytorch) (5.26.1)
Requirement already satisfied: numpy>=1.20 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from onnx->openml-pytorch) (1.24.2)
Requirement already satisfied: liac-arff>=2.4.0 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from openml->openml-pytorch) (2.5.0)
Requirement already satisfied: xmltodict in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from openml->openml-pytorch) (0.13.0)
Requirement already satisfied: requests in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from openml->openml-pytorch) (2.28.2)
Requirement already satisfied: scikit-learn>=0.18 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from openml->openml-pytorch) (1.2.2)
Requirement already satisfied: python-dateutil in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from openml->openml-pytorch) (2.8.2)
Requirement already satisfied: pandas>=1.0.0 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from openml->openml-pytorch) (1.5.3)
Requirement already satisfied: scipy>=0.13.3 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from openml->openml-pytorch) (1.10.1)
Requirement already satisfied: minio in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from openml->openml-pytorch) (7.1.13)
Requirement already satisfied: pyarrow in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from openml->openml-pytorch) (11.0.0)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from torchvision->openml-pytorch) (10.3.0)
Requirement already satisfied: pytz>=2020.1 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from pandas>=1.0.0->openml->openml-pytorch) (2022.7.1)
Requirement already satisfied: six>=1.5 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from python-dateutil->openml->openml-pytorch) (1.16.0)
Requirement already satisfied: threadpoolctl>=2.0.0 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from scikit-learn>=0.18->openml->openml-pytorch) (3.1.0)
Requirement already satisfied: joblib>=1.1.1 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from scikit-learn>=0.18->openml->openml-pytorch) (1.2.0)
Requirement already satisfied: MarkupSafe>=2.0 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from jinja2->torch<2.2.0,>=1.4.0->openml-pytorch) (2.1.5)
Requirement already satisfied: urllib3 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from minio->openml->openml-pytorch) (1.26.15)
Requirement already satisfied: certifi in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from minio->openml->openml-pytorch) (2022.12.7)
Requirement already satisfied: idna<4,>=2.5 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from requests->openml->openml-pytorch) (3.4)
Requirement already satisfied: charset-normalizer<4,>=2 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from requests->openml->openml-pytorch) (3.1.0)
Requirement already satisfied: mpmath>=0.19 in /Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages (from sympy->torch<2.2.0,>=1.4.0->openml-pytorch) (1.3.0)

[notice] A new release of pip is available: 23.0.1 -> 24.0
[notice] To update, run: pip install --upgrade pip

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)
/Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
/Users/eragon/.pyenv/versions/3.9.19/envs/openml/lib/python3.9/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet18_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet18_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)

# 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) -&gt; 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)
/Users/eragon/Documents/CODE/Github/openml-pytorch/openml_pytorch/extension.py:154: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df.loc[:, 'encoded_labels'] = label_encoder.transform(y)
/Users/eragon/Documents/CODE/Github/openml-pytorch/openml_pytorch/extension.py:154: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df.loc[:, 'encoded_labels'] = label_encoder.transform(y)
/Users/eragon/Documents/CODE/Github/openml-pytorch/openml_pytorch/extension.py:154: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df.loc[:, 'encoded_labels'] = label_encoder.transform(y)
/Users/eragon/Documents/CODE/Github/openml-pytorch/openml_pytorch/extension.py:154: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df.loc[:, 'encoded_labels'] = label_encoder.transform(y)
/Users/eragon/Documents/CODE/Github/openml-pytorch/openml_pytorch/extension.py:154: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df.loc[:, 'encoded_labels'] = label_encoder.transform(y)
/Users/eragon/Documents/CODE/Github/openml-pytorch/openml_pytorch/extension.py:154: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df.loc[:, 'encoded_labels'] = label_encoder.transform(y)
/Users/eragon/Documents/CODE/Github/openml-pytorch/openml_pytorch/extension.py:154: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df.loc[:, 'encoded_labels'] = label_encoder.transform(y)
/Users/eragon/Documents/CODE/Github/openml-pytorch/openml_pytorch/extension.py:154: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df.loc[:, 'encoded_labels'] = label_encoder.transform(y)
/Users/eragon/Documents/CODE/Github/openml-pytorch/openml_pytorch/extension.py:154: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df.loc[:, 'encoded_labels'] = label_encoder.transform(y)
/Users/eragon/Documents/CODE/Github/openml-pytorch/openml_pytorch/extension.py:154: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df.loc[:, 'encoded_labels'] = label_encoder.transform(y)

import openml
print(run.flow)
OpenML Flow
===========
Flow Name.......: torch.nn.ResNet.73f8a33b44a6743
Flow Description: Automatically created pytorch flow.
Dependencies....: torch==2.1.2
numpy>=1.6.1
scipy>=0.9

# 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))