rasa/core/policies/embedding_policy.py
def _tf_sim(self,
                embed_dialogue: tf.Tensor,
                embed_action: tf.Tensor,
                mask: Optional[tf.Tensor]
                ) -> Tuple[tf.Tensor, tf.Tensor]:
        """Define similarity.

        This method has two roles:
        - calculate similarity between
            two embedding vectors of the same size
            and output binary mask and similarity;
        - calculate similarity with several embedded actions for the loss
            and output similarities between user input and bot actions
            and similarities between bot actions.

        They are kept in the same helper method,
        because it is necessary for them to be mathematically identical.
        """

        if self.similarity_type == 'cosine':
            # normalize embedding vectors for cosine similarity
            embed_dialogue = tf.nn.l2_normalize(embed_dialogue, -1)
            embed_action = tf.nn.l2_normalize(embed_action, -1)

        if self.similarity_type in {'cosine', 'inner'}:

            if len(embed_dialogue.shape) == len(embed_action.shape):
                # calculate similarity between
                # two embedding vectors of the same size
                sim = tf.reduce_sum(embed_dialogue * embed_action, -1,
                                    keepdims=True)
                bin_sim = tf.where(sim > (self.mu_pos - self.mu_neg) / 2.0,
                                   tf.ones_like(sim),
                                   tf.zeros_like(sim))

                # output binary mask and similarity
                return bin_sim, sim

            else:
                # calculate similarity with several
                # embedded actions for the loss
                sim = tf.reduce_sum(tf.expand_dims(embed_dialogue, -2) *
                                    embed_action, -1)
                sim *= tf.expand_dims(mask, 2)

                sim_act = tf.reduce_sum(embed_action[:, :, :1, :] *
                                        embed_action[:, :, 1:, :], -1)
                sim_act *= tf.expand_dims(mask, 2)

                # output similarities between user input and bot actions
                # and similarities between bot actions
                return sim, sim_act

        else:
            raise ValueError("Wrong similarity type {}, "
                             "should be 'cosine' or 'inner'"
                             "".format(self.similarity_type))
Define similarity.

        This method has two roles:
        - calculate similarity between
            two embedding vectors of the same size
            and output binary mask and similarity;
        - calculate similarity with several embedded actions for the loss
            and output similarities between user input and bot actions
            and similarities between bot actions.

        They are kept in the same helper method,
        because it is necessary for them to be mathematically identical.
        
Define similarity.
This method has two roles:
calculate similarity between
two embedding vectors of the same size
and output binary mask and similarity;
calculate similarity with several embedded actions for the loss
and output similarities between user input and bot actions
and similarities between bot actions.

They are kept in the same helper method,
because it is necessary for them to be mathematically identical.
Define similarity . This method has two roles : calculate similarity between two embedding vectors of the same size and output binary mask and similarity ; calculate similarity with several embedded actions for the loss and output similarities between user input and bot actions and similarities between bot actions . They are kept in the same helper method because it is necessary for them to be mathematically identical .
def _tf_sim(self,
                embed_dialogue: tf.Tensor,
                embed_action: tf.Tensor,
                mask: Optional[tf.Tensor]
                ) -> Tuple[tf.Tensor, tf.Tensor]:
        if self.similarity_type == 'cosine':
            embed_dialogue = tf.nn.l2_normalize(embed_dialogue, -1)
            embed_action = tf.nn.l2_normalize(embed_action, -1)
        if self.similarity_type in {'cosine', 'inner'}:
            if len(embed_dialogue.shape) == len(embed_action.shape):
                sim = tf.reduce_sum(embed_dialogue * embed_action, -1,
                                    keepdims=True)
                bin_sim = tf.where(sim > (self.mu_pos - self.mu_neg) / 2.0,
                                   tf.ones_like(sim),
                                   tf.zeros_like(sim))
                return bin_sim, sim
            else:
                sim = tf.reduce_sum(tf.expand_dims(embed_dialogue, -2) *
                                    embed_action, -1)
                sim *= tf.expand_dims(mask, 2)
                sim_act = tf.reduce_sum(embed_action[:, :, :1, :] *
                                        embed_action[:, :, 1:, :], -1)
                sim_act *= tf.expand_dims(mask, 2)
                return sim, sim_act
        else:
            raise ValueError("Wrong similarity type {}, "
                             "should be 'cosine' or 'inner'"
                             "".format(self.similarity_type))
def _tf_sim ( self , embed_dialogue : tf . Tensor , embed_action : tf . Tensor , mask : Optional [ tf . Tensor ] ) -> Tuple [ tf . Tensor , tf . Tensor ] : if self . similarity_type == 'cosine' : embed_dialogue = tf . nn . l2_normalize ( embed_dialogue , - 1 ) embed_action = tf . nn . l2_normalize ( embed_action , - 1 ) if self . similarity_type in { 'cosine' , 'inner' } : if len ( embed_dialogue . shape ) == len ( embed_action . shape ) : sim = tf . reduce_sum ( embed_dialogue * embed_action , - 1 , keepdims = True ) bin_sim = tf . where ( sim > ( self . mu_pos - self . mu_neg ) / 2.0 , tf . ones_like ( sim ) , tf . zeros_like ( sim ) ) return bin_sim , sim else : sim = tf . reduce_sum ( tf . expand_dims ( embed_dialogue , - 2 ) * embed_action , - 1 ) sim *= tf . expand_dims ( mask , 2 ) sim_act = tf . reduce_sum ( embed_action [ : , : , : 1 , : ] * embed_action [ : , : , 1 : , : ] , - 1 ) sim_act *= tf . expand_dims ( mask , 2 ) return sim , sim_act else : raise ValueError ( "Wrong similarity type {}, " "should be 'cosine' or 'inner'" "" . format ( self . similarity_type ) )
Define similarity.
Define similarity .
"""Define similarity.

        This method has two roles:
        - calculate similarity between
            two embedding vectors of the same size
            and output binary mask and similarity;
        - calculate similarity with several embedded actions for the loss
            and output similarities between user input and bot actions
            and similarities between bot actions.

        They are kept in the same helper method,
        because it is necessary for them to be mathematically identical.
        """ # normalize embedding vectors for cosine similarity # calculate similarity between # two embedding vectors of the same size # output binary mask and similarity # calculate similarity with several # embedded actions for the loss # output similarities between user input and bot actions # and similarities between bot actions
self
embed_dialogue
tf.Tensor
embed_action
tf.Tensor
mask
Optional[tf.Tensor]