concepts.language.neural_ccg.grammar.NeuralCCG#

class NeuralCCG[source]#

Bases: Module

The neural CCG grammar and the implementation for parsing.

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.

format_lexicon_table_sentence(words, ...[, ...])

format_lexicons_table(vocab[, ...])

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.

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, distribution_over_lexicon_entries)

Parse the sentence using the CKY algorithm.

parse_beamsearch(words, ...[, ...])

Parse the sentence with the CKY algorithm, but with 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()

See torch.Tensor.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

nr_candidate_lexicon_entries

training

domain

The function domain.

executor

The executor for the domain.

composition_system

The composition system.

candidate_lexicon_entries

The candidate lexicon entries.

joint_execution

Whether to execute the partial programs during CKY.

allow_none_lexicon

Whether to allow None lexicon.

reweight_meaning_lex

Whether to reweight the meaning lexicon entries.

__init__(domain, executor, candidate_lexicon_entries, composition_system=None, joint_execution=True, allow_none_lexicon=False, reweight_meaning_lex=False)[source]#

Initialize the neural CCG grammar.

Parameters:
  • domain (FunctionDomain) – the function domain of the grammar.

  • executor (FunctionDomainExecutor) – the executor for the function domain.

  • candidate_lexicon_entries (Iterable[NeuralCCGLexiconSearchResult]) – a list of candidate lexicon entries.

  • composition_system (CCGCompositionSystem | None) – the composition system. If None, the default composition system will be used.

  • joint_execution (bool) – whether to execute the partial programs during CKY.

  • allow_none_lexicon (bool) – whether to allow None lexicon.

  • reweight_meaning_lex (bool) – whether to reweight the meaning lexicon entries. Specifically, if there are two parsings share the same set of lexicon entries (i.e., caused by ambiguities in combination), specifying this flag will reweight both of them to be 1 / (number of parsings that used this set of lexicon entries).

format_lexicon_table_sentence(words, distribution_over_lexicon_entries, max_entries=None)[source]#
format_lexicons_table(vocab, distribution_over_lexicon_entries=None, distribution_over_lexicon_entries_words=None, *, used_lexicon_weights=None, words=None, full=False, max_entries=None, print_grad=True)[source]#
parse(words, distribution_over_lexicon_entries, used_lexicon_entries=None, acceptable_rtypes=None, max_research=0)[source]#

Parse the sentence using the CKY algorithm. The function maintains a “chart” for all possible spans of the sentence, and for each span, it maintains a list of possible CCG nodes. When max_research is set to a positive integer, for each span, the function will keep a tuple of lists of CCG nodes, where dp[i][j][k] corresponds to candidate CCG nodes for span [i, j] with k words being re-searched.

Parameters:
  • words (Sequence[str] | str) – the words of the sentence. If the input is a string, it will be tokenized using the default .split() method.

  • distribution_over_lexicon_entries (Tensor) – the distribution over the lexicon entries for each word.

  • used_lexicon_entries (Dict[str, Set[int]] | None) – the used lexicon entries for each word. If None, it will be computed from the distribution. This is a dictionary mapping from the word to the set of lexicon entry indices.

  • acceptable_rtypes (Sequence[TypeBase] | None) – the acceptable return types. If None, it will accept all return types.

  • max_research (int) – the maximum number of words whose lexicon entries can be re-searched (i.e., the used_lexicon_entries for this word will be ignored).

Returns:

The parsing result.

Return type:

List[NeuralCCGNode]

parse_beamsearch(words, distribution_over_lexicon_entries, used_lexicon_entries=None, acceptable_rtypes=None, beam=5, lexical_beam=0, out_of_vocab_weight=None)[source]#

Parse the sentence with the CKY algorithm, but with beam search. Note that this function does not support re-search for used lexicon entries, and it can only be used when the model is in eval mode, because the beam-search will break the correctness of the gradient computation.

Parameters:
  • words (Sequence[str] | str) – the words of the sentence. If the input is a string, it will be tokenized using the default .split() method.

  • distribution_over_lexicon_entries (Tensor) – the distribution over the lexicon entries for each word.

  • used_lexicon_entries (Dict[str, Set[int]] | None) – the used lexicon entries for each word. If None, it will be computed from the distribution. This is a dictionary mapping from the word to the set of lexicon entry indices.

  • acceptable_rtypes (List[TypeBase] | None) – the acceptable return types. If None, it will accept all return types.

  • beam (int) – the beam size.

  • lexical_beam (int) – the lexical beam size. If 0, we will keep all the lexical entries.

  • out_of_vocab_weight (float | None) – the weight for out-of-vocabulary words. Specifically, if this value is set, we will consider all lexical entries (instead of only those in the used_lexicon_entries). However, the corresponding weights for those entries will be set to out_of_vocab_weight.

Returns:

The parsing result, as a list of parsing trees.

Return type:

List[NeuralCCGNode]

allow_none_lexicon: bool#

Whether to allow None lexicon.

candidate_lexicon_entries: Tuple[NeuralCCGLexiconSearchResult, ...]#

The candidate lexicon entries.

composition_system: CCGCompositionSystem#

The composition system.

domain: FunctionDomain#

The function domain.

executor: FunctionDomainExecutor#

The executor for the domain.

joint_execution: bool#

Whether to execute the partial programs during CKY.

property nr_candidate_lexicon_entries#
reweight_meaning_lex: bool#

Whether to reweight the meaning lexicon entries.

training: bool#