Feyn Documentation

Feyn Documentation

  • Learn
  • Guides
  • Tutorials
  • API Reference
  • FAQ

›Advanced

Getting Started

  • Quick start
  • Using Feyn
  • Installation
  • What is the QLattice?

Essentials

  • Auto Run
  • Summary plot
  • Plot response
  • Splitting a dataset
  • Seeding a QLattice
  • Predicting with a model
  • Saving and loading models
  • Categorical features

Evaluate Regressors

  • Regression plot
  • Residuals plot

Evaluate Classifiers

  • ROC curve
  • Confusion matrix
  • Plot probability scores

Understand Your Models

  • Plot response 1D
  • Plot response 2D
  • Model signal
  • Segmented loss
  • Interactive flow

Primitive Operations

  • Using the primitives
  • Updating priors
  • Sample models
  • Fitting models
  • Pruning models
  • Visualise a model
  • Diverse models
  • Updating a QLattice
  • Validate data
  • Semantic types

Advanced

  • Converting a model to SymPy
  • Logging in Feyn
  • Setting themes
  • Saving a graph as an image
  • Using the query language
  • Estimating priors
  • Filtering models
  • Model parameters
  • Model complexity

Privacy & Commercial

  • Privacy
  • Community edition
  • Commercial use
  • Transition to Feyn 3.0

Converting a model to SymPy

by: Kevin Broløs & Meera Machado
(Feyn version 3.4.0 or newer)


Feyn models are, in truth, mathematical expressions represented as directed graphs. We can use SymPy to convert these graph representations to symbolic mathematical expressions. Specifically, models can be converted to SymPy objects for further manipulation or processing: executing them as equations, printing them in LaTeX, or just implementing them in any environment following the equation.

The only limitation is that we don't currently support exporting categorical registers to an executable function, so for models with categories, you'll only have an expression for understanding purposes.

Let's first generate a dataset and initialize a QLattice

import feyn

import numpy as np
import pandas as pd

# Generate a dataset and put it into a dataframe
np.random.seed(42)
X = np.random.normal(size=100)
y = np.exp(X)

data = pd.DataFrame(X, columns=['X'])
data['y'] = y

# Instantiate a QLattice
ql = feyn.QLattice(random_seed=666)

Sample and fit models real quick

We run the QLattice for just 2 epochs, and convert the best model to a mathematical expression.

models = ql.auto_run(data, 'y', n_epochs=2)

# Take the best model
model = models[0]
model

example graph

The model maps the input XXX to the output yyy through an exponential function, just like the generated data.

Convert to SymPy

Let's see how the SymPy expression looks converted to LaTeX:

sympy_model = model.sympify(signif=3)

sympy_model.as_expr()

1.0e1.0X−0.00306\displaystyle 1.0e^{1.0X} - 0.003061.0e1.0X−0.00306

The parameter signif specifies the number of significant digits in the expression's coefficients.

Let's try it for classification

For the sake of simplicity, we create a classification task by setting a boundary on the yyy values.

# Boundary at the median
data['y'] = np.where(data['y'] > 0.88, 1, 0)

# Instantiate a new QLattice
ql = feyn.QLattice(random_seed=666)

# Run QLattice for classification
models = ql.auto_run(data, 'y', n_epochs=2)

# Best model
model = models[0]
model

example graph

Let's see how the SymPy expression looks converted to LaTeX:

sympy_model = model.sympify(symbolic_lr=True, include_weights=False)

sympy_model.as_expr()

11+e−tanh⁡(X+eX)\displaystyle \frac{1}{1 + e^{-\tanh{(X + e^X)}}}1+e−tanh(X+eX)1​

In a classification problem, the mathematical expression in wrapped in a logistic function, as clearly seen above. Setting the parameter symbolic_lr to True makes the logistic function explicit. In addition, by setting include_weights to False the coefficients are not displayed, which can make the mathematical expression cleaner.

What to do with the SymPy object

You can check out their documentation on how to use it if you don't already know how. It works automatically by pretty printing in unicode terminals and IPython environments.

You can also use this for portability of final Feyn graphs, as you don't need the Python runtime to execute a simple mathematical equation. So you can take the output of these functions and port to R, STATA, JavaScript, or even Excel if you want to.

← Semantic typesLogging in Feyn →
  • Let's first generate a dataset and initialize a QLattice
  • Sample and fit models real quick
  • Convert to SymPy
  • Let's try it for classification
  • What to do with the SymPy object

Subscribe to get news about Feyn and the QLattice.

You can opt out at any time, and you can read our privacy policy here.

Copyright © 2024 Abzu.ai - Feyn license: CC BY-NC-ND 4.0
Feyn®, QGraph®, and the QLattice® are registered trademarks of Abzu®