Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Disentangled checked_sentence_as_label. (#4000)
Browse files Browse the repository at this point in the history
* Disentangled checked_sentence_as_label. Before it also added the label to the input.

* wiint+wow: more knowledge pred changes (#3999)

* Disentangled checked_sentence_as_label. Before it also added the label to the input.

* Fixed bug in mutators that caused them to copy the adjusted label in the next context. Unified mutators across WizWiki and WizInt.

* Fixed merge problem.

Co-authored-by: Leonard Adolphs <ladolphs@devfair0319.h2.fair>
Co-authored-by: jaseweston <jase@fb.com>
  • Loading branch information
3 people authored Sep 9, 2021
1 parent ebcd25e commit 034e3e4
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 70 deletions.
82 changes: 44 additions & 38 deletions parlai/tasks/wizard_of_internet/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
from parlai.utils.data import DatatypeHelper
import parlai.utils.logging as logging
import parlai.tasks.wizard_of_internet.constants as CONST
from parlai.core.mutators import register_mutator, MessageMutator
from parlai.core.mutators import register_mutator
from parlai.tasks.wizard_of_wikipedia.agents import (
AddLabel as AddLabelWizWiki,
AddLabelLM as AddLabelLMWizWiki,
CheckedSentenceAsLabel as CheckedSentenceAsLabelWizWiki,
AddCheckedSentence as AddCheckedSentenceWizWiki,
)

import random
from .build import build
Expand Down Expand Up @@ -575,60 +581,60 @@ def _knowledge_piece(self):
return CONST.SELECTED_DOCS_TITLES


@register_mutator("checked_sentence_as_label")
class CheckedSentenceAsLabel(MessageMutator):
@register_mutator("add_checked_sentence_to_input")
class AddCheckedSentence(AddCheckedSentenceWizWiki):
"""
Sets the checked sentences as the label, and the label to the end of text.
Adds the checked sentence to the end of the text.
E.g. run with: parlai display_data -t wizard_of_internet -n 100 -dt valid --mutators
flatten,checked_sentence_as_label
flatten,add_checked_sentence_to_input
"""

def message_mutation(self, message: Message) -> Message:
original_message = message.copy()
try:
text = message.pop('text')
label = message.pop('labels')[0]
checked_sentence = ' '.join(message.get(CONST.SELECTED_SENTENCES, ''))
@property
def checked_sentence_kword(self):
return CONST.SELECTED_SENTENCES


text += f'\n__label__ {label} __endlabel__'
message['text'] = text
@register_mutator("checked_sentence_as_label")
class CheckedSentenceAsLabel(CheckedSentenceAsLabelWizWiki):
"""
Uses the checked sentence (knowledge) as label.
message['labels'] = [checked_sentence]
except KeyError:
return original_message
E.g. run with: parlai display_data -t wizard_of_internet -n 100 -dt valid --mutators
flatten,checked_sentence_as_label
"""

return message
@property
def checked_sentence_kword(self):
return CONST.SELECTED_SENTENCES


@register_mutator("checked_sentence_as_label_lm")
class CheckedSentenceAsLabelLm(MessageMutator):
@register_mutator("add_label_to_input")
class AddLabel(AddLabelWizWiki):
"""
Sets the checked sentences as the label, and the label to the end of text. Language
modeling version where a random piece of the label is sampled in the input.
Adds the dialogue sentence to the input.
E.g. run with: parlai display_data -t wizard_of_internet -n 100 -dt valid --mutators
flatten,checked_sentence_as_label_lm
flatten,checked_sentence_as_label,add_label_to_input
"""

def message_mutation(self, message: Message) -> Message:
original_message = message.copy()
try:
text = message.pop('text')
label = message.pop('labels')[0]
checked_sentence = ' '.join(message.get(CONST.SELECTED_SENTENCES, ''))
pass


ls = label.split(' ')
ind = random.randint(0, len(ls) - 1)
@register_mutator("add_label_to_input_lm")
class AddLabelLM(AddLabelLMWizWiki):
"""
Adds the dialogue sentence to the input (language modeling version).
label1 = ' '.join(ls[0:ind])
label2 = ' '.join(ls[ind : len(ls)])
Language modeling version where a random piece of the label is sampled in
the input. The rest is placed inside special tokens.
text += f'\n{label1}\n__label__ {label2} __endlabel__'
message['text'] = text
E.g. run with: parlai display_data -t wizard_of_internet -n 100 -dt valid --mutators
flatten,add_label_to_input_lm
message['labels'] = [checked_sentence]
except KeyError:
return original_message
To add the checked sentence as the label, use:
parlai display_data -t wizard_of_internet -n 100 -dt valid --mutators
flatten,add_label_to_input_lm,checked_sentence_as_label
"""

return message
pass
105 changes: 73 additions & 32 deletions parlai/tasks/wizard_of_wikipedia/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,76 +1279,117 @@ class SelfchatTeacher(BasicBothDialogTeacher):
pass


@register_mutator("add_checked_sentence")
@register_mutator("add_checked_sentence_to_input")
class AddCheckedSentence(MessageMutator):
"""
Adds the checked sentence to the end of text.
Adds the checked sentence to the end of the text.
But only a single time.
"""

@property
def checked_sentence_kword(self):
return 'checked_sentence'

def message_mutation(self, message: Message) -> Message:
original_message = message.copy()
new_message = message.copy()
if 'text' not in message:
return original_message
text = message.pop('text')
checked_sentence = message.get('checked_sentence', '')
return message
text = new_message.pop('text')
checked_sentence = new_message.get(self.checked_sentence_kword, '')
if isinstance(checked_sentence, list):
checked_sentence = ' '.join(checked_sentence)

text += f'\n{TOKEN_KNOWLEDGE} {checked_sentence} {TOKEN_END_KNOWLEDGE}'
message['text'] = text
new_message['text'] = text

return message
return new_message


@register_mutator("checked_sentence_as_label")
class CheckedSentenceAsLabel(MessageMutator):
"""
Adds the dialogue sentence to the end of the text. But only a single time.
The label then becomes the checked sentence.
Uses the checked sentence (knowledge) as label.
"""

@property
def checked_sentence_kword(self):
return 'checked_sentence'

def message_mutation(self, message: Message) -> Message:
new_message = message.copy()
if 'text' not in message or 'labels' not in message or not message['labels']:
return message
labels = new_message.pop('labels')
checked_sentence = new_message.get(self.checked_sentence_kword, '')
if isinstance(checked_sentence, list):
checked_sentence = ' '.join(checked_sentence)

new_message['dialogue_response'] = labels
new_message['labels'] = [checked_sentence]
return new_message


@register_mutator("add_label_to_input")
class AddLabel(MessageMutator):
"""
Adds the dialogue sentence to the input.
But only a single time.
"""

def message_mutation(self, message: Message) -> Message:
original_message = message.copy()
new_message = message.copy()
if 'text' not in message or 'labels' not in message or not message['labels']:
return original_message
text = message.pop('text')
labels = message.pop('labels')
return message
if 'dialogue_response' in new_message:
# checked_sentence_as_label was applied before
labels = new_message['dialogue_response']
else:
labels = new_message['labels']
dialogue_response = labels[0]
checked_sentence = message.get('checked_sentence', '')
text = new_message.pop('text')

text += f'\n{TOKEN_LABEL} {dialogue_response} {TOKEN_END_LABEL}'
message['text'] = text
message['labels'] = [checked_sentence]
new_message['text'] = text

return message
return new_message


@register_mutator("checked_sentence_as_label_lm")
class CheckedSentenceAsLabelLm(MessageMutator):
@register_mutator("add_label_to_input_lm")
class AddLabelLM(MessageMutator):
"""
Sets the checked sentences as the label, and the label to the end of text. Language
modeling version where a random piece of the label is sampled in the input.
Adds the dialogue sentence to the input (language modeling version).
Language modeling version where a random piece of the label is sampled in
the input. The rest is placed inside special tokens.
E.g. run with: parlai display_data -t wizard_of_wikipedia -n 100 -dt valid --mutators
flatten,checked_sentence_as_label_lm --add-missing-turns all
flatten,add_label_to_input_lm
To add the checked sentence as the label, use:
parlai display_data -t wizard_of_wikipedia -n 100 -dt valid --mutators
flatten,add_label_to_input_lm,checked_sentence_as_label
"""

def message_mutation(self, message: Message) -> Message:
original_message = message.copy()
new_message = message.copy()
if 'text' not in message or 'labels' not in message or not message['labels']:
return original_message
text = message.pop('text')
labels = message.pop('labels')
return message
if 'dialogue_response' in new_message:
# checked_sentence_as_label was applied before
labels = new_message['dialogue_response']
else:
labels = new_message['labels']
dialogue_response = labels[0]
checked_sentence = message.get('checked_sentence', '')
text = new_message.pop('text')

ls = dialogue_response.split(' ')
ls = dialogue_response.split()
ind = random.randint(0, len(ls) - 1)
label1 = ' '.join(ls[0:ind])
label2 = ' '.join(ls[ind : len(ls)])

text += f'\n{label1}\n{TOKEN_LABEL} {label2} {TOKEN_END_LABEL}'
message['text'] = text
message['labels'] = [checked_sentence]
new_message['text'] = text

return message
return new_message

0 comments on commit 034e3e4

Please sign in to comment.