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)

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.

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

format_lexicons_table(vocab[, ...])

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.

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

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

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#