How should I exactly override the Keras Policy (I want to change the default LSTM model)

How should I exactly override the Keras Policy (I want to change the default LSTM model) In the Docs it is given that to change the model we need to override the model_architecture method but exactly which files we have to create and where to specify the new model seems unknown to me. One method that I found the shortest is to edit the keras_policy.py in the source code but would be unfair to use in the current scenario. Do let me know the best approach.

Like you would do with a normal python class. Just inherit from the keras_policy class, override the model_architecture method and add it as a custom policy to your config

How should I import KerasPolicy I tried creating a newKerasPolicy.py with newKerasPolicy class at project root with model_architecture and train but it says

rasa.core.policies.ensemble.InvalidPolicyConfig: Module for policy 'newKerasPolicy' could not be loaded. Please make sure the name is a valid policy.

Configure your python path properly so it knows where to look for that particular module

I had already configured PYTHONPATH and verified using

>>> import sys
>>> for line in sys.path: print(line)

I then created a file named newKerasPolicy.py at project root with the following contents:

class newKerasPolicy(KerasPolicy):
     def model_architecture(...)
           ...
     def train(....)
           ...

My config.yml file:

...
policies:
- name: newKerasPolicy
  random_seed: 1
  validation_split: 0.1
...

Still I have the same error as above, I think I have made a mistake in class inheritance but cannot find it out. Infact I am able to load the module using import newKerasPolicy from any location, still the same error persists.

Try putting newKerasPolicy.newKerasPolicy in the name instead.

1 Like

Ya Thanks a lot, this finally worked.
I then also had to include from rasa.core.policies.keras_policy import KerasPolicy

and all the imported libraries at: ~/miniconda3/envs/rasax_gpu/lib/python3.7/site-packages/rasa/core/policies/keras_policy.py and from typing import Any, List, Dict, Text, Optional, Tuple outside and inside the class respectively.

It then worked totally fine.
Had a nice brush up of concepts, and Yeah having someone looking at the problem does really help out.

Glad to hear it works ^^ Just some advice, try to follow the convention for naming your files and classes so it would be something like new_keras_policy.NewKerasPolicy.

1 Like

Sure, Thanks Again !!