Esempio n. 1
0
    def load(cls, path):
        # type: (Text) -> KerasPolicy
        from tensorflow.keras.models import load_model

        if os.path.exists(path):
            featurizer = TrackerFeaturizer.load(path)
            meta_path = os.path.join(path, "keras_policy.json")
            if os.path.isfile(meta_path):
                with io.open(meta_path) as f:
                    meta = json.loads(f.read())

                model_file = os.path.join(path, meta["model"])

                graph = tf.Graph()
                with graph.as_default():
                    session = tf.Session()
                    with session.as_default():
                        model = load_model(model_file)

                return cls(featurizer=featurizer,
                           model=model,
                           graph=graph,
                           session=session,
                           current_epoch=meta["epochs"])
            else:
                return cls(featurizer=featurizer)
        else:
            raise Exception("Failed to load dialogue model. Path {} "
                            "doesn't exist".format(os.path.abspath(path)))
Esempio n. 2
0
    def load(cls, path):
        # type: (Text) -> MemoizationPolicy

        featurizer = TrackerFeaturizer.load(path)
        memorized_file = os.path.join(path, 'memorized_turns.json')
        if os.path.isfile(memorized_file):
            with io.open(memorized_file) as f:
                data = json.loads(f.read())
            return cls(featurizer=featurizer, lookup=data["lookup"])
        else:
            logger.info("Couldn't load memoization for policy. "
                        "File '{}' doesn't exist. Falling back to empty "
                        "turn memory.".format(memorized_file))
            return cls()
Esempio n. 3
0
    def load(cls, path):
        # type: (Text) -> MemoizationPolicy

        featurizer = TrackerFeaturizer.load(path)
        memorized_file = os.path.join(path, 'memorized_turns.json')
        if os.path.isfile(memorized_file):
            with io.open(memorized_file) as f:
                data = json.loads(f.read())
            return cls(featurizer=featurizer, lookup=data["lookup"])
        else:
            logger.info("Couldn't load memoization for policy. "
                        "File '{}' doesn't exist. Falling back to empty "
                        "turn memory.".format(memorized_file))
            return cls()
Esempio n. 4
0
    def load(cls, path: Text) -> 'MemoizationPolicy':

        featurizer = TrackerFeaturizer.load(path)
        memorized_file = os.path.join(path, 'memorized_turns.json')
        if os.path.isfile(memorized_file):
            data = json.loads(utils.read_file(memorized_file))
            return cls(featurizer=featurizer,
                       priority=data["priority"],
                       lookup=data["lookup"])
        else:
            logger.info("Couldn't load memoization for policy. "
                        "File '{}' doesn't exist. Falling back to empty "
                        "turn memory.".format(memorized_file))
            return cls()
Esempio n. 5
0
 def load(cls, path):
     # type: (Text) -> TorchPolicy
     if os.path.exists(path):
         featurizer = TrackerFeaturizer.load(path)
         meta_path = os.path.join(path, "torch_policy.json")
         if os.path.isfile(meta_path):
             with io.open(meta_path) as f:
                 meta = json.loads(f.read())
             model_file = os.path.join(path, meta["model"])
             model = torch.load(model_file)
             return cls(featurizer=featurizer, model=model)
         else:
             return cls(featurizer=featurizer)
     else:
         raise Exception("path {} does not exist".format(path))
Esempio n. 6
0
 def load(cls, path):
     # type: (Text) -> KerasPolicy
     if os.path.exists(path):
         featurizer = TrackerFeaturizer.load(path)
         meta_path = os.path.join(path, "keras_policy.json")
         if os.path.isfile(meta_path):
             with io.open(meta_path) as f:
                 meta = json.loads(f.read())
             model_arch = cls._load_model_arch(path, meta)
             return cls(featurizer=featurizer,
                        model=cls._load_weights_for_model(
                            path, model_arch, meta),
                        current_epoch=meta["epochs"])
         else:
             return cls(featurizer=featurizer)
     else:
         raise Exception("Failed to load dialogue model. Path {} "
                         "doesn't exist".format(os.path.abspath(path)))
Esempio n. 7
0
    def load(cls, path: Text) -> Policy:
        filename = os.path.join(path, 'sklearn_model.pkl')
        if not os.path.exists(path):
            raise OSError("Failed to load dialogue model. Path {} "
                          "doesn't exist".format(os.path.abspath(filename)))

        featurizer = TrackerFeaturizer.load(path)
        assert isinstance(featurizer, MaxHistoryTrackerFeaturizer), \
            ("Loaded featurizer of type {}, should be "
             "MaxHistoryTrackerFeaturizer.".format(type(featurizer).__name__))

        policy = cls(featurizer=featurizer)

        with open(filename, 'rb') as f:
            state = pickle.load(f)
        vars(policy).update(state)

        logger.info("Loaded sklearn model")
        return policy
Esempio n. 8
0
 def load(cls, path):
     # type: (Text) -> KerasPolicy
     if os.path.exists(path):
         featurizer = TrackerFeaturizer.load(path)
         meta_path = os.path.join(path, "keras_policy.json")
         if os.path.isfile(meta_path):
             with io.open(meta_path) as f:
                 meta = json.loads(f.read())
             model_arch = cls._load_model_arch(path, meta)
             return cls(
                     featurizer=featurizer,
                     model=cls._load_weights_for_model(path,
                                                       model_arch,
                                                       meta),
                     current_epoch=meta["epochs"])
         else:
             return cls(featurizer=featurizer)
     else:
         raise Exception("Failed to load dialogue model. Path {} "
                         "doesn't exist".format(os.path.abspath(path)))
Esempio n. 9
0
    def load(cls, path):
        # type: (Text) -> Policy
        filename = os.path.join(path, 'sklearn_model.pkl')
        if not os.path.exists(path):
            raise OSError("Failed to load dialogue model. Path {} "
                          "doesn't exist".format(os.path.abspath(filename)))

        featurizer = TrackerFeaturizer.load(path)
        assert isinstance(featurizer, MaxHistoryTrackerFeaturizer), \
            ("Loaded featurizer of type {}, should be "
             "MaxHistoryTrackerFeaturizer.".format(type(featurizer).__name__))

        policy = cls(featurizer=featurizer)

        with open(filename, 'rb') as f:
            state = pickle.load(f)
        vars(policy).update(state)

        logger.info("Loaded sklearn model")
        return policy
Esempio n. 10
0
def test_fail_to_load_non_existent_featurizer():
    assert TrackerFeaturizer.load("non_existent_class") is None
Esempio n. 11
0
def test_fail_to_load_non_existent_featurizer():
    assert TrackerFeaturizer.load("non_existent_class") is None