by: Kevin Broløs
How to use the QLattice
For a quick introduction, check out our Quick Start.
You back? Good!
Let's go a little more in depth with your new friend.
Access your QLattice
Recall that you can access your
QLattice like so:
from feyn import QLattice qlattice = QLattice(url = "<URL to your qlattice>", api_token="<Your API token>")
Here's a guide on how to get your URL and API token.
The blank slate
QLattice will be a blank slate, prime for molding by extracting
QGraphs, fitting them and updating the
QLattice with the results.
What happens if you've been experimenting with some different datasets, or approaches, and you want to go back to this initial state?
reset all knowledge in the
QLattice, so take care not to use it for production systems. Except for restoring a previous version, this action cannot be undone.
In addition to resetting the learned relationships, it also removes all previously registered
Essential to preparing your
QLattice to solve any given problem, is adding
registers. As explained in What is a QLattice, these are equivalent to
columns in your dataset(s).
You can choose to use a subset of
registers in the
QLattice for solving the problem, or all of them. This also means, that if you share a
QLattice for multiple problems, some
registers may overlap between them, and some may not. This is by design, and enables sharing inspiration between problems.
Let's use the iris dataset available in
from sklearn.datasets import load_iris import pandas as pd from feyn.tools import split # Load the dataset iris = load_iris() features = iris.feature_names # Wrap in a pandas dataframe for ease of use data = pd.DataFrame(iris.data, columns = features) data['target'] = pd.Series(iris.target) # Split into a train and test set train, test = split(data, ratio=(0.75, 0.25))
Let's start registering these features in our
for feature_name in data.columns: qlattice.get_register(name=feature_name)
get_register call. This returns a
register instance, that holds a reference to its location in the
QLattice. You can choose to save and hold on to it if you want to use it later. Moving on, however, we're just going to use the feature names to define the inputs and output.
We use these
registers later, when we tell the
QLattice which are the inputs, and which should be the
output register. Because you can choose to use all or some of the
registers, you'll need to specify the names of the ones you want to use when you extract
Inputs and outputs
You might have already guessed it - but there's nothing special about the
output register - it's simply to let the
QLattice know where the terminal node is. With some exception, any
register can function both as an input and output.
This means you could utilize the learnings in your
QLattice (and the very same
registers) to extract
graphs that flip the question you're asking - for instance in this case to predict the
sepal length (cm) from the
class of flower and the
sepal width (cm).
Once you've defined your
registers, you can use them to extract a
QGraph is a collection of
Graphs that might each be possible solutions to your problem, containing one or more of your
input registers, and leading to your
qgraph = qlattice.get_qgraph(data.columns, 'target')
You still haven't used any of the data yet, and that's because your
QLattice doesn't work with data, it works with concepts.
When you have a
QGraph, you can start fitting it. The
QGraph is a pretty blank slate when you initially get it as it only relies on past experiences (if any). Fitting it to your specific dataset will allow you to optimise the weights of each
graph, letting you select the best fitting ones. Then you can updating your
QLattice with the learnings from the best graph.
Update your QLattice
Once you've selected a good graph based on whichever perfomance metric you prefer, you can update your
QLattice with the learnings.
best_graph = qgraph.select(train) qlattice.update(best_graph)
This is essential to how you can tap into the full potential of your
QLattice. Updating the
QLattice means restructuring the knowledge in a way that takes into account the structure of the
graph you have just fitted with your dataset. This narrows the search field for new
graphs and allows you to get new and different
graphs to fit, and converge faster on solutions that work for your specific problem.
In layman's terms, you tell the
QLattice what works and what you'd like to see more of, and it'll use this knowledge to make you happier, faster.
Moving on to registers
On to the next part, which will dive a little into
registers, and explain what they really represent.