by: Tom Jelen
In order to deploy a
Graph that you have trained with Feyn, you simply just need to save it to a file. Then from the machine (or agent) where you want to use this
Graph for doing predictions, you just load it again from the file via Feyn.
This notebook shows how to use
Graph.load to take a
Graph and promote it to a production environment.
This notebook will not go into the details of how build and teach Feyn-models. If that is of your interest, then take a look at some of the other notebooks.
import numpy as np import pandas as pd from feyn.tools import split
import feyn from feyn import tools
data = pd.read_csv('pulsar_stars.csv') train, test = split(data)
Create a Feyn-model from the pulsar stars dataset
This happens on a machine that is running a training process.
The quality of this model is not important for this tutorial, so we won't spend a lot of time fitting the graph.
qlattice = feyn.QLattice() in_columns = data.columns[data.columns != "out_target_class"] registers = [qlattice.get_register(name=col) for col in in_columns] out_register = qlattice.get_register(name="out_target_class")
qgraph = qlattice.get_qgraph(registers, out_register) qgraph.fit(train, epochs=2)
Select and promote a graph
Once a suitable graph has been selected (human selection, or maybe just purely based on statistics), you need to save it somewhere, so that it can be picked up by your process that executes it to do predictions.
For this demo we will just use a temporary file, but it can be saved to any file- or path-like object.
production_database_file = '/tmp/pulsar-stars.graph' best_graph = qgraph.select(train) best_graph.save(production_database_file)
In your production environment, you have a process or an interface that uses the
Graph to execute predictions. It can use the
Graph.load function to load the previously saved
best_graph = feyn.Graph.load(production_database_file) prediction = best_graph.predict(test) tools.plot_confusion_matrix(y_true=test['out_target_class'], y_pred=prediction.round(), title="Confusion Matrix [Test]")