concepts.language.neural_ccg.tensorized_syntax.TensorizedSyntaxOnlyCCG#
- class TensorizedSyntaxOnlyCCG[source]#
Bases:
Module
Data structures and parsing implementation for a syntax-only CCG grammar. It uses a tensor-based representation for the chart, therefore is highly parallelizable.
Methods
add_module
(name, module)Add a child module to the current module.
apply
(fn)Apply
fn
recursively to every submodule (as returned by.children()
) as well as self.bfloat16
()Casts all floating point parameters and buffers to
bfloat16
datatype.buffers
([recurse])Return an iterator over module buffers.
children
()Return an iterator over immediate children modules.
compile
(*args, **kwargs)Compile this Module's forward using
torch.compile()
.cpu
()Move all model parameters and buffers to the CPU.
cuda
([device])Move all model parameters and buffers to the GPU.
double
()Casts all floating point parameters and buffers to
double
datatype.eval
()Set the module in evaluation mode.
extra_repr
()Set the extra representation of the module.
float
()Casts all floating point parameters and buffers to
float
datatype.forward
(*input)Define the computation performed at every call.
get_buffer
(target)Return the buffer given by
target
if it exists, otherwise throw an error.get_extra_state
()Return any extra state to include in the module's state_dict.
get_parameter
(target)Return the parameter given by
target
if it exists, otherwise throw an error.get_submodule
(target)Return the submodule given by
target
if it exists, otherwise throw an error.half
()Casts all floating point parameters and buffers to
half
datatype.ipu
([device])Move all model parameters and buffers to the IPU.
load_state_dict
(state_dict[, strict, assign])Copy parameters and buffers from
state_dict
into this module and its descendants.merge
(A, B, transition_matrix)The underlying merge function for chart parsing.
merge_beamsearch
(a_list, b_list, ...)The underlying merge function for chart parsing with beam search.
modules
()Return an iterator over all modules in the network.
named_buffers
([prefix, recurse, ...])Return an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
named_children
()Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
named_modules
([memo, prefix, remove_duplicate])Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
named_parameters
([prefix, recurse, ...])Return an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
parameters
([recurse])Return an iterator over module parameters.
parse
(words, words_length, ...)Parse a batch of sentences.
parse_beamsearch
(words, words_length, ...[, ...])Parse a batch of sentences using beam search.
register_backward_hook
(hook)Register a backward hook on the module.
register_buffer
(name, tensor[, persistent])Add a buffer to the module.
register_forward_hook
(hook, *[, prepend, ...])Register a forward hook on the module.
register_forward_pre_hook
(hook, *[, ...])Register a forward pre-hook on the module.
register_full_backward_hook
(hook[, prepend])Register a backward hook on the module.
register_full_backward_pre_hook
(hook[, prepend])Register a backward pre-hook on the module.
register_load_state_dict_post_hook
(hook)Register a post hook to be run after module's
load_state_dict
is called.register_module
(name, module)Alias for
add_module()
.register_parameter
(name, param)Add a parameter to the module.
register_state_dict_pre_hook
(hook)Register a pre-hook for the
state_dict()
method.requires_grad_
([requires_grad])Change if autograd should record operations on parameters in this module.
set_extra_state
(state)Set extra state contained in the loaded state_dict.
share_memory
()state_dict
(*args[, destination, prefix, ...])Return a dictionary containing references to the whole state of the module.
to
(*args, **kwargs)Move and/or cast the parameters and buffers.
to_empty
(*, device[, recurse])Move the parameters and buffers to the specified device without copying storage.
train
([mode])Set the module in training mode.
type
(dst_type)Casts all parameters and buffers to
dst_type
.xpu
([device])Move all model parameters and buffers to the XPU.
zero_grad
([set_to_none])Reset gradients of all model parameters.
Attributes
T_destination
call_super_init
dump_patches
A list of candidate syntax types.
The composition system.
- __init__(candidate_syntax_types, composition_system=None)[source]#
Initialize the CCG grammar.
- Parameters:
candidate_syntax_types (Iterable[CCGSyntaxType]) – the candidate syntax types.
composition_system (CCGCompositionSystem | None) – the composition system. If None, the default composition system will be used.
- merge(A, B, transition_matrix)[source]#
The underlying merge function for chart parsing.
- Parameters:
A (Tensor) – the distribution over syntax types for the left child, should have shape
[batch_size, nr_types]
.B (Tensor) – the distribution over syntax types for the right child, should have shape
[batch_size, nr_types]
.transition_matrix (Tensor) – the transition matrix, should have shape [A: nr_types, B: nr_types, C: nr_types]. Basically, each entry [A, B, C] means the probability of composing a node of type A and a node of type B to a node of type C.
- Returns:
The distribution over syntax types for the merged node, should have shape
[batch_size, nr_types]
.
- merge_beamsearch(a_list, b_list, transition_matrix)[source]#
The underlying merge function for chart parsing with beam search.
- Parameters:
a_list (List[CCGNode]) – the list of nodes for the left child.
b_list (List[CCGNode]) – the list of nodes for the right child.
transition_matrix (Tensor) – the transition matrix, should have shape [A: nr_types, B: nr_types, C: nr_types]. Basically, each entry [A, B, C] means the probability of composing a node of type A and a node of type B to a node of type C.
- Returns:
The list of merged nodes.
- Return type:
- parse(words, words_length, distribution_over_syntax_types, transition_matrix, allowed_root_type_indices)[source]#
Parse a batch of sentences.
- Parameters:
words (Tensor) – the word indices, should have shape
[batch_size, max_seqlen]
.words_length (Tensor) – the length of each sentence, should have shape
[batch_size]
.distribution_over_syntax_types (Tensor) – the distribution over syntax types for each word, should have shape
[batch_size, max_seqlen, nr_types]
.transition_matrix (Tensor) – the transition matrix, should have shape [A: nr_types, B: nr_types, C: nr_types]. Basically, each entry [A, B, C] means the probability of composing a node of type A and a node of type B to a node of type C.
allowed_root_type_indices (int | List[int]) – the indices of the syntax types that are allowed to be the root of the tree. It can be either a single integer or a list of integers.
- Returns:
The root nodes of the trees, with shape
[batch_size]
(whenallowed_root_type_indices
is a single integer) or[batch_size, nr_allowed_root_types]
(whenallowed_root_type_indices
is a list of integers). Each entry i can be interpreted as the log-probability of the root node being syntax typeallowed_root_type_indices[i]
.- Return type:
- parse_beamsearch(words, words_length, words_tokenized, distribution_over_syntax_types, transition_matrix, allowed_root_types, beam_size=3)[source]#
Parse a batch of sentences using beam search. This function will return candidate parsing trees (instead of just the probability distribution of the root note). Therefore, this function is significantly slower than
parse()
and should not be used for training.- Parameters:
words (Tensor) – the word indices, should have shape
[batch_size, max_seqlen]
.words_length (Tensor) – the length of each sentence, should have shape
[batch_size]
.distribution_over_syntax_types (Tensor) – the distribution over syntax types for each word, should have shape
[batch_size, max_seqlen, nr_types]
.transition_matrix (Tensor) – the transition matrix, should have shape [A: nr_types, B: nr_types, C: nr_types]. Basically, each entry [A, B, C] means the probability of composing a node of type A and a node of type B to a node of type C.
allowed_root_types (List[str]) – the syntax types that are allowed to be the root of the tree.
beam_size (int | None) – the beam size.
- Returns:
A list of candidate parsing trees for each sentence. Each entry is a list of trees, where each tree is a
CCGNode
object.- Return type:
- candidate_syntax_types: Tuple[CCGSyntaxType, ...]#
A list of candidate syntax types.
- composition_system: CCGCompositionSystem#
The composition system.
- property syntax_name2index#