Sequential classification¶
- Sequential classification of a tabular MNIST dataset (Task 3573) using a simple neural network.
In [ ]:
Copied!
import torch.nn
import torch.optim
import openml_pytorch.config
import openml
import logging
import warnings
# Suppress FutureWarning messages
warnings.simplefilter(action='ignore')
############################################################################
# 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)
############################################################################
import torch.nn
import torch.optim
import openml_pytorch.config
import openml
import logging
import warnings
# Suppress FutureWarning messages
warnings.simplefilter(action='ignore')
############################################################################
# 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)
############################################################################
In [ ]:
Copied!
from openml_pytorch.trainer import OpenMLTrainerModule
from openml_pytorch.trainer import OpenMLDataModule
from openml_pytorch.trainer import OpenMLTrainerModule
from openml_pytorch.trainer import OpenMLDataModule
Define the Model¶
In [ ]:
Copied!
############################################################################
# Define a sequential network that does the initial image reshaping
# and normalization model.
processing_net = torch.nn.Sequential(
openml_pytorch.layers.Functional(function=torch.Tensor.reshape,
shape=(-1, 1, 28, 28)),
torch.nn.BatchNorm2d(num_features=1)
)
############################################################################
############################################################################
# Define a sequential network that does the extracts the features from the
# image.
features_net = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=1, out_channels=32, kernel_size=5),
torch.nn.LeakyReLU(),
torch.nn.MaxPool2d(kernel_size=2),
torch.nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5),
torch.nn.LeakyReLU(),
torch.nn.MaxPool2d(kernel_size=2),
)
############################################################################
############################################################################
# Define a sequential network that flattens the features and compiles the
# results into probabilities for each digit.
results_net = torch.nn.Sequential(
openml_pytorch.layers.Functional(function=torch.Tensor.reshape,
shape=(-1, 4 * 4 * 64)),
torch.nn.Linear(in_features=4 * 4 * 64, out_features=256),
torch.nn.LeakyReLU(),
torch.nn.Dropout(),
torch.nn.Linear(in_features=256, out_features=10),
)
############################################################################
# openml.config.apikey = 'key'
############################################################################
# The main network, composed of the above specified networks.
model = torch.nn.Sequential(
processing_net,
features_net,
results_net
)
############################################################################
############################################################################
# Define a sequential network that does the initial image reshaping
# and normalization model.
processing_net = torch.nn.Sequential(
openml_pytorch.layers.Functional(function=torch.Tensor.reshape,
shape=(-1, 1, 28, 28)),
torch.nn.BatchNorm2d(num_features=1)
)
############################################################################
############################################################################
# Define a sequential network that does the extracts the features from the
# image.
features_net = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=1, out_channels=32, kernel_size=5),
torch.nn.LeakyReLU(),
torch.nn.MaxPool2d(kernel_size=2),
torch.nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5),
torch.nn.LeakyReLU(),
torch.nn.MaxPool2d(kernel_size=2),
)
############################################################################
############################################################################
# Define a sequential network that flattens the features and compiles the
# results into probabilities for each digit.
results_net = torch.nn.Sequential(
openml_pytorch.layers.Functional(function=torch.Tensor.reshape,
shape=(-1, 4 * 4 * 64)),
torch.nn.Linear(in_features=4 * 4 * 64, out_features=256),
torch.nn.LeakyReLU(),
torch.nn.Dropout(),
torch.nn.Linear(in_features=256, out_features=10),
)
############################################################################
# openml.config.apikey = 'key'
############################################################################
# The main network, composed of the above specified networks.
model = torch.nn.Sequential(
processing_net,
features_net,
results_net
)
############################################################################
Configure the Data Module¶
- Make sure the
target_col
is correctly set.
In [ ]:
Copied!
data_module = OpenMLDataModule(
type_of_data="dataframe",
filename_col="class",
target_mode="categorical",
)
data_module = OpenMLDataModule(
type_of_data="dataframe",
filename_col="class",
target_mode="categorical",
)
Configure the Trainer Module¶
In [ ]:
Copied!
trainer = OpenMLTrainerModule(
data_module=data_module,
verbose = True,
epoch_count = 1,
callbacks=[],
)
openml_pytorch.config.trainer = trainer
trainer = OpenMLTrainerModule(
data_module=data_module,
verbose = True,
epoch_count = 1,
callbacks=[],
)
openml_pytorch.config.trainer = trainer
Download the task¶
In [ ]:
Copied!
# Download the OpenML task for the mnist 784 dataset.
task = openml.tasks.get_task(3573)
# Download the OpenML task for the mnist 784 dataset.
task = openml.tasks.get_task(3573)
Run the model on the task¶
In [ ]:
Copied!
run = openml.runs.run_model_on_task(model, task, avoid_duplicate_runs=False)
run = openml.runs.run_model_on_task(model, task, avoid_duplicate_runs=False)
View loss¶
In [ ]:
Copied!
trainer.runner.cbs[1].plot_loss()
trainer.runner.cbs[1].plot_loss()
View learning rate¶
In [ ]:
Copied!
trainer.runner.cbs[1].plot_lr()
trainer.runner.cbs[1].plot_lr()
Publish the run to OpenML¶
In [ ]:
Copied!
run.publish()
run.publish()