chatbot.py
def beam_search_generator(sess, net, initial_state, initial_sample, early_term_token, beam_width, args):
    # global args

    '''Run beam search! Yield consensus tokens sequentially, as a generator;
    return when reaching early_term_token (newline).

    Args:
        sess: tensorflow session reference
        net: tensorflow net graph (must be compatible with the forward_net function)
        initial_state: initial hidden state of the net
        initial_sample: single token (excluding any seed/priming material)
            to start the generation
        early_term_token: stop when the beam reaches consensus on this token
            (but do not return this token).
        beam_width: how many beams to track
        forward_model_fn: function to forward the model, must be of the form:
            probability_output, beam_state =
                    forward_model_fn(sess, net, beam_state, beam_sample, forward_args)
            (Note: probability_output has to be a valid probability distribution!)
        tot_steps: how many tokens to generate before stopping,
            unless already stopped via early_term_token.
    Returns: a generator to yield a sequence of beam-sampled tokens.'''
    # Store state, outputs and probabilities for up to args.beam_width beams.
    # Initialize with just the one starting entry; it will branch to fill the beam
    # in the first step.
    beam_states = [initial_state] # Stores the best activation states
    beam_outputs = [[initial_sample]] # Stores the best generated output sequences so far.
    beam_probs = [1.] # Stores the cumulative normalized probabilities of the beams so far.
    beam_entps = [1.]

    count = 0
    while True:
        # Keep a running list of the best beam branches for next step.
        # Don't actually copy any big data structures yet, just keep references
        # to existing beam state entries, and then clone them as necessary
        # at the end of the generation step.
        new_beam_indices = []
        new_beam_probs = []
        new_beam_samples = []

        # Iterate through the beam entries.
        for beam_index, beam_state in enumerate(beam_states):

            beam_prob = beam_probs[beam_index]
            # beam_entp = beam_entps[beam_index]

            beam_sample = beam_outputs[beam_index][-1]

            # import pdb
            # pdb.set_trace()
            # Forward the model.
            prediction, beam_states[beam_index] = forward_with_mask( sess, net, beam_state, beam_sample, args) 

            # print (chr(beam_sample),end='')


            # print(sum(1 if p > 0. else 0 for p in prediction))
             # Sample best_tokens from the probability distribution.
            # Sample from the scaled probability distribution beam_width choices
            # (but not more than the number of positive probabilities in scaled_prediction).
            # count = min(beam_width, sum(1 if p > 0. else 0 for p in prediction))

            # best_probs = [p for p in np.random.choice(prediction,beam_width) if p > 0.]
            indexes = np.random.choice(len(prediction),size=args.beam_width,replace=False,p=prediction)
            best_iprobs = [(i,prediction[i]) for i in indexes if prediction[i] > 0.]

            # print(best_iprobs)
            # exit()
            # best_tokens = np.random.choice(len(prediction), size=args.beam_width, replace=False, p=prediction)
            for (token,prob) in best_iprobs:
            # for token in best_tokens:
                # prob = prediction[token] * beam_prob
                prob *= beam_prob
                if len(new_beam_indices) < beam_width:
                    # print('high prob***********')
                    # If we don't have enough new_beam_indices, we automatically qualify.
                    new_beam_indices.append(beam_index)
                    new_beam_probs.append(prob)
                    new_beam_samples.append(token)
                else:
                    # TAC -- simplified this code
                    # print('************low prob***********')
                    # Sample a low-probability beam to possibly replace.
                    
                    # np_new_beamhe_probs = np.array(new_beam_probs)
                    
                    # inverse_probs = -np_new_beam_probs + max(np_new_beam_probs) + min(np_new_beam_probs)
                    # inverse_probs = inverse_probs / sum(inverse_probs)
                    # sampled_beam_index = np.random.choice(beam_width, p=inverse_probs)
                    sampled_beam_index = np.argmin(new_beam_probs)
                    # print (len(sampled_beam_index))
                    # exit()
                    
                    if new_beam_probs[sampled_beam_index] <= prob:
                        # Replace it.
                        new_beam_indices[sampled_beam_index] = beam_index
                        new_beam_probs[sampled_beam_index] = prob
                        new_beam_samples[sampled_beam_index] = token

        # Replace the old states with the new states, first by referencing and then by copying.
        already_referenced = [False] * beam_width
        new_beam_states = []
        new_beam_outputs = []
        for i, new_index in enumerate(new_beam_indices):
            if already_referenced[new_index]:
                new_beam = copy.deepcopy(beam_states[new_index])
            else:
                new_beam = beam_states[new_index]
                already_referenced[new_index] = True
            new_beam_states.append(new_beam)
            new_beam_outputs.append(beam_outputs[new_index] + [new_beam_samples[i]])

        # Normalize the beam probabilities so they don't drop to zero
        beam_probs = new_beam_probs / sum(new_beam_probs)
        beam_states = new_beam_states
        beam_outputs = new_beam_outputs

        # Prune the agreed portions of the outputs
        # and yield the tokens on which the beam has reached consensus.
        l, early_term = consensus_length(beam_outputs, early_term_token)

        # sanity check against models (line frequency alone) that never find
        # termination characters  
        # count += 1
        # if ( count >= 30 ): early_term = True

        #################################################
        # just display the probs as strings 
        # import string
        if args.verbose:
            trans = str.maketrans("\n", u"\u00B7") # center dot for display only
            print ('\n','-'*30)
            strings = []
            for i,bo in enumerate(beam_outputs):
                s = ''.join([chr(i) for i in bo])
                p = beam_probs[i]
                e = sentenceEntropy(s,args)
                strings.append('{:.4f} - {}- {}'.format(p,s,e))
            for s in sorted(strings):
                print(s.translate(trans))
            if l>0:
                print('{:6s} - {}'.format(' ','='*l))
            print('.'*30)
        #################################################

        if l > 0:
            for token in beam_outputs[0][:l]: 
                yield token
            beam_outputs = [output[l:] for output in beam_outputs]
        if early_term: return
