Skip to content
This repository has been archived by the owner on Aug 22, 2019. It is now read-only.

Commit

Permalink
Merge f699179 into 065bf7d
Browse files Browse the repository at this point in the history
  • Loading branch information
ricwo committed Jan 2, 2019
2 parents 065bf7d + f699179 commit 72c4cd4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Expand Up @@ -22,6 +22,7 @@ Added
the intent right
- Additional checks in PolicyEnsemble to ensure that custom Policy
classes' load function returns the correct type
- Entries for tensorflow and sklearn versions to the policy metadata

Removed
-------
Expand Down
34 changes: 24 additions & 10 deletions rasa_core/policies/ensemble.py
@@ -1,13 +1,14 @@
import sys
from collections import defaultdict

import importlib
import json
import logging
import numpy as np
import os
import sys
from collections import defaultdict
from datetime import datetime
from typing import Text, Optional, Any, List, Dict, Tuple

import numpy as np

import rasa_core
from rasa_core import utils, training, constants
from rasa_core.actions.action import ACTION_LISTEN_NAME
Expand All @@ -26,6 +27,8 @@


class PolicyEnsemble(object):
versioned_packages = ["rasa_core", "tensorflow", "sklearn"]

def __init__(self,
policies: List[Policy],
action_fingerprints: Optional[Dict] = None) -> None:
Expand Down Expand Up @@ -97,12 +100,22 @@ def _create_action_fingerprints(training_events):
action_fingerprints[k] = {"slots": slots}
return action_fingerprints

def _add_package_version_info(self, metadata: Dict[Text, Any]) -> None:
"""Adds version info for self.versioned_packages to metadata."""

for package_name in self.versioned_packages:
try:
p = importlib.import_module(package_name)
metadata[package_name] = p.__version__
except ImportError:
pass

def _persist_metadata(self,
path: Text,
dump_flattened_stories: bool = False) -> None:
"""Persists the domain specification to storage."""

# make sure the directory we persist to exists
# make sure the directory we persist exists
domain_spec_path = os.path.join(path, 'policy_metadata.json')
training_data_path = os.path.join(path, 'stories.md')
utils.create_dir_for_file(domain_spec_path)
Expand All @@ -117,22 +130,24 @@ def _persist_metadata(self,

metadata = {
"action_fingerprints": action_fingerprints,
"rasa_core": rasa_core.__version__,
"python": ".".join([str(s) for s in sys.version_info[:3]]),
"max_histories": self._max_histories(),
"ensemble_name": self.__module__ + "." + self.__class__.__name__,
"policy_names": policy_names,
"trained_at": self.date_trained
}

self._add_package_version_info(metadata)

utils.dump_obj_as_json_to_file(domain_spec_path, metadata)

# if there are lots of stories, saving flattened stories takes a long
# time, so this is turned off by default
if dump_flattened_stories:
training.persist_data(self.training_trackers, training_data_path)

def persist(self, path: Text, dump_flattened_stories: bool = False) -> None:
def persist(self, path: Text,
dump_flattened_stories: bool = False) -> None:
"""Persists the policy to storage."""

self._persist_metadata(path, dump_flattened_stories)
Expand Down Expand Up @@ -173,13 +188,12 @@ def _ensure_loaded_policy(cls, policy, policy_cls, policy_name: Text):
if policy is None:
raise Exception(
"Failed to load policy {}: "
"load returned None"
.format(policy_name))
"load returned None".format(policy_name))
elif not isinstance(policy, policy_cls):
raise Exception(
"Failed to load policy {}: "
"load returned object that is not instance of its own class"
.format(policy_name))
"".format(policy_name))

@classmethod
def load(cls, path: Text) -> 'PolicyEnsemble':
Expand Down
2 changes: 1 addition & 1 deletion rasa_core/utils.py
Expand Up @@ -96,7 +96,7 @@ def class_from_module_path(module_path: Text) -> Any:


def module_path_from_instance(inst: Any) -> Text:
"""Return the module path of an instances class."""
"""Return the module path of an instance's class."""
return inst.__module__ + "." + inst.__class__.__name__


Expand Down

0 comments on commit 72c4cd4

Please sign in to comment.