rasa/core/policies/embedding_policy.py
def train(self,
              training_trackers: List[DialogueStateTracker],
              domain: Domain,
              **kwargs: Any
              ) -> None:
        """Train the policy on given training trackers."""

        logger.debug('Started training embedding policy.')

        # set numpy random seed
        np.random.seed(self.random_seed)

        # dealing with training data
        training_data = self.featurize_for_training(training_trackers,
                                                    domain,
                                                    **kwargs)
        # assume that characteristic time is the mean length of the dialogues
        self.characteristic_time = np.mean(training_data.true_length)
        if self.attn_shift_range is None:
            self.attn_shift_range = int(self.characteristic_time / 2)

        # encode all actions with policies' featurizer
        self.encoded_all_actions = \
            self.featurizer.state_featurizer.create_encoded_all_actions(
                domain)

        # check if number of negatives is less than number of actions
        logger.debug("Check if num_neg {} is smaller "
                     "than number of actions {}, "
                     "else set num_neg to the number of actions - 1"
                     "".format(self.num_neg, domain.num_actions))
        self.num_neg = min(self.num_neg, domain.num_actions - 1)

        # extract actual training data to feed to tf session
        session_data = self._create_tf_session_data(domain,
                                                    training_data.X,
                                                    training_data.y)

        self.graph = tf.Graph()

        with self.graph.as_default():
            # set random seed in tf
            tf.set_random_seed(self.random_seed)

            dialogue_len = None  # use dynamic time for rnn
            # create placeholders
            self.a_in = tf.placeholder(
                dtype=tf.float32,
                shape=(None, dialogue_len,
                       session_data.X.shape[-1]),
                name='a'
            )
            self.b_in = tf.placeholder(
                dtype=tf.float32,
                shape=(None, dialogue_len,
                       None, session_data.Y.shape[-1]),
                name='b'
            )
            self.c_in = tf.placeholder(
                dtype=tf.float32,
                shape=(None, dialogue_len,
                       session_data.slots.shape[-1]),
                name='slt'
            )
            self.b_prev_in = tf.placeholder(
                dtype=tf.float32,
                shape=(None, dialogue_len,
                       session_data.Y.shape[-1]),
                name='b_prev'
            )
            self._dialogue_len = tf.placeholder(
                dtype=tf.int32,
                shape=(),
                name='dialogue_len'
            )
            self._x_for_no_intent_in = tf.placeholder(
                dtype=tf.float32,
                shape=(1, session_data.X.shape[-1]),
                name='x_for_no_intent'
            )
            self._y_for_no_action_in = tf.placeholder(
                dtype=tf.float32,
                shape=(1, session_data.Y.shape[-1]),
                name='y_for_no_action'
            )
            self._y_for_action_listen_in = tf.placeholder(
                dtype=tf.float32,
                shape=(1, session_data.Y.shape[-1]),
                name='y_for_action_listen'
            )
            self._is_training = tf.placeholder_with_default(False, shape=())

            self._loss_scales = tf.placeholder(dtype=tf.float32,
                                               shape=(None, dialogue_len))

            # create embedding vectors
            self.user_embed = self._create_tf_user_embed(self.a_in)
            self.bot_embed = self._create_tf_bot_embed(self.b_in)
            self.slot_embed = self._create_embed(self.c_in,
                                                 layer_name_suffix='slt')

            embed_prev_action = self._create_tf_bot_embed(self.b_prev_in)
            embed_for_no_intent = self._create_tf_no_intent_embed(
                self._x_for_no_intent_in)
            embed_for_no_action = self._create_tf_no_action_embed(
                self._y_for_no_action_in)
            embed_for_action_listen = self._create_tf_no_action_embed(
                self._y_for_action_listen_in)

            # mask different length sequences
            # if there is at least one `-1` it should be masked
            mask = tf.sign(tf.reduce_max(self.a_in, -1) + 1)

            # get rnn output
            cell_output, final_state = self._create_tf_dial_embed(
                self.user_embed, self.slot_embed, embed_prev_action, mask,
                embed_for_no_intent, embed_for_no_action,
                embed_for_action_listen
            )
            # process rnn output
            if self.is_using_attention():
                self.alignment_history = \
                    self._alignments_history_from(final_state)

                self.all_time_masks = self._all_time_masks_from(final_state)

            sims_rnn_to_max = self._sims_rnn_to_max_from(cell_output)
            self.dial_embed = self._embed_dialogue_from(cell_output)

            # calculate similarities
            self.sim_op, sim_act = self._tf_sim(self.dial_embed,
                                                self.bot_embed, mask)
            # construct loss
            loss = self._tf_loss(self.sim_op, sim_act, sims_rnn_to_max, mask)

            # define which optimizer to use
            self._train_op = tf.train.AdamOptimizer(
                learning_rate=0.001, epsilon=1e-16).minimize(loss)
            # train tensorflow graph
            self.session = tf.Session(config=self._tf_config)

            self._train_tf(session_data, loss, mask)
