class_docstr: Basic tests on the util functions in TorchAgent.
class_docstr_tok: ["Basic","tests","on","the","util","functions","in","TorchAgent","."]
docstr: Test the vectorization of observations.
  
  Make sure they do not recompute results, and respect the different param
  options.
docstr_tok: ["Test","the","vectorization","of","observations",".","Make","sure","they","do","not","recompute","results",",","and","respect","the","different","param","options","."]
code: def test_vectorize(self):
      """
      Test the vectorization of observations.
  
      Make sure they do not recompute results, and respect the different param
      options.
      """
      agent = get_agent()
      obs_labs = Message(
          {
              "text": "No. Try not.",
              "labels": ["Do.", "Do not."],
              "episode_done": True,
          }
      )
      obs_elabs = Message(
          {
              "text": "No. Try not.",
              "eval_labels": ["Do.", "Do not."],
              "episode_done": True,
          }
      )
  
      for obs in (obs_labs, obs_elabs):
          lab_key = "labels" if "labels" in obs else "eval_labels"
          lab_vec = lab_key + "_vec"
          lab_chc = lab_key + "_choice"
  
          inp = obs.copy()
          # test add_start=True, add_end=True
          agent.history.reset()
          agent.history.update_history(inp)
          out = agent.vectorize(
              inp, agent.history, add_start=True, add_end=True
          )
          self.assertEqual(out["text_vec"].tolist(), [1, 2, 3])
          # note that label could be either label above
          self.assertEqual(out[lab_vec][0].item(), MockDict.BEG_IDX)
          self.assertEqual(out[lab_vec][1].item(), 1)
          self.assertEqual(out[lab_vec][-1].item(), MockDict.END_IDX)
          self.assertEqual(out[lab_chc][:2], "Do")
  
          # test add_start=True, add_end=False
          inp = obs.copy()
          out = agent.vectorize(
              inp, agent.history, add_start=True, add_end=False
          )
          self.assertEqual(out["text_vec"].tolist(), [1, 2, 3])
          # note that label could be either label above
          self.assertEqual(out[lab_vec][0].item(), MockDict.BEG_IDX)
          self.assertNotEqual(out[lab_vec][-1].item(), MockDict.END_IDX)
          self.assertEqual(out[lab_chc][:2], "Do")
  
          # test add_start=False, add_end=True
          inp = obs.copy()
          out = agent.vectorize(
              inp, agent.history, add_start=False, add_end=True
          )
          self.assertEqual(out["text_vec"].tolist(), [1, 2, 3])
          # note that label could be either label above
          self.assertNotEqual(out[lab_vec][0].item(), MockDict.BEG_IDX)
          self.assertEqual(out[lab_vec][-1].item(), MockDict.END_IDX)
          self.assertEqual(out[lab_chc][:2], "Do")
  
          # test add_start=False, add_end=False
          inp = obs.copy()
          out = agent.vectorize(
              inp, agent.history, add_start=False, add_end=False
          )
          self.assertEqual(out["text_vec"].tolist(), [1, 2, 3])
          # note that label could be either label above
          self.assertNotEqual(out[lab_vec][0].item(), MockDict.BEG_IDX)
          self.assertNotEqual(out[lab_vec][-1].item(), MockDict.END_IDX)
          self.assertEqual(out[lab_chc][:2], "Do")
  
          # test caching of tensors
          out_again = agent.vectorize(out, agent.history)
          # should have cached result from before
          self.assertIs(out["text_vec"], out_again["text_vec"])
          self.assertEqual(out["text_vec"].tolist(), [1, 2, 3])
          # next: should truncate cached result
          prev_vec = out["text_vec"]
          out_again = agent.vectorize(
              out, agent.history, text_truncate=1
          )
          self.assertIsNot(prev_vec, out_again["text_vec"])
          self.assertEqual(out["text_vec"].tolist(), [3])
  
      # test split_lines
      agent = get_agent(split_lines=True)
      obs = Message(
          {
              "text": "Hello.\nMy name is Inogo Montoya.\n"
              "You killed my father.\nPrepare to die.",
              "episode_done": True,
          }
      )
      agent.history.update_history(obs)
      vecs = agent.history.get_history_vec_list()
      self.assertEqual(
          vecs, [[1], [1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2, 3]]
      )
  
      # check cache
      out_again = agent.vectorize(obs, agent.history)
      vecs = agent.history.get_history_vec_list()
      self.assertEqual(
          vecs, [[1], [1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2, 3]]
      )
  
imports: import os
  import unittest
  from parlai.core.agents import create_agent_from_shared
  from parlai.utils.testing import tempdir
  from parlai.utils.misc import Message
label_desc: Use image node_id 5 for calling the TestTorchAgent obj's underlying member method code with example usage: obj.test_vectorize() without return types