Learn the Basics || Quick Start || Dataset & Data Structure || Learning Part || Reasoning Part || Evaluation Metrics || Bridge
Learning Part
In this section, we will look at how to build the learning part.
In ABLkit, building the learning part involves two steps:
Build a machine learning base model used to make predictions on instance-level data.
Instantiate an
ABLModelwith the base model, which enables the learning part to process example-level data.
import sklearn
import torchvision
from ablkit.learning import BasicNN, ABLModel
Building a base model
ABL toolkit allows the base model to be one of the following forms:
Any machine learning model conforming to the scikit-learn style, i.e., models which has implemented the
fitandpredictmethods;A PyTorch-based neural network, provided it has defined the architecture and implemented the
forwardmethod.
For a scikit-learn model, we can directly use the model itself as a base model. For example, we can customize our base model by a KNN classfier:
base_model = sklearn.neighbors.KNeighborsClassifier(n_neighbors=3)
For a PyTorch-based neural network, we need to encapsulate it within a BasicNN object to create a base model. For example, we can customize our base model by a pre-trained ResNet-18:
# Load a PyTorch-based neural network
net = torchvision.models.resnet18(pretrained=True)
# loss function and optimizer are used for training
loss_fn = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters())
base_model = BasicNN(net, loss_fn, optimizer)
BasicNN
BasicNN is a wrapper class for PyTorch-based neural networks, which enables them to work as scikit-learn models. It encapsulates the neural network, loss function, optimizer, and other elements into a single object, which can be used as a base model.
Besides the necessary methods required to instantiate an ABLModel, i.e., fit and predict, BasicNN also implements the following methods:
Method |
Function |
|---|---|
|
Train the neural network for one epoch. |
|
Predict the class probabilities of |
|
Calculate the accuracy of the model on test data. |
|
Save the model. |
|
Load the model. |
Instantiating an ABLModel
Typically, base model is trained to make predictions on instance-level data, and can not directly process example-level data, which is not suitable for most neural-symbolic tasks. ABLkit provides the ABLModel to solve this problem. This class serves as a unified wrapper for all base models, which enables the learning part to train, test, and predict on example-level data.
Generally, we can simply instantiate an ABLModel by:
# Instantiate an ABLModel
model = ABLModel(base_model)