Train the policy on given training trackers.
Train the policy on given training trackers.
Train the policy on given training trackers .
def train(self,
              training_trackers: List[DialogueStateTracker],
              domain: Domain,
              **kwargs: Any
              ) -> None:
        logger.debug('Started training embedding policy.')
        np.random.seed(self.random_seed)
        training_data = self.featurize_for_training(training_trackers,
                                                    domain,
                                                    **kwargs)
        self.characteristic_time = np.mean(training_data.true_length)
        if self.attn_shift_range is None:
            self.attn_shift_range = int(self.characteristic_time / 2)
        self.encoded_all_actions = \
            self.featurizer.state_featurizer.create_encoded_all_actions(
                domain)
        logger.debug("Check if num_neg {} is smaller "
                     "than number of actions {}, "
                     "else set num_neg to the number of actions - 1"
                     "".format(self.num_neg, domain.num_actions))
        self.num_neg = min(self.num_neg, domain.num_actions - 1)
        session_data = self._create_tf_session_data(domain,
                                                    training_data.X,
                                                    training_data.y)
        self.graph = tf.Graph()
        with self.graph.as_default():
            tf.set_random_seed(self.random_seed)
            dialogue_len = None  
            self.a_in = tf.placeholder(
                dtype=tf.float32,
                shape=(None, dialogue_len,
                       session_data.X.shape[-1]),
                name='a'
            )
            self.b_in = tf.placeholder(
                dtype=tf.float32,
                shape=(None, dialogue_len,
                       None, session_data.Y.shape[-1]),
                name='b'
            )
            self.c_in = tf.placeholder(
                dtype=tf.float32,
                shape=(None, dialogue_len,
                       session_data.slots.shape[-1]),
                name='slt'
            )
            self.b_prev_in = tf.placeholder(
                dtype=tf.float32,
                shape=(None, dialogue_len,
                       session_data.Y.shape[-1]),
                name='b_prev'
            )
            self._dialogue_len = tf.placeholder(
                dtype=tf.int32,
                shape=(),
                name='dialogue_len'
            )
            self._x_for_no_intent_in = tf.placeholder(
                dtype=tf.float32,
                shape=(1, session_data.X.shape[-1]),
                name='x_for_no_intent'
            )
            self._y_for_no_action_in = tf.placeholder(
                dtype=tf.float32,
                shape=(1, session_data.Y.shape[-1]),
                name='y_for_no_action'
            )
            self._y_for_action_listen_in = tf.placeholder(
                dtype=tf.float32,
                shape=(1, session_data.Y.shape[-1]),
                name='y_for_action_listen'
            )
            self._is_training = tf.placeholder_with_default(False, shape=())
            self._loss_scales = tf.placeholder(dtype=tf.float32,
                                               shape=(None, dialogue_len))
            self.user_embed = self._create_tf_user_embed(self.a_in)
            self.bot_embed = self._create_tf_bot_embed(self.b_in)
            self.slot_embed = self._create_embed(self.c_in,
                                                 layer_name_suffix='slt')
            embed_prev_action = self._create_tf_bot_embed(self.b_prev_in)
            embed_for_no_intent = self._create_tf_no_intent_embed(
                self._x_for_no_intent_in)
            embed_for_no_action = self._create_tf_no_action_embed(
                self._y_for_no_action_in)
            embed_for_action_listen = self._create_tf_no_action_embed(
                self._y_for_action_listen_in)
            mask = tf.sign(tf.reduce_max(self.a_in, -1) + 1)
            cell_output, final_state = self._create_tf_dial_embed(
                self.user_embed, self.slot_embed, embed_prev_action, mask,
                embed_for_no_intent, embed_for_no_action,
                embed_for_action_listen
            )
            if self.is_using_attention():
                self.alignment_history = \
                    self._alignments_history_from(final_state)
                self.all_time_masks = self._all_time_masks_from(final_state)
            sims_rnn_to_max = self._sims_rnn_to_max_from(cell_output)
            self.dial_embed = self._embed_dialogue_from(cell_output)
            self.sim_op, sim_act = self._tf_sim(self.dial_embed,
                                                self.bot_embed, mask)
            loss = self._tf_loss(self.sim_op, sim_act, sims_rnn_to_max, mask)
            self._train_op = tf.train.AdamOptimizer(
                learning_rate=0.001, epsilon=1e-16).minimize(loss)
            self.session = tf.Session(config=self._tf_config)
            self._train_tf(session_data, loss, mask)
