Skip to content

Learn a PDFA using (Palmer & Goldberg, 2005)

PDFA Learning (Palmer & Goldberg, 2005)

Note: this implementation is deprecated. Consider using the implementation of the Balle's algorithm (see the next documentation page)

In this notebook, we will show how to use the implementation of PDFA learning, as described in [1].

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
[2021-01-02 16:14:37,642][matplotlib.pyplot][DEBUG] Loaded backend module://ipykernel.pylab.backend_inline version unknown.

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)
[2021-01-02 16:14:39,452][graphviz.files][DEBUG] write 195 bytes to '/tmp/tmpgcaox75a/output'
[2021-01-02 16:14:39,455][graphviz.backend][DEBUG] run ['dot', '-Kdot', '-Tsvg', '-O', 'output']

%3 fake 0 0 fake->0 0->0 0, 0.3 1 1 0->1 1, 0.7 -1 -1 1->-1 -1, 1.0

Now we will run the PAC learning algorithm to learn the above automaton.

  • MultiprocessedGenerator wraps the automaton and generates samples using multiple processes;
  • learn_pdfa is the main entrypoint of the algorithm implementation.
  • n1_max_debug is the maximum number for N_1 (for the subgraph learning)
  • n2_max_debug is the maximum number for N_2 (for the probabilities learning)
  • m0_max_debug is the maximum number for m_0 (for multiset filtering)
generator = MultiprocessedGenerator(SimpleGenerator(automaton), nb_processes=8)

pdfa = learn_pdfa(
    algorithm=Algorithm.PALMER,
    sample_generator=generator,
    alphabet_size=2,
    epsilon=0.2,
    delta_1=0.2,
    delta_2=0.2,
    mu=0.1,
    n=3,
    n1_max_debug=100000,
    n2_max_debug=100000,
    m0_max_debug=100000 / 10,
)
[2021-01-02 16:14:40,743][pdfa_learning.learn_pdfa][INFO] Parameters: ('PalmerParams(sample_generator=<pdfa_learning.learn_pdfa.utils.generator.MultiprocessedGenerator '
 'object at 0x7f2aabd60f10>, alphabet_size=2, epsilon=0.2, delta_1=0.2, '
 'delta_2=0.2, mu=0.1, n=3, m0_max_debug=10000.0, n1_max_debug=100000, '
 'n2_max_debug=100000)')
[2021-01-02 16:14:40,745][pdfa_learning.learn_pdfa][INFO] N1 = 54432.579348157145, N2 = 55998960.0. Chosen: 55998960
[2021-01-02 16:14:40,745][pdfa_learning.learn_pdfa][INFO] m0 = 466658
[2021-01-02 16:14:40,746][pdfa_learning.learn_pdfa][INFO] N = 55998960
[2021-01-02 16:14:40,746][pdfa_learning.learn_pdfa][INFO] using m0 = 10000.0, N = 100000
[2021-01-02 16:14:42,789][pdfa_learning.learn_pdfa][INFO] Sampling done.
[2021-01-02 16:14:42,790][pdfa_learning.learn_pdfa][INFO] Number of samples: 100000.
[2021-01-02 16:14:42,793][pdfa_learning.learn_pdfa][INFO] Avg. length of samples: 2.43472.
[2021-01-02 16:14:42,908][pdfa_learning.learn_pdfa][INFO] Iteration 0
[2021-01-02 16:14:43,152][pdfa_learning.learn_pdfa][INFO] Iteration 1
[2021-01-02 16:14:43,307][pdfa_learning.learn_pdfa][INFO] Iteration 2
[2021-01-02 16:14:43,435][pdfa_learning.learn_pdfa][INFO] Vertices: {0, 1}
[2021-01-02 16:14:43,436][pdfa_learning.learn_pdfa][INFO] Transitions: {0: {-1: -1, 0: 0, 1: 1}, 1: {-1: -1}}
[2021-01-02 16:14:43,436][pdfa_learning.learn_pdfa][INFO] Computed final node: -1 (no outgoing transitions)
[2021-01-02 16:14:43,438][pdfa_learning.learn_pdfa][INFO] Number of vertices: 2.
[2021-01-02 16:14:43,439][pdfa_learning.learn_pdfa][INFO] Transitions: {0: {-1: -1, 0: 0, 1: 1}, 1: {-1: -1}}.
[2021-01-02 16:14:43,440][pdfa_learning.learn_pdfa][INFO] Start learning probabilities.
[2021-01-02 16:14:43,440][pdfa_learning.learn_pdfa][INFO] Sample size: 21734484183613.
[2021-01-02 16:14:43,441][pdfa_learning.learn_pdfa][INFO] Using N = 100000.
[2021-01-02 16:14:45,992][pdfa_learning.learn_pdfa][INFO] Computed vertices: {0, 1}
[2021-01-02 16:14:45,993][pdfa_learning.learn_pdfa][INFO] Computed transition dictionary: {0: {-1: (-1, 0.0), 0: (0, 0.30019034822528273), 1: (1, 0.6998096517747173)},
 1: {-1: (-1, 1.0)}}

The learned automaton is:

print("Transitions: ")
pprint(pdfa.transitions)
render_automaton(pdfa)
[2021-01-02 16:14:46,001][graphviz.files][DEBUG] write 203 bytes to '/tmp/tmpgq7qad18/output'
[2021-01-02 16:14:46,067][graphviz.backend][DEBUG] run ['dot', '-Kdot', '-Tsvg', '-O', 'output']

