Jann/utils.py
def inference(self, input_text, num_neighbors=10, use_sentence_piece=True):
        """Inference from nearest neighbor model."""

        # Handle the short input
        if len(input_text) < 1:
            return 'Say something!'

        tf.logging.info('Input text: {}'.format(input_text))

        # Build a list of the user input
        user_input = [input_text]

        if use_sentence_piece:
            # process unencoded lines to values and IDs in sparse format
            values, indices, dense_shape = process_to_IDs_in_sparse_format(
                sp=self.sp, sentences=user_input)

            # run the session
            # Get embedding of the input text
            embeddings = self.sess.run(
                self.embeddings,
                feed_dict={
                    self.input_placeholder.values: values,
                    self.input_placeholder.indices: indices,
                    self.input_placeholder.dense_shape: dense_shape
                }
            )
        else:
            embeddings = self.sess.run(
                self.embeddings,
                feed_dict={
                    self.input_placeholder: user_input
                }
            )

        tf.logging.info(
            'Successfully generated {} embeddings of length {}.'.format(
                len(embeddings), len(embeddings[0])))

        # Extract the query vector of interest.
        query_vector = embeddings[0]

        # Get nearest neighbors
        nns, distances = self.annoy_index.get_nns_by_vector(
            query_vector, num_neighbors, search_k=-1, include_distances=True)

        # Log the ids
        tf.logging.info('Nearest neighbor IDS: {}'.format(nns))

        return nns, distances
Inference from nearest neighbor model.
Inference from nearest neighbor model.
Inference from nearest neighbor model .
def inference(self, input_text, num_neighbors=10, use_sentence_piece=True):
        if len(input_text) < 1:
            return 'Say something!'
        tf.logging.info('Input text: {}'.format(input_text))
        user_input = [input_text]
        if use_sentence_piece:
            values, indices, dense_shape = process_to_IDs_in_sparse_format(
                sp=self.sp, sentences=user_input)
            embeddings = self.sess.run(
                self.embeddings,
                feed_dict={
                    self.input_placeholder.values: values,
                    self.input_placeholder.indices: indices,
                    self.input_placeholder.dense_shape: dense_shape
                }
            )
        else:
            embeddings = self.sess.run(
                self.embeddings,
                feed_dict={
                    self.input_placeholder: user_input
                }
            )
        tf.logging.info(
            'Successfully generated {} embeddings of length {}.'.format(
                len(embeddings), len(embeddings[0])))
        query_vector = embeddings[0]
        nns, distances = self.annoy_index.get_nns_by_vector(
            query_vector, num_neighbors, search_k=-1, include_distances=True)
        tf.logging.info('Nearest neighbor IDS: {}'.format(nns))
        return nns, distances
def inference ( self , input_text , num_neighbors = 10 , use_sentence_piece = True ) : if len ( input_text ) < 1 : return 'Say something!' tf . logging . info ( 'Input text: {}' . format ( input_text ) ) user_input = [ input_text ] if use_sentence_piece : values , indices , dense_shape = process_to_IDs_in_sparse_format ( sp = self . sp , sentences = user_input ) embeddings = self . sess . run ( self . embeddings , feed_dict = { self . input_placeholder . values : values , self . input_placeholder . indices : indices , self . input_placeholder . dense_shape : dense_shape } ) else : embeddings = self . sess . run ( self . embeddings , feed_dict = { self . input_placeholder : user_input } ) tf . logging . info ( 'Successfully generated {} embeddings of length {}.' . format ( len ( embeddings ) , len ( embeddings [ 0 ] ) ) ) query_vector = embeddings [ 0 ] nns , distances = self . annoy_index . get_nns_by_vector ( query_vector , num_neighbors , search_k = - 1 , include_distances = True ) tf . logging . info ( 'Nearest neighbor IDS: {}' . format ( nns ) ) return nns , distances
Inference from nearest neighbor model.
Inference from nearest neighbor model .
"""Inference from nearest neighbor model.""" # Handle the short input # Build a list of the user input # process unencoded lines to values and IDs in sparse format # run the session # Get embedding of the input text # Extract the query vector of interest. # Get nearest neighbors # Log the ids
self
input_text
num_neighbors
use_sentence_piece