def train ( self , training_trackers : List [ DialogueStateTracker ] , domain : Domain , ** kwargs : Any ) -> None : logger . debug ( 'Started training embedding policy.' ) np . random . seed ( self . random_seed ) training_data = self . featurize_for_training ( training_trackers , domain , ** kwargs ) self . characteristic_time = np . mean ( training_data . true_length ) if self . attn_shift_range is None : self . attn_shift_range = int ( self . characteristic_time / 2 ) self . encoded_all_actions = self . featurizer . state_featurizer . create_encoded_all_actions ( domain ) logger . debug ( "Check if num_neg {} is smaller " "than number of actions {}, " "else set num_neg to the number of actions - 1" "" . format ( self . num_neg , domain . num_actions ) ) self . num_neg = min ( self . num_neg , domain . num_actions - 1 ) session_data = self . _create_tf_session_data ( domain , training_data . X , training_data . y ) self . graph = tf . Graph ( ) with self . graph . as_default ( ) : tf . set_random_seed ( self . random_seed ) dialogue_len = None self . a_in = tf . placeholder ( dtype = tf . float32 , shape = ( None , dialogue_len , session_data . X . shape [ - 1 ] ) , name = 'a' ) self . b_in = tf . placeholder ( dtype = tf . float32 , shape = ( None , dialogue_len , None , session_data . Y . shape [ - 1 ] ) , name = 'b' ) self . c_in = tf . placeholder ( dtype = tf . float32 , shape = ( None , dialogue_len , session_data . slots . shape [ - 1 ] ) , name = 'slt' ) self . b_prev_in = tf . placeholder ( dtype = tf . float32 , shape = ( None , dialogue_len , session_data . Y . shape [ - 1 ] ) , name = 'b_prev' ) self . _dialogue_len = tf . placeholder ( dtype = tf . int32 , shape = ( ) , name = 'dialogue_len' ) self . _x_for_no_intent_in = tf . placeholder ( dtype = tf . float32 , shape = ( 1 , session_data . X . shape [ - 1 ] ) , name = 'x_for_no_intent' ) self . _y_for_no_action_in = tf . placeholder ( dtype = tf . float32 , shape = ( 1 , session_data . Y . shape [ - 1 ] ) , name = 'y_for_no_action' ) self . _y_for_action_listen_in = tf . placeholder ( dtype = tf . float32 , shape = ( 1 , session_data . Y . shape [ - 1 ] ) , name = 'y_for_action_listen' ) self . _is_training = tf . placeholder_with_default ( False , shape = ( ) ) self . _loss_scales = tf . placeholder ( dtype = tf . float32 , shape = ( None , dialogue_len ) ) self . user_embed = self . _create_tf_user_embed ( self . a_in ) self . bot_embed = self . _create_tf_bot_embed ( self . b_in ) self . slot_embed = self . _create_embed ( self . c_in , layer_name_suffix = 'slt' ) embed_prev_action = self . _create_tf_bot_embed ( self . b_prev_in ) embed_for_no_intent = self . _create_tf_no_intent_embed ( self . _x_for_no_intent_in ) embed_for_no_action = self . _create_tf_no_action_embed ( self . _y_for_no_action_in ) embed_for_action_listen = self . _create_tf_no_action_embed ( self . _y_for_action_listen_in ) mask = tf . sign ( tf . reduce_max ( self . a_in , - 1 ) + 1 ) cell_output , final_state = self . _create_tf_dial_embed ( self . user_embed , self . slot_embed , embed_prev_action , mask , embed_for_no_intent , embed_for_no_action , embed_for_action_listen ) if self . is_using_attention ( ) : self . alignment_history = self . _alignments_history_from ( final_state ) self . all_time_masks = self . _all_time_masks_from ( final_state ) sims_rnn_to_max = self . _sims_rnn_to_max_from ( cell_output ) self . dial_embed = self . _embed_dialogue_from ( cell_output ) self . sim_op , sim_act = self . _tf_sim ( self . dial_embed , self . bot_embed , mask ) loss = self . _tf_loss ( self . sim_op , sim_act , sims_rnn_to_max , mask ) self . _train_op = tf . train . AdamOptimizer ( learning_rate = 0.001 , epsilon = 1e-16 ) . minimize ( loss ) self . session = tf . Session ( config = self . _tf_config ) self . _train_tf ( session_data , loss , mask )
Train the policy on given training trackers.
Train the policy on given training trackers .
"""Train the policy on given training trackers.""" # set numpy random seed # dealing with training data # assume that characteristic time is the mean length of the dialogues # encode all actions with policies' featurizer # check if number of negatives is less than number of actions # extract actual training data to feed to tf session # set random seed in tf # use dynamic time for rnn # create placeholders # create embedding vectors # mask different length sequences # if there is at least one `-1` it should be masked # get rnn output # process rnn output # calculate similarities # construct loss # define which optimizer to use # train tensorflow graph
self
training_trackers
List[DialogueStateTracker]
domain
Domain
kwargs
Any