Transitions: 
{(0, -1, 0.0, -1),
 (0, 0, 0.30019034822528273, 0),
 (0, 1, 0.6998096517747173, 1),
 (1, -1, 1.0, -1)}

%3 fake 0 0 fake->0 0->0 0, 0.30019 1 1 0->1 1, 0.69981 -1 -1 1->-1 -1, 1.0

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)
[2021-01-02 16:14:46,168][graphviz.files][DEBUG] write 248 bytes to '/tmp/tmp1ryjqa9g/output'
[2021-01-02 16:14:46,170][graphviz.backend][DEBUG] run ['dot', '-Kdot', '-Tsvg', '-O', 'output']

%3 fake 0 0 fake->0 1 1 0->1 0, 0.4 2 2 0->2 1, 0.6 1->1 1, 0.7 1->2 0, 0.3 -1 -1 2->-1 -1, 1.0
generator = MultiprocessedGenerator(SimpleGenerator(automaton), nb_processes=8)

pdfa = learn_pdfa(
    algorithm=Algorithm.PALMER,
    sample_generator=generator,
    alphabet_size=2,
    epsilon=0.2,
    delta_1=0.2,
    delta_2=0.2,
    mu=0.1,
    n=3,
    n1_max_debug=3000000,
    n2_max_debug=1000000,
    m0_max_debug=3000000 / 10,
)
[2021-01-02 16:14:46,289][pdfa_learning.learn_pdfa][INFO] Parameters: ('PalmerParams(sample_generator=<pdfa_learning.learn_pdfa.utils.generator.MultiprocessedGenerator '
 'object at 0x7f2a76456710>, alphabet_size=2, epsilon=0.2, delta_1=0.2, '
 'delta_2=0.2, mu=0.1, n=3, m0_max_debug=300000.0, n1_max_debug=3000000, '
 'n2_max_debug=1000000)')
[2021-01-02 16:14:46,291][pdfa_learning.learn_pdfa][INFO] N1 = 54432.579348157145, N2 = 55998960.0. Chosen: 55998960
[2021-01-02 16:14:46,292][pdfa_learning.learn_pdfa][INFO] m0 = 466658
[2021-01-02 16:14:46,293][pdfa_learning.learn_pdfa][INFO] N = 55998960
[2021-01-02 16:14:46,294][pdfa_learning.learn_pdfa][INFO] using m0 = 300000.0, N = 3000000
[2021-01-02 16:17:14,294][pdfa_learning.learn_pdfa][INFO] Sampling done.
[2021-01-02 16:17:14,296][pdfa_learning.learn_pdfa][INFO] Number of samples: 3000000.
[2021-01-02 16:17:14,417][pdfa_learning.learn_pdfa][INFO] Avg. length of samples: 3.3331573333333333.
[2021-01-02 16:17:18,827][pdfa_learning.learn_pdfa][INFO] Iteration 0
[2021-01-02 16:17:30,301][pdfa_learning.learn_pdfa][INFO] Iteration 1
[2021-01-02 16:17:37,928][pdfa_learning.learn_pdfa][INFO] Iteration 2
[2021-01-02 16:17:47,846][pdfa_learning.learn_pdfa][INFO] Iteration 3
[2021-01-02 16:17:58,387][pdfa_learning.learn_pdfa][INFO] Iteration 4
[2021-01-02 16:18:06,808][pdfa_learning.learn_pdfa][INFO] Vertices: {0, 1, 2}
[2021-01-02 16:18:06,811][pdfa_learning.learn_pdfa][INFO] Transitions: {0: {-1: -1, 0: 2, 1: 1}, 1: {-1: -1}, 2: {-1: -1, 0: 1, 1: 2}}
[2021-01-02 16:18:06,813][pdfa_learning.learn_pdfa][INFO] Computed final node: -1 (no outgoing transitions)
[2021-01-02 16:18:06,903][pdfa_learning.learn_pdfa][INFO] Number of vertices: 3.
[2021-01-02 16:18:06,904][pdfa_learning.learn_pdfa][INFO] Transitions: {0: {-1: -1, 0: 2, 1: 1}, 1: {-1: -1}, 2: {-1: -1, 0: 1, 1: 2}}.
[2021-01-02 16:18:06,904][pdfa_learning.learn_pdfa][INFO] Start learning probabilities.
[2021-01-02 16:18:06,905][pdfa_learning.learn_pdfa][INFO] Sample size: 21734484183613.
[2021-01-02 16:18:06,906][pdfa_learning.learn_pdfa][INFO] Using N = 1000000.
[2021-01-02 16:19:04,326][pdfa_learning.learn_pdfa][INFO] Computed vertices: {0, 1, 2}
[2021-01-02 16:19:04,327][pdfa_learning.learn_pdfa][INFO] Computed transition dictionary: {0: {-1: (-1, 0.0), 0: (2, 0.399264), 1: (1, 0.600736)},
 1: {-1: (-1, 1.0)},
 2: {-1: (-1, 0.0), 0: (1, 0.30008658425128676), 1: (2, 0.6999134157487132)}}

render_automaton(pdfa)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-5f1d6655d94a> in <module>
----> 1 render_automaton(pdfa)

NameError: name 'render_automaton' is not defined

Palmer N., Goldberg P.W. (2005) PAC-Learnability of Probabilistic Deterministic Finite State Automata in Terms of Variation Distance. In: Jain S., Simon H.U., Tomita E. (eds) Algorithmic Learning Theory. ALT 2005. Lecture Notes in Computer Science, vol 3734. Springer, Berlin, Heidelberg. https://doi.org/10.1007/11564089_14