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)

Adds a child module to the current module.

apply(fn)

Applies 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])

Returns an iterator over module buffers.

children()

Returns an iterator over immediate children modules.

cpu()

Moves all model parameters and buffers to the CPU.

cuda([device])

Moves all model parameters and buffers to the GPU.

double()

Casts all floating point parameters and buffers to double datatype.

eval()

Sets 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)

Defines the computation performed at every call.

get_buffer(target)

Returns the buffer given by target if it exists, otherwise throws an error.

get_extra_state()

Returns any extra state to include in the module's state_dict.

get_parameter(target)

Returns the parameter given by target if it exists, otherwise throws an error.

get_submodule(target)

Returns the submodule given by target if it exists, otherwise throws an error.

half()

Casts all floating point parameters and buffers to half datatype.

load_state_dict(state_dict[, strict])

Copies 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()

Returns an iterator over all modules in the network.

named_buffers([prefix, recurse])

Returns an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.

named_children()

Returns 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])

Returns 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])

Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.

parameters([recurse])

Returns 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)

Registers a backward hook on the module.

register_buffer(name, tensor[, persistent])

Adds a buffer to the module.

register_forward_hook(hook)

Registers a forward hook on the module.

register_forward_pre_hook(hook)

Registers a forward pre-hook on the module.

register_full_backward_hook(hook)

Registers a backward hook on the module.

register_parameter(name, param)

Adds a parameter to the module.

requires_grad_([requires_grad])

Change if autograd should record operations on parameters in this module.

set_extra_state(state)

This function is called from load_state_dict() to handle any extra state found within the state_dict.

share_memory()

See torch.Tensor.share_memory_()

state_dict([destination, prefix, keep_vars])

Returns a dictionary containing a whole state of the module.

to(*args, **kwargs)

Moves and/or casts the parameters and buffers.

to_empty(*, device)

Moves the parameters and buffers to the specified device without copying storage.

train([mode])

Sets the module in training mode.

type(dst_type)

Casts all parameters and buffers to dst_type.

xpu([device])

Moves all model parameters and buffers to the XPU.

zero_grad([set_to_none])

Sets gradients of all model parameters to zero.

Attributes

T_destination

dump_patches

This allows better BC support for load_state_dict().

syntax_name2index

training

candidate_syntax_types

A list of candidate syntax types.

composition_system

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:

List[CCGNode]

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] (when allowed_root_type_indices is a single integer) or [batch_size, nr_allowed_root_types] (when allowed_root_type_indices is a list of integers). Each entry i can be interpreted as the log-probability of the root node being syntax type allowed_root_type_indices[i].

Return type:

Tensor

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].

  • words_tokenized (List[List[str]]) – the tokenized words.

  • 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:

List[List[CCGNode]]

candidate_syntax_types: Tuple[CCGSyntaxType, ...]#

A list of candidate syntax types.

composition_system: CCGCompositionSystem#

The composition system.

property syntax_name2index#
training: bool#