Run beam search! Yield consensus tokens sequentially, as a generator;
    return when reaching early_term_token (newline).

    Args:
        sess: tensorflow session reference
        net: tensorflow net graph (must be compatible with the forward_net function)
        initial_state: initial hidden state of the net
        initial_sample: single token (excluding any seed/priming material)
            to start the generation
        early_term_token: stop when the beam reaches consensus on this token
            (but do not return this token).
        beam_width: how many beams to track
        forward_model_fn: function to forward the model, must be of the form:
            probability_output, beam_state =
                    forward_model_fn(sess, net, beam_state, beam_sample, forward_args)
            (Note: probability_output has to be a valid probability distribution!)
        tot_steps: how many tokens to generate before stopping,
            unless already stopped via early_term_token.
    Returns: a generator to yield a sequence of beam-sampled tokens.
Run beam search. Yield consensus tokens sequentially, as a generator;
return when reaching early_term_token (newline).
Run beam search . Yield consensus tokens sequentially as a generator ; return when reaching early_term_token ( newline ) .
def beam_search_generator(sess, net, initial_state, initial_sample, early_term_token, beam_width, args):
    beam_states = [initial_state] 
    beam_outputs = [[initial_sample]] 
    beam_probs = [1.] 
    beam_entps = [1.]
    count = 0
    while True:
        new_beam_indices = []
        new_beam_probs = []
        new_beam_samples = []
        for beam_index, beam_state in enumerate(beam_states):
            beam_prob = beam_probs[beam_index]
            beam_sample = beam_outputs[beam_index][-1]
            prediction, beam_states[beam_index] = forward_with_mask( sess, net, beam_state, beam_sample, args) 
            indexes = np.random.choice(len(prediction),size=args.beam_width,replace=False,p=prediction)
            best_iprobs = [(i,prediction[i]) for i in indexes if prediction[i] > 0.]
            for (token,prob) in best_iprobs:
                prob *= beam_prob
                if len(new_beam_indices) < beam_width:
                    new_beam_indices.append(beam_index)
                    new_beam_probs.append(prob)
                    new_beam_samples.append(token)
                else:
                    sampled_beam_index = np.argmin(new_beam_probs)
                    if new_beam_probs[sampled_beam_index] <= prob:
                        new_beam_indices[sampled_beam_index] = beam_index
                        new_beam_probs[sampled_beam_index] = prob
                        new_beam_samples[sampled_beam_index] = token
        already_referenced = [False] * beam_width
        new_beam_states = []
        new_beam_outputs = []
        for i, new_index in enumerate(new_beam_indices):
            if already_referenced[new_index]:
                new_beam = copy.deepcopy(beam_states[new_index])
            else:
                new_beam = beam_states[new_index]
                already_referenced[new_index] = True
            new_beam_states.append(new_beam)
            new_beam_outputs.append(beam_outputs[new_index] + [new_beam_samples[i]])
        beam_probs = new_beam_probs / sum(new_beam_probs)
        beam_states = new_beam_states
        beam_outputs = new_beam_outputs
        l, early_term = consensus_length(beam_outputs, early_term_token)
        if args.verbose:
            trans = str.maketrans("\n", u"\u00B7") 
            print ('\n','-'*30)
            strings = []
            for i,bo in enumerate(beam_outputs):
                s = ''.join([chr(i) for i in bo])
                p = beam_probs[i]
                e = sentenceEntropy(s,args)
                strings.append('{:.4f} - {}- {}'.format(p,s,e))
            for s in sorted(strings):
                print(s.translate(trans))
            if l>0:
                print('{:6s} - {}'.format(' ','='*l))
            print('.'*30)
        if l > 0:
            for token in beam_outputs[0][:l]: 
                yield token
            beam_outputs = [output[l:] for output in beam_outputs]
        if early_term: return
