def train_dialogue(domain_file = 'customer_domain.yml', model_path = './models/dialogue', training_data_file = 'stories.md'): ''' This function uses the intent clasifier to build responses to sent messages input : domain_file : containing the actions, templates , entities necessary for underdtanding the dialogue.The file is located in the dialogue system model_path : directory for saving trained dialogue system training_data_file : contains the story file. It is training file needed to train rasa core dialogue system ''' featurizer = MaxHistoryTrackerFeaturizer(BinarySingleStateFeaturizer(), max_history=5) # define featurizer for learning # define a bot agent agent = Agent(domain_file, policies = [MemoizationPolicy(), KerasPolicy(featurizer, epochs=300, batch_size=10)]) # asyncio is needed for concurrent wait during training loop = asyncio.get_event_loop() # define asyncio data = loop.run_until_complete( agent.load_data(training_data_file )) # embedded data in asyncio # let agent train agent.train( data, validation=0.2) # persistent saving of model agent.persist(model_path) # return agent return agent
def train_core( domain_file: Text = "domain.yml", model_directory: Text = "models", model_name: Text = "current", training_data_file: Text = "data/stories.md", ): agent = Agent( domain_file, policies=[ MemoizationPolicy(max_history=3), MappingPolicy(), RestaurantPolicy(batch_size=100, epochs=100, validation_split=0.2), ], ) # augmentation_factor 扩展系数 10 training_data = agent.load_data(training_data_file, augmentation_factor=10) agent.train(training_data) # Attention: agent.persist stores the model and all meta data into a folder. # The folder itself is not zipped. 未打包的的模型文件 model_path = os.path.join(model_directory, model_name, "core") agent.persist(model_path) logger.info("Model trained. Stored in '{}'.".format(model_path)) return model_path
async def train_core(domain_file="domain.yml", model_directory="models", model_name="current", training_data_file="data/stories.md"): agent = Agent( domain_file, policies=[ MemoizationPolicy(max_history=3), MappingPolicy(), RestaurantPolicy(batch_size=100, epochs=100, validation_split=0.2), ], ) training_data_file = "data/tiny_stories.md" training_data = await agent.load_data(training_data_file, augmentation_factor=10) # show_training_data(training_data) # print(type(training_data)) # print(training_data) for data in training_data: print(type(data)) # viz_domain(data.domain) # viz_TrackerWithCachedStates(data, view_domain=False) # exit() agent.train(training_data) # Attention: agent.persist stores the model and all meta data into a folder. # The folder itself is not zipped. model_path = os.path.join(model_directory, model_name, "core") agent.persist(model_path) logger.info(f"Model trained. Stored in '{model_path}'.") return model_path
async def prepared_agent(tmpdir_factory) -> Agent: model_path = tmpdir_factory.mktemp("model").strpath agent = Agent("data/test_domains/default.yml", policies=[AugmentedMemoizationPolicy(max_history=3)]) training_data = await agent.load_data(DEFAULT_STORIES_FILE) agent.train(training_data) agent.persist(model_path) return agent
def train_dialogue(domain_file='./config/domain.yml', model_path='./models/dialogue', training_data_file='./data/stories.md'): policies = config.load("./config/config.yml") agent = Agent(domain_file, policies=policies) loop = asyncio.get_event_loop() data = loop.run_until_complete(agent.load_data(training_data_file)) agent.train(data) agent.persist(model_path) return agent
def train_dialogue_transformer(domain_file="mobile_domain.yml", model_path="models/dialogue_transformer", training_data_file="data/mobile_edit_story.md"): # 通过加载yml配置文件方式配置policy policies = config.load('./policy/attention_policy.yml') agent = Agent(domain_file, policies=policies) training_data = agent.load_data(training_data_file) agent.train(training_data, validation_split=0.2) agent.persist(model_path) return agent
async def _trained_default_agent(tmpdir_factory: TempdirFactory) -> Tuple[Agent, str]: model_path = tmpdir_factory.mktemp("model").strpath agent = Agent( "data/test_domains/default_with_slots.yml", policies=[AugmentedMemoizationPolicy(max_history=3)], ) training_data = await agent.load_data(DEFAULT_STORIES_FILE) agent.train(training_data) agent.persist(model_path) return agent
def train_dialog() -> None: training_data_file = './data/stories.md' model_path = './models' policies = policy_config.load("Config.yml") # featurizer = MaxHistoryTrackerFeaturizer(BinarySingleStateFeaturizer(), max_history=5) agent = Agent('domain.yml', policies=policies) data = asyncio.run( agent.load_data(training_data_file, augmentation_factor=50)) # data = agent.load_data(training_data_file) agent.train(data) agent.persist(model_path)
async def _trained_default_agent() -> Tuple[Agent, str]: model_path = "{}/models/".format(prj_dir) agent = Agent( "{}/data/default_with_slots.yml".format(prj_dir), policies=[AugmentedMemoizationPolicy(max_history=3)], ) training_data = await agent.load_data( "{}/data/stories_defaultdomain.md".format(prj_dir)) agent.train(training_data) agent.persist(model_path) return agent
async def train_core(domain_file, training_data_file, model_directory): agent = Agent(domain_file, policies=[MemoizationPolicy(max_history=3), MappingPolicy(), KerasPolicy(epochs=500)]) training_data = await agent.load_data(training_data_file, augmentation_factor=10) agent.train(training_data) # Attention: agent.persist stores the model and all meta data into a folder. # The folder itself is not zipped. model_path = os.path.join(model_directory, "core") agent.persist(model_path) logger.info(f"Model trained. Stored in '{model_path}'.") return model_path
async def train( domain_file: Union[Domain, Text], training_resource: Union[Text, "TrainingDataImporter"], output_path: Text, interpreter: Optional["NaturalLanguageInterpreter"] = None, endpoints: "AvailableEndpoints" = None, policy_config: Optional[Union[Text, Dict]] = None, exclusion_percentage: Optional[int] = None, additional_arguments: Optional[Dict] = None, model_to_finetune: Optional["Agent"] = None, ) -> "Agent": from rasa.core import config, utils from rasa.core.utils import AvailableEndpoints from rasa.core.agent import Agent if not endpoints: endpoints = AvailableEndpoints() if not additional_arguments: additional_arguments = {} policies = config.load(policy_config) agent = Agent( domain_file, generator=endpoints.nlg, action_endpoint=endpoints.action, interpreter=interpreter, policies=policies, ) data_load_args, additional_arguments = utils.extract_args( additional_arguments, { "use_story_concatenation", "unique_last_num_states", "augmentation_factor", "remove_duplicates", "debug_plots", }, ) training_data = await agent.load_data( training_resource, exclusion_percentage=exclusion_percentage, **data_load_args) if model_to_finetune: agent.policy_ensemble = model_to_finetune.policy_ensemble agent.train(training_data, **additional_arguments) agent.persist(output_path) return agent
async def _trained_default_agent(tmpdir_factory: TempdirFactory, stories_path: Text) -> Agent: model_path = tmpdir_factory.mktemp("model").strpath agent = Agent( "data/test_domains/default_with_slots.yml", policies=[AugmentedMemoizationPolicy(max_history=3), RulePolicy()], ) training_data = await agent.load_data(stories_path) agent.train(training_data) agent.persist(model_path) return agent
def train_dialogue(domain_file='domain.yml', model_path='./models/dialogue', training_data_file='./data/stories.md'): agent = Agent(domain_file, policies=[MemoizationPolicy(), KerasPolicy()]) agent.train(training_data_file, max_history=3, epochs=300, batch_size=50, validation_split=0.2, augmentation_factor=50) agent.persist(model_path) return agent
async def train( domain_file: Union[Domain, Text], stories_file: Text, output_path: Text, interpreter: Optional["NaturalLanguageInterpreter"] = None, endpoints: "AvailableEndpoints" = None, dump_stories: bool = False, policy_config: Text = None, exclusion_percentage: int = None, kwargs: Optional[Dict] = None, ): from rasa.core.agent import Agent from rasa.core import config, utils from rasa.core.utils import AvailableEndpoints if not endpoints: endpoints = AvailableEndpoints() if not kwargs: kwargs = {} policies = config.load(policy_config) agent = Agent( domain_file, generator=endpoints.nlg, action_endpoint=endpoints.action, interpreter=interpreter, policies=policies, ) data_load_args, kwargs = utils.extract_args( kwargs, { "use_story_concatenation", "unique_last_num_states", "augmentation_factor", "remove_duplicates", "debug_plots", }, ) training_data = await agent.load_data( stories_file, exclusion_percentage=exclusion_percentage, **data_load_args) agent.train(training_data, **kwargs) agent.persist(output_path, dump_stories) return agent
async def train( domain_file: Union[Domain, Text], training_resource: Union[Text, "TrainingDataImporter"], output_path: Text, interpreters: Optional[Dict[Text, "NaturalLanguageInterpreter"]] = None, endpoints: "AvailableEndpoints" = None, dump_stories: bool = False, policy_config: Optional[Union[Text, Dict]] = None, exclusion_percentage: int = None, additional_arguments: Optional[Dict] = None, ): from rasa.core.agent import Agent from rasa.core import config, utils from rasa.core.utils import AvailableEndpoints if not endpoints: endpoints = AvailableEndpoints() if not additional_arguments: additional_arguments = {} policies = config.load(policy_config) agent = Agent( domain_file, generator=endpoints.nlg, action_endpoint=endpoints.action, interpreters=interpreters or {}, # fix to avoid model not ready error policies=policies, ) data_load_args, additional_arguments = utils.extract_args( additional_arguments, { "use_story_concatenation", "unique_last_num_states", "augmentation_factor", "remove_duplicates", "debug_plots", }, ) training_data = await agent.load_data( training_resource, exclusion_percentage=exclusion_percentage, **data_load_args) agent.train(training_data, **additional_arguments) agent.persist(output_path, dump_stories) return agent
def train_dialogue(domain_file='./data/criminal_domain.yml', model_path='./models/dialogue', training_data_file='./data/stories.md'): agent = Agent(domain_file, policies=[ MemoizationPolicy(), KerasPolicy(max_history=3, epochs=200, batch_size=50) ]) data = agent.load_data(training_data_file) agent.train(data) agent.persist(model_path) return agent
async def train_dialogue(domain_file="domain.yml", model_path="models/dialogue", training_data_file="data/stories.md"): agent = Agent(domain_file, policies=[MemoizationPolicy(max_history=3), MappingPolicy(), RestaurantPolicy(batch_size=100, epochs=400, validation_split=0.2)]) training_data = await agent.load_data(training_data_file) agent.train( training_data ) agent.persist(model_path) return agent
def train_dialogue(domain_file="domain.yml", model_path="./models/dialogue", training_data_file="./data/stories.md"): #agent = Agent(domain_file, policies=[MemoizationPolicy(max_history=3), KerasPolicy()]) #training_data = agent.load_data(training_data_file) #agent.train(training_data,epochs=400,batch_size=100,validation_split=0.2) #agent.persist(model_path) policies = policy_config.load("./policies.yml") agent = Agent(domain_file, policies=policies) loop = asyncio.get_event_loop() dataa = loop.run_until_complete(agent.load_data(training_data_file)) agent.train(dataa) agent.persist(model_path) return agent
def train_dialogue(domain_file='domain.yml', model_path='./models/dialogue', training_data_file='./data/stories.md'): fallback = FallbackPolicy(fallback_action_name="utter_unclear", core_threshold=0.3, nlu_threshold=0.75) agent = Agent(domain_file, policies=[ MemoizationPolicy(max_history=7), KerasPolicy(current_epoch=100, max_history=7), fallback ]) data = asyncio.run(agent.load_data(training_data_file)) agent.train(data) # agent.train( # data, # epochs=500, # batch_size=50, # validation_split=0.2) agent.persist(model_path) return agent
async def train_core(domain_file: Text = "domain.yml", model_directory: Text = "models", model_name: Text = "core", training_data_file: Text = "data/stories.md",): logging.basicConfig(filename=logfile, level=logging.DEBUG) agent = Agent( domain_file, policies=[ MemoizationPolicy(max_history=3), KerasPolicy(batch_size=100, epochs=400, validation_split=0.2) ] ) training_data = await agent.load_data(training_data_file) agent.train(training_data) # Attention: agent.persist stores the model and all meta data into a folder. # The folder itself is not zipped. model_path = os.path.join(model_directory, model_name) agent.persist(model_path)
async def test_agent_train(tmpdir, default_domain): training_data_file = "examples/moodbot/data/stories.md" agent = Agent("examples/moodbot/domain.yml", policies=[AugmentedMemoizationPolicy()]) training_data = await agent.load_data(training_data_file) agent.train(training_data) agent.persist(tmpdir.strpath) loaded = Agent.load(tmpdir.strpath) # test domain assert loaded.domain.action_names == agent.domain.action_names assert loaded.domain.intents == agent.domain.intents assert loaded.domain.entities == agent.domain.entities assert loaded.domain.templates == agent.domain.templates assert [s.name for s in loaded.domain.slots ] == [s.name for s in agent.domain.slots] # test policies assert isinstance(loaded.policy_ensemble, type(agent.policy_ensemble)) assert [type(p) for p in loaded.policy_ensemble.policies ] == [type(p) for p in agent.policy_ensemble.policies]
def train(): domain_file = "data/sct11/rasa_models/sct11_domain.yml" caller_model_path = "data/sct11/rasa_models/caller" callee_model_path = "data/sct11/rasa_models/callee" caller_training = "data/sct11/rasa_models/sct11_story_caller.md" callee_training = "data/sct11/rasa_models/sct11_story_callee.md" caller_agent = Agent(domain_file, policies=[RandomChoicePolicy()]) callee_agent = Agent(domain_file, policies=[RandomChoicePolicy()]) caller_train_data = caller_agent.load_data(caller_training) caller_agent.train(caller_train_data, epochs=100, batch_size=100, validation_split=0.2) caller_agent.persist(caller_model_path) callee_train_data = callee_agent.load_data(callee_training) callee_agent.train(callee_train_data, epochs=100, batch_size=100, validation_split=0.2) callee_agent.persist(callee_model_path)
async def train_core( domain_file: Text = "domain.yml", model_directory: Text = "models", model_name: Text = "current", training_data_file: Text = "data/stories.md", ): agent = Agent( domain_file, policies=[CustomPolicy()], ) training_data = await agent.load_data(training_data_file, augmentation_factor=10) agent.train(training_data) # Attention: agent.persist stores the model and all meta data into a folder. # The folder itself is not zipped. model_path = os.path.join(model_directory, model_name, "core") agent.persist(model_path) logger.info("Model trained. Stored in '{}'.".format(model_path)) return model_path
async def train_core( domain_file: Text = "domain.yml", model_path: Text = "models/core", training_data_file: Text = "data/stories.md", ): agent = Agent( domain_file, policies=[ MemoizationPolicy(max_history=3), MappingPolicy(), RestaurantPolicy(batch_size=100, epochs=400, validation_split=0.2), ], ) training_data = await agent.load_data(training_data_file) agent.train(training_data) # Attention: agent.persist stores the model and all meta data into a folder. # The folder itself is not zipped. agent.persist(model_path) logger.info("Model trained. Stored in '{}'.".format(model_path)) return model_path
def default_agent_path(_default_agent: Agent, tmpdir_factory: TempdirFactory): path = tmpdir_factory.mktemp("agent").strpath _default_agent.persist(path) return path
from __future__ import absolute_import from __future__ import division from __future__ import unicode_literals import logging from rasa.core.agent import Agent from rasa.core.policies.keras_policy import KerasPolicy from rasa.core.policies.memoization import MemoizationPolicy if __name__ == 'main': logging.basicConfig(level='info') training_data_file = './data/stories.md' model_path = './models/' agent = Agent('domain.yml', policies=[MemoizationPolicy(), KerasPolicy()]) agent.train(training_data_file, augmentation_factor=50, max_history=5, epochs=100, batch_size=10, validation_split=0.2) agent.persist(model_path)
# In[25]: loop = asyncio.get_event_loop() training_data = loop.run_until_complete(agent.load_data(training_data_file)) # In[26]: agent.train(training_data) # In[27]: agent_model_path = os.path.join(model_dir, policy_fixed_model_name) # In[28]: agent.persist(agent_model_path) # In[29]: #get_ipython().system('ls ../../rasa-ted-demo/*') # In[36]: #get_ipython().system('ls ../../rasa-ted-demo/models/dialogue/core') # --------------------- # In[37]: from rasa.core.agent import Agent from rasa.core.interpreter import NaturalLanguageInterpreter