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

Commit

Permalink
moved polciy to own model to allow it getting persisted properly
Browse files Browse the repository at this point in the history
  • Loading branch information
tmbo committed Feb 13, 2018
1 parent 794b9b1 commit 90a235c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
4 changes: 2 additions & 2 deletions docs/tutorial_supervised.rst
Expand Up @@ -151,9 +151,9 @@ For this bot, we came up with our own policy.
This policy extends the Keras policy modifying the ML architecture of the
underlying neural network.
Check out the
``RestaurantPolicy`` class in ``bot.py`` for the gory details:
``RestaurantPolicy`` class in ``policy.py`` for the glory details:

.. literalinclude:: ../examples/restaurantbot/bot.py
.. literalinclude:: ../examples/restaurantbot/policy.py
:linenos:
:pyobject: RestaurantPolicy

Expand Down
30 changes: 3 additions & 27 deletions examples/restaurantbot/bot.py
Expand Up @@ -7,19 +7,19 @@
import logging
import warnings

from policy import RestaurantPolicy
from rasa_core import utils
from rasa_core.actions import Action
from rasa_core.agent import Agent
from rasa_core.channels.console import ConsoleInputChannel
from rasa_core.events import SlotSet
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.policies.keras_policy import KerasPolicy
from rasa_core.policies.memoization import MemoizationPolicy
from rasa_core.events import SlotSet

logger = logging.getLogger(__name__)


class RestaurantAPI:
class RestaurantAPI(object):
def search(self, info):
return "papi's pizza place"

Expand Down Expand Up @@ -48,30 +48,6 @@ def run(self, dispatcher, tracker, domain):
return []


class RestaurantPolicy(KerasPolicy):
def model_architecture(self, num_features, num_actions, max_history_len):
"""Build a Keras model and return a compiled model."""
from keras.layers import LSTM, Activation, Masking, Dense
from keras.models import Sequential

n_hidden = 32 # size of hidden layer in LSTM
# Build Model
batch_shape = (None, max_history_len, num_features)

model = Sequential()
model.add(Masking(-1, batch_input_shape=batch_shape))
model.add(LSTM(n_hidden, batch_input_shape=batch_shape))
model.add(Dense(input_dim=n_hidden, output_dim=num_actions))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])

logger.debug(model.summary())
return model


def train_dialogue(domain_file="restaurant_domain.yml",
model_path="models/dialogue",
training_data_file="data/babi_stories.md"):
Expand Down
34 changes: 34 additions & 0 deletions examples/restaurantbot/policy.py
@@ -0,0 +1,34 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging

from rasa_core.policies.keras_policy import KerasPolicy

logger = logging.getLogger(__name__)


class RestaurantPolicy(KerasPolicy):
def model_architecture(self, num_features, num_actions, max_history_len):
"""Build a Keras model and return a compiled model."""
from keras.layers import LSTM, Activation, Masking, Dense
from keras.models import Sequential

n_hidden = 32 # size of hidden layer in LSTM
# Build Model
batch_shape = (None, max_history_len, num_features)

model = Sequential()
model.add(Masking(-1, batch_input_shape=batch_shape))
model.add(LSTM(n_hidden, batch_input_shape=batch_shape))
model.add(Dense(input_dim=n_hidden, output_dim=num_actions))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])

logger.debug(model.summary())
return model

0 comments on commit 90a235c

Please sign in to comment.