How to Use the iModelsX Library for Explainable Text Modeling

Apr 11, 2022 | Data Science

The iModelsX library is designed to make text modeling more understandable and accessible. In this guide, we will walk you through the steps to get started with this Scikit-learn compatible library, and how to utilize its various features for explainable modeling and text data manipulation.

Quickstart Installation

To get started, you will first need to install the library. Open your terminal and run the following command:

pip install imodelsx

Getting Started

Once you have installed the library, you can explore its capabilities through demo notebooks. These notebooks contain example code for various functionalities that the library offers. Visit the demo notebooks for more information.

Explaining and Steering Models

The iModelsX library employs several model types that focus on explainability, allowing you to not just make predictions but also understand how those predictions are derived. Below, we explain how the different models work using an analogy.

Analogy: The Chef and the Recipe

Imagine a chef in a kitchen where every ingredient stands for a piece of input data. The chef uses a recipe—represented by a model—to create a dish that is your output (the prediction). Just as a recipe guides how to combine and cook the ingredients, models like Tree-Prompt or AutoPrompt use specific prompts to navigate through the data, helping the chef (model) produce an appetite-worthy dish (explanation of the output).

Think of the enhanceability of the model in terms of steering the chef’s process. Instead of simply following the recipe, the chef can adjust cooking times, substitute ingredients, or modify techniques based on feedback or taste. This is akin to how the Tree-Prompt generates trees of prompts to clarify model decisions, or how SASC uses language models to explain black-box outputs.

Key Features of iModelsX

The library includes various models each serving a unique purpose:

  • Tree-Prompt: Enables explanation through a tree structure of prompts.
  • iPrompt: Generates prompts to elaborate on patterns in the data.
  • AutoPrompt: Develops natural language prompts using input gradients.
  • D3: Describes the differences between two data distributions.
  • Aug-Linear: Enhances linear models with language models for better feature extraction.

Example: Using TreePromptClassifier

Here’s a simple way to use the TreePromptClassifier to explain text model predictions:

from imodelsx import TreePromptClassifier
import datasets
import numpy as np
from sklearn.tree import plot_tree
import matplotlib.pyplot as plt

# Set up data
rng = np.random.default_rng(seed=42)
dset_train = datasets.load_dataset("rotten_tomatoes")["train"]
dset_val = datasets.load_dataset("rotten_tomatoes")["validation"]

# Fit the model
prompts = ["This movie is,", "Positive or Negative?", "The movie was,"]
verbalizer = {0: "Negative.", 1: "Positive."}
checkpoint = "gpt2"

m = TreePromptClassifier(checkpoint=checkpoint, prompts=prompts, verbalizer=verbalizer)
m.fit(dset_train["text"], dset_train["label"])

# Compute accuracy
preds = m.predict(dset_val["text"])
print("Tree-Prompt acc (val):", np.mean(preds == dset_val["label"]))

# Visualize decision tree
plot_tree(m.clf_, fontsize=10, feature_names=m.feature_names_, class_names=list(verbalizer.values()), filled=True)
plt.show()

Troubleshooting

If you encounter any issues while using the iModelsX library, here are some troubleshooting tips:

  • Ensure that all dependencies are installed correctly. You can try to reinstall the library by running the installation command again.
  • Check the compatibility of the version of Python you are using. The library requires Python 3.9 or later.
  • If a certain model isn’t working as expected, verify whether the prompt structure you’re using is correctly formatted.
  • Refer to the demo notebooks for example use cases that may align with your needs.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox