Cfg
neps.search_spaces.architecture.cfg
#
Grammar
#
Bases: CFG
Extended context free grammar (CFG) class from the NLTK python package We have provided functionality to sample from the CFG. We have included generation capability within the class (before it was an external function) Also allow sampling to return whole trees (not just the string of terminals).
Source code in neps/search_spaces/architecture/cfg.py
mutate
#
Grammar-based mutation, i.e., we sample a new subtree from a nonterminal node in the parse tree.
PARAMETER | DESCRIPTION |
---|---|
parent
|
parent of the mutation.
TYPE:
|
subtree_index
|
index pointing to the node that is root of the subtree.
TYPE:
|
subtree_node
|
nonterminal symbol of the node.
TYPE:
|
patience
|
Number of tries. Defaults to 50.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
mutated child from parent.
TYPE:
|
Source code in neps/search_spaces/architecture/cfg.py
rand_subtree
#
Helper function to choose a random subtree in a given parse tree. Runs a single pass through the tree (stored as string) to look for the location of swappable nonterminal symbols.
PARAMETER | DESCRIPTION |
---|---|
tree
|
parse tree.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[str, int]
|
Tuple[str, int]: return the parent node of the subtree and its index. |
Source code in neps/search_spaces/architecture/cfg.py
remove_subtree
staticmethod
#
Helper functioon to remove a subtree from a parse tree given its index. E.g. '(S (S (T 2)) (ADD +) (T 1))' becomes '(S (S (T 2)) ', '(T 1))' after removing (ADD +).
PARAMETER | DESCRIPTION |
---|---|
tree
|
parse tree
TYPE:
|
index
|
index of the subtree root node
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[str, str, str]
|
Tuple[str, str, str]: part before the subtree, subtree, part past subtree |