def from_params(cls, vocab, params: Params) -> 'NlvrDirectSemanticParser': sentence_embedder_params = params.pop("sentence_embedder") sentence_embedder = TextFieldEmbedder.from_params( vocab, sentence_embedder_params) action_embedding_dim = params.pop_int('action_embedding_dim') encoder = Seq2SeqEncoder.from_params(params.pop("encoder")) dropout = params.pop_float('dropout', 0.0) attention_function_type = params.pop("attention_function", None) if attention_function_type is not None: attention_function = SimilarityFunction.from_params( attention_function_type) else: attention_function = None decoder_beam_search = BeamSearch.from_params( params.pop("decoder_beam_search")) max_decoding_steps = params.pop_int("max_decoding_steps") params.assert_empty(cls.__name__) return cls(vocab, sentence_embedder=sentence_embedder, action_embedding_dim=action_embedding_dim, encoder=encoder, attention_function=attention_function, decoder_beam_search=decoder_beam_search, max_decoding_steps=max_decoding_steps, dropout=dropout)
def test_search(self): beam_search = BeamSearch.from_params(Params({'beam_size': 4})) initial_state = SimpleDecoderState([0, 1, 2, 3], [[], [], [], []], [ Variable(torch.Tensor([0.0])), Variable(torch.Tensor([0.0])), Variable(torch.Tensor([0.0])), Variable(torch.Tensor([0.0])) ], [-3, 1, -20, 5]) decoder_step = SimpleDecoderStep(include_value_in_score=True) best_states = beam_search.search(5, initial_state, decoder_step, keep_final_unfinished_states=False) # Instance with batch index 2 needed too many steps to finish, and batch index 3 had no # path to get to a finished state. (See the simple transition system definition; goal is # to end up at 4, actions are either add one or two to starting value.) assert len(best_states) == 2 assert best_states[0][0].action_history[0] == [-1, 1, 3, 4] assert best_states[1][0].action_history[0] == [3, 4] best_states = beam_search.search(5, initial_state, decoder_step, keep_final_unfinished_states=True) # Now we're keeping final unfinished states, which allows a "best state" for the instances # that didn't have one before. Our previous best states for the instances that finish # doesn't change, because the score for taking another step is always negative at these # values. assert len(best_states) == 4 assert best_states[0][0].action_history[0] == [-1, 1, 3, 4] assert best_states[1][0].action_history[0] == [3, 4] assert best_states[2][0].action_history[0] == [-18, -16, -14, -12, -10] assert best_states[3][0].action_history[0] == [7, 9, 11, 13, 15]
def from_params(cls, vocab, params: Params) -> 'WikiTablesSemanticParser': question_embedder = TextFieldEmbedder.from_params(vocab, params.pop("question_embedder")) action_embedding_dim = params.pop_int("action_embedding_dim") encoder = Seq2SeqEncoder.from_params(params.pop("encoder")) entity_encoder = Seq2VecEncoder.from_params(params.pop('entity_encoder')) max_decoding_steps = params.pop_int("max_decoding_steps") mixture_feedforward_type = params.pop('mixture_feedforward', None) if mixture_feedforward_type is not None: mixture_feedforward = FeedForward.from_params(mixture_feedforward_type) else: mixture_feedforward = None decoder_beam_search = BeamSearch.from_params(params.pop("decoder_beam_search")) # If no attention function is specified, we should not use attention, not attention with # default similarity function. attention_function_type = params.pop("attention_function", None) if attention_function_type is not None: attention_function = SimilarityFunction.from_params(attention_function_type) else: attention_function = None dropout = params.pop_float('dropout', 0.0) num_linking_features = params.pop_int('num_linking_features', 8) rule_namespace = params.pop('rule_namespace', 'rule_labels') params.assert_empty(cls.__name__) return cls(vocab, question_embedder=question_embedder, action_embedding_dim=action_embedding_dim, encoder=encoder, entity_encoder=entity_encoder, mixture_feedforward=mixture_feedforward, decoder_beam_search=decoder_beam_search, max_decoding_steps=max_decoding_steps, attention_function=attention_function, dropout=dropout, num_linking_features=num_linking_features, rule_namespace=rule_namespace)
def from_params(cls, vocab, params: Params) -> 'WikiTablesMmlSemanticParser': question_embedder = TextFieldEmbedder.from_params(vocab, params.pop("question_embedder")) action_embedding_dim = params.pop_int("action_embedding_dim") encoder = Seq2SeqEncoder.from_params(params.pop("encoder")) entity_encoder = Seq2VecEncoder.from_params(params.pop('entity_encoder')) max_decoding_steps = params.pop_int("max_decoding_steps") mixture_feedforward_type = params.pop('mixture_feedforward', None) if mixture_feedforward_type is not None: mixture_feedforward = FeedForward.from_params(mixture_feedforward_type) else: mixture_feedforward = None decoder_beam_search = BeamSearch.from_params(params.pop("decoder_beam_search")) input_attention = Attention.from_params(params.pop("attention")) training_beam_size = params.pop_int('training_beam_size', None) use_neighbor_similarity_for_linking = params.pop_bool('use_neighbor_similarity_for_linking', False) dropout = params.pop_float('dropout', 0.0) num_linking_features = params.pop_int('num_linking_features', 10) tables_directory = params.pop('tables_directory', '/wikitables/') rule_namespace = params.pop('rule_namespace', 'rule_labels') params.assert_empty(cls.__name__) return cls(vocab, question_embedder=question_embedder, action_embedding_dim=action_embedding_dim, encoder=encoder, entity_encoder=entity_encoder, mixture_feedforward=mixture_feedforward, decoder_beam_search=decoder_beam_search, max_decoding_steps=max_decoding_steps, input_attention=input_attention, training_beam_size=training_beam_size, use_neighbor_similarity_for_linking=use_neighbor_similarity_for_linking, dropout=dropout, num_linking_features=num_linking_features, tables_directory=tables_directory, rule_namespace=rule_namespace)
def from_params(cls, vocab: Vocabulary, params: Params) -> 'ProStructModel': embedder_params = params.pop("text_field_embedder") text_field_embedder = TextFieldEmbedder.from_params(vocab, embedder_params) use_attention = params.pop("use_attention") seq2seq_encoder_params = params.pop("seq2seq_encoder") seq2vec_encoder_params = params.pop("seq2vec_encoder") seq2seq_encoder = None seq2vec_encoder = None if use_attention: seq2seq_encoder = Seq2SeqEncoder.from_params(seq2seq_encoder_params) else: seq2vec_encoder = Seq2VecEncoder.from_params(seq2vec_encoder_params) # Initialize params for location span related layers span_end_encoder_after = Seq2SeqEncoder.from_params(params.pop("span_end_encoder_after")) initializer = InitializerApplicator.from_params(params.pop("initializer", [])) use_decoder_trainer = params.pop("use_decoder_trainer") decoder_beam_search = None if use_decoder_trainer: decoder_beam_search = BeamSearch.from_params(params.pop("decoder_beam_search")) kb_configs = params.pop("kb_configs", { "kb_to_use": "lexicalkb", "lexical_kb_path": "tests/fixtures/decoder_data/kbs/kb3/lexical-kb-v0.tsv", "partial_grids_path": "tests/fixtures/decoder_data/kbs/kb2/kb2-partialgrids.tsv", "partialgrid_prompts_load_path": "tests/fixtures/decoder_data/kbs/kb2/partial-grids.tsv", "fullgrid_prompts_load_path": "tests/fixtures/decoder_data/kbs/kb2/full-grids.tsv" }) # AllenNLP predictors requires no change in a serialized model # Making this more flexible by adding other_configs as a dict. other_configs = params.pop("other_configs", {}) return cls(vocab=vocab, text_field_embedder=text_field_embedder, seq2seq_encoder=seq2seq_encoder, seq2vec_encoder=seq2vec_encoder, use_attention=use_attention, span_end_encoder_after=span_end_encoder_after, use_decoder_trainer=use_decoder_trainer, decoder_beam_search=decoder_beam_search, kb_configs=kb_configs, other_configs=other_configs, initializer=initializer)
def from_params(cls, vocab, params: Params) -> 'NlvrDirectSemanticParser': sentence_embedder_params = params.pop("sentence_embedder") sentence_embedder = TextFieldEmbedder.from_params( vocab, sentence_embedder_params) action_embedding_dim = params.pop_int('action_embedding_dim') encoder = Seq2SeqEncoder.from_params(params.pop("encoder")) dropout = params.pop_float('dropout', 0.0) input_attention = Attention.from_params(params.pop("attention")) decoder_beam_search = BeamSearch.from_params( params.pop("decoder_beam_search")) max_decoding_steps = params.pop_int("max_decoding_steps") params.assert_empty(cls.__name__) return cls(vocab, sentence_embedder=sentence_embedder, action_embedding_dim=action_embedding_dim, encoder=encoder, input_attention=input_attention, decoder_beam_search=decoder_beam_search, max_decoding_steps=max_decoding_steps, dropout=dropout)
def test_search(self): beam_search = BeamSearch.from_params(Params({'beam_size': 4})) initial_state = SimpleDecoderState([0, 1, 2, 3], [[], [], [], []], [torch.Tensor([0.0]), torch.Tensor([0.0]), torch.Tensor([0.0]), torch.Tensor([0.0])], [-3, 1, -20, 5]) decoder_step = SimpleDecoderStep(include_value_in_score=True) best_states = beam_search.search(5, initial_state, decoder_step, keep_final_unfinished_states=False) # Instance with batch index 2 needed too many steps to finish, and batch index 3 had no # path to get to a finished state. (See the simple transition system definition; goal is # to end up at 4, actions are either add one or two to starting value.) assert len(best_states) == 2 assert best_states[0][0].action_history[0] == [-1, 1, 3, 4] assert best_states[1][0].action_history[0] == [3, 4] best_states = beam_search.search(5, initial_state, decoder_step, keep_final_unfinished_states=True) # Now we're keeping final unfinished states, which allows a "best state" for the instances # that didn't have one before. Our previous best states for the instances that finish # doesn't change, because the score for taking another step is always negative at these # values. assert len(best_states) == 4 assert best_states[0][0].action_history[0] == [-1, 1, 3, 4] assert best_states[1][0].action_history[0] == [3, 4] assert best_states[2][0].action_history[0] == [-18, -16, -14, -12, -10] assert best_states[3][0].action_history[0] == [7, 9, 11, 13, 15]
def test_with_model_scores_3steps(self): beam_search = BeamSearch.from_params(Params({'beam_size': 1})) max_num_steps = 3 num_participants = 2 num_labels = 2 # (Create/0, Destroy/1) # supplies parti embedding: group_size, nparti, num_tokens_p * token_embedding_dim # 1, num_p = 2, max_tokens_p = 1, token_embedding= size 3 participants_embedding = [[[0.11, 0.21, 0.31]], [[0.12, 0.22, 0.32]]] # shape: 2 (batchsize), 2 (steps), 3 (participants), 4 (labels) # action_logits = [[[0.1, 0.7], [0.8, 0.2]], [[0.3, 0.4], [0.4, 0.6]], [[0.2, 0.4], [0.5, 0.4]]] logit_tensor = torch.autograd.Variable(torch.Tensor([action_logits])) grouped_initial_state = ProParaDecoderState( # 2 labels (C, D) ^2 participants=4 states per step. # These four can be grouped into one grouped_initial_state group_indices=[0], # Initially, there is no action history per group item. action_history=[ [], # history for group 0 # [] # history for group 1 ], # shape (num_groups, num_participants) participant_score_per_group=[ torch.autograd.Variable( torch.Tensor([0.0 for _ in range(num_participants)])) ], # # Initial scores for all "num_participants" participants. # # Perhaps we should read these off from model_scores. # # or call action_scorer on the initial state? # participant_score_per_group=[ # [], # score for group 0 # # [] # score for group 1 # ], participants_embedding=participants_embedding, # action_logits=action_logits, # shape: 2 (batchsize), 2 (steps), 3 (participants), 4 (labels) logit_tensor=logit_tensor, instance_id=None, metadata={'in_beam_search': True}, # Initial states: containing labels for every participant. # Perhaps the action generator should generate all possible states for step 0. # and start_values be None. overall_score=None, start_values=None) decoder_step = ProParaDecoderStep( # KBBasedActionScorer(), ActionScorerDummy(), DummyConstrainedStepper(num_labels, num_participants)) best_states = beam_search.search(max_num_steps, grouped_initial_state, decoder_step, keep_final_unfinished_states=False) # "best state" for all instances batch_wise # This passed before adding action logits. for k, v in best_states.items(): # v[0] is the highest scored state in this step. # action_history[0] is the zeroth group element (note that the finished states have exactly one group elem) if k == 0: assert v[0].action_history[0] == [[1, 0], [1, 1], [1, 0]] #assert v[0].score[0].data[0] == pytest.approx(-2.117511510848999) assert len(best_states) == 1