def beam_search_generator ( sess , net , initial_state , initial_sample , early_term_token , beam_width , args ) : beam_states = [ initial_state ] beam_outputs = [ [ initial_sample ] ] beam_probs = [ 1. ] beam_entps = [ 1. ] count = 0 while True : new_beam_indices = [ ] new_beam_probs = [ ] new_beam_samples = [ ] for beam_index , beam_state in enumerate ( beam_states ) : beam_prob = beam_probs [ beam_index ] beam_sample = beam_outputs [ beam_index ] [ - 1 ] prediction , beam_states [ beam_index ] = forward_with_mask ( sess , net , beam_state , beam_sample , args ) indexes = np . random . choice ( len ( prediction ) , size = args . beam_width , replace = False , p = prediction ) best_iprobs = [ ( i , prediction [ i ] ) for i in indexes if prediction [ i ] > 0. ] for ( token , prob ) in best_iprobs : prob *= beam_prob if len ( new_beam_indices ) < beam_width : new_beam_indices . append ( beam_index ) new_beam_probs . append ( prob ) new_beam_samples . append ( token ) else : sampled_beam_index = np . argmin ( new_beam_probs ) if new_beam_probs [ sampled_beam_index ] <= prob : new_beam_indices [ sampled_beam_index ] = beam_index new_beam_probs [ sampled_beam_index ] = prob new_beam_samples [ sampled_beam_index ] = token already_referenced = [ False ] * beam_width new_beam_states = [ ] new_beam_outputs = [ ] for i , new_index in enumerate ( new_beam_indices ) : if already_referenced [ new_index ] : new_beam = copy . deepcopy ( beam_states [ new_index ] ) else : new_beam = beam_states [ new_index ] already_referenced [ new_index ] = True new_beam_states . append ( new_beam ) new_beam_outputs . append ( beam_outputs [ new_index ] + [ new_beam_samples [ i ] ] ) beam_probs = new_beam_probs / sum ( new_beam_probs ) beam_states = new_beam_states beam_outputs = new_beam_outputs l , early_term = consensus_length ( beam_outputs , early_term_token ) if args . verbose : trans = str . maketrans ( "\n" , u"\u00B7" ) print ( '\n' , '-' * 30 ) strings = [ ] for i , bo in enumerate ( beam_outputs ) : s = '' . join ( [ chr ( i ) for i in bo ] ) p = beam_probs [ i ] e = sentenceEntropy ( s , args ) strings . append ( '{:.4f} - {}- {}' . format ( p , s , e ) ) for s in sorted ( strings ) : print ( s . translate ( trans ) ) if l > 0 : print ( '{:6s} - {}' . format ( ' ' , '=' * l ) ) print ( '.' * 30 ) if l > 0 : for token in beam_outputs [ 0 ] [ : l ] : yield token beam_outputs = [ output [ l : ] for output in beam_outputs ] if early_term : return
Run beam search!
Run beam search!
# global args '''Run beam search! Yield consensus tokens sequentially, as a generator;
    return when reaching early_term_token (newline).

    Args:
        sess: tensorflow session reference
        net: tensorflow net graph (must be compatible with the forward_net function)
        initial_state: initial hidden state of the net
        initial_sample: single token (excluding any seed/priming material)
            to start the generation
        early_term_token: stop when the beam reaches consensus on this token
            (but do not return this token).
        beam_width: how many beams to track
        forward_model_fn: function to forward the model, must be of the form:
            probability_output, beam_state =
                    forward_model_fn(sess, net, beam_state, beam_sample, forward_args)
            (Note: probability_output has to be a valid probability distribution!)
        tot_steps: how many tokens to generate before stopping,
            unless already stopped via early_term_token.
    Returns: a generator to yield a sequence of beam-sampled tokens.''' # Store state, outputs and probabilities for up to args.beam_width beams. # Initialize with just the one starting entry; it will branch to fill the beam # in the first step. # Stores the best activation states # Stores the best generated output sequences so far. # Stores the cumulative normalized probabilities of the beams so far. # Keep a running list of the best beam branches for next step. # Don't actually copy any big data structures yet, just keep references # to existing beam state entries, and then clone them as necessary # at the end of the generation step. # Iterate through the beam entries. # beam_entp = beam_entps[beam_index] # import pdb # pdb.set_trace() # Forward the model. # print (chr(beam_sample),end='') # print(sum(1 if p > 0. else 0 for p in prediction)) # Sample best_tokens from the probability distribution. # Sample from the scaled probability distribution beam_width choices # (but not more than the number of positive probabilities in scaled_prediction). # count = min(beam_width, sum(1 if p > 0. else 0 for p in prediction)) # best_probs = [p for p in np.random.choice(prediction,beam_width) if p > 0.] # print(best_iprobs) # exit() # best_tokens = np.random.choice(len(prediction), size=args.beam_width, replace=False, p=prediction) # for token in best_tokens: # prob = prediction[token] * beam_prob # print('high prob***********') # If we don't have enough new_beam_indices, we automatically qualify. # TAC -- simplified this code # print('************low prob***********') # Sample a low-probability beam to possibly replace. # np_new_beamhe_probs = np.array(new_beam_probs) # inverse_probs = -np_new_beam_probs + max(np_new_beam_probs) + min(np_new_beam_probs) # inverse_probs = inverse_probs / sum(inverse_probs) # sampled_beam_index = np.random.choice(beam_width, p=inverse_probs) # print (len(sampled_beam_index)) # exit() # Replace it. # Replace the old states with the new states, first by referencing and then by copying. # Normalize the beam probabilities so they don't drop to zero # Prune the agreed portions of the outputs # and yield the tokens on which the beam has reached consensus. # sanity check against models (line frequency alone) that never find # termination characters   # count += 1 # if ( count >= 30 ): early_term = True ################################################# # just display the probs as strings  # import string # center dot for display only #################################################
sess
tensorflow session reference
tensorflow session reference
net
tensorflow net graph (must be compatible with the forward_net function)
tensorflow net graph ( must be compatible with the forward_net function )
initial_state
initial hidden state of the net
initial hidden state of the net
initial_sample
single token (excluding any seed/priming material)
to start the generation
single token ( excluding any seed / priming material ) to start the generation
early_term_token
stop when the beam reaches consensus on this token
(but do not return this token).
stop when the beam reaches consensus on this token ( but do not return this token ) .
beam_width
None
args
forward_model_fn
function to forward the model, must be of the form:
probability_output, beam_state
forward_model_fn(sess, net, beam_state, beam_sample, forward_args)
(Note: probability_output has to be a valid probability distribution!)
function to forward the model must be of the form : probability_output beam_state forward_model_fn ( sess net beam_state beam_sample forward_args ) ( Note : probability_output has to be a valid probability distribution! )
tot_steps
None