Learn a PDFA using (Balle et al., 2013)
PDFA Learning (Balle et al., 2013)
In this notebook, we will show how to use the implementation of PDFA learning, as described in [2].
In terms of APIs, the only things that change
are the parameter algorithm
to learn_pdfa
(it must be Algorithm.BALLE
)
and the set of parameters.
See pdfa_learning.learn_pdfa.balle.params.py
for all the details.
Example
Utility functions to display SVGs.
%matplotlib inline
from pprint import pprint
from helpers import render_automaton
from pdfa_learning.learn_pdfa.base import learn_pdfa, Algorithm
from pdfa_learning.learn_pdfa.utils.generator import MultiprocessedGenerator, SimpleGenerator
from pdfa_learning.pdfa import PDFA
Example with 1 state.
Let's use the following automaton to generate samples.
p = 0.3
automaton = PDFA(
nb_states=2,
alphabet_size=2,
transition_dict={
0: {
0: (0, p),
1: (1, 1 - p),
},
1: {-1: (-1, 1.0)}
}
)
render_automaton(automaton)
Now we will run the PAC learning algorithm
to learn the above automaton.
With respect to the previous guide, we have the following parameters:
nb_samples
is the number of samples.delta
: is the probability of failure.n
: is the upperbound on the number of states.
generator = MultiprocessedGenerator(SimpleGenerator(automaton), nb_processes=8)
pdfa = learn_pdfa(
algorithm=Algorithm.BALLE,
sample_generator=generator,
alphabet_size=automaton.alphabet_size,
nb_samples=20000,
delta=0.1,
n=10,
)
The learned automaton is:
print("Transitions: ")
pprint(pdfa.transitions)
render_automaton(pdfa)
Example with 2 states.
Now let's try to learn the following automaton:
p1 = 0.4
p2 = 0.7
automaton = PDFA(
3,
2,
{
0: {
0: (1, p1),
1: (2, 1 - p1),
},
1: {
0: (2, 1 - p2),
1: (1, p2),
},
2: {
-1: (-1, 1.0)
}
},
)
render_automaton(automaton)
generator = MultiprocessedGenerator(SimpleGenerator(automaton), nb_processes=8)
pdfa = learn_pdfa(
algorithm=Algorithm.BALLE,
sample_generator=generator,
alphabet_size=automaton.alphabet_size,
nb_samples=20000,
delta=0.1,
n=10,
)
render_automaton(pdfa)
References
- [1] Balle, Borja, Jorge Castro, and Ricard Gavaldà . "Learning probabilistic automata: A study in state distinguishability." Theoretical Computer Science 473 (2013): 46-60.