Label should be added to the dialog history instead of model's prediction
from parlai.core.agents import Agent
from parlai.core.dict import DictionaryAgent
+from parlai.core.utils import maintain_dialog_history
import torch
from torch import optim
@@ -51,6 +52,12 @@ class MemnnAgent(Agent):
help='disable GPUs even if available')
arg_group.add_argument('--gpu', type=int, default=-1,
help='which GPU device to use')
+ arg_group.add_argument('-hist', '--history-length', default=100000, type=int,
+ help='Number of past tokens to remember. '
+ 'Default remembers 100000 tokens.')
+ arg_group.add_argument('-histr', '--history-replies', default='label', type=str,
+ choices=['none', 'model', 'label'],
+ help='Keep replies in the history, or not.')
def __init__(self, opt, shared=None):
opt['cuda'] = not opt['no_cuda'] and torch.cuda.is_available()
@@ -104,6 +111,7 @@ class MemnnAgent(Agent):
else:
self.answers = shared['answers']
+ self.history = {}
self.episode_done = True
self.last_cands, self.last_cands_list = None, None
super().__init__(opt, shared)
@@ -114,19 +122,24 @@ class MemnnAgent(Agent):
return shared
def observe(self, observation):
- observation = copy.copy(observation)
- if not self.episode_done and not observation.get('preprocessed', False):
- # if the last example wasn't the end of an episode, then we need to
- # recall what was said in that example
- prev_dialogue = self.observation['text'] if self.observation is not None else ''
+ """Save observation for act.
+ If multiple observations are from the same episode, concatenate them.
+ """
+ self.episode_done = observation['episode_done']
+ # shallow copy observation (deep copy can be expensive)
+ obs = observation.copy()
batch_idx = self.opt.get('batchindex', 0)
- if self.answers[batch_idx] is not None:
- prev_dialogue += '\n' + self.answers[batch_idx]
+
+ obs['text'] = '\n'.join(maintain_dialog_history(
+ self.history, obs,
+ reply=self.answers[batch_idx] if self.answers[batch_idx] is not None else '',
+ historyLength=self.opt['history_length'],
+ useReplies=self.opt['history_replies'],
+ dict=None, useStartEndIndices=False))
+
+ self.observation = obs
self.answers[batch_idx] = None
- observation['text'] = prev_dialogue + '\n' + observation['text']
- self.observation = observation
- self.episode_done = observation['episode_done']
- return observation
+ return obs
def predict(self, xs, cands, ys=None):
is_training = ys is not None
