usrbinenv python3 run the samplemodel modelname124m string which model to use seednone integer seed for random number generators fix seed to reproduce results nsamples0 number of samples to return if 0 continues to generate samples indefinately batchsize1 number of batches only affects speedmemory lengthnone number of tokens in generated text if none default is determined by model hyperparameters temperature1 float value controlling randomness in boltzmann distribution lower temperature results in less random completions as the temperature approaches zero the model will become deterministic and repetitive higher temperature results in more random completions topk0 integer value controlling diversity 1 means only 1 word is considered for each step token resulting in deterministic completions while 40 means 40 words are considered at each step 0 default is a special setting meaning no restrictions 40 generally is a good value modelsdir path to parent folder containing model subfolders i e contains the modelname folder usr bin env python3 run the sample_model model_name 124m string which model to use seed none integer seed for random number generators fix seed to reproduce results nsamples 0 number of samples to return if 0 continues to generate samples indefinately batch_size 1 number of batches only affects speed memory length none number of tokens in generated text if none default is determined by model hyperparameters temperature 1 float value controlling randomness in boltzmann distribution lower temperature results in less random completions as the temperature approaches zero the model will become deterministic and repetitive higher temperature results in more random completions top_k 0 integer value controlling diversity 1 means only 1 word is considered for each step token resulting in deterministic completions while 40 means 40 words are considered at each step 0 default is a special setting meaning no restrictions 40 generally is a good value models_dir path to parent folder containing model subfolders i e contains the model_name folder
import fire
import json
import os
import numpy as np
import tensorflow as tf

import model, sample, encoder

def sample_model(
    model_name='124M',
    seed=None,
    nsamples=0,
    batch_size=1,
    length=None,
    temperature=1,
    top_k=0,
    top_p=1,
    models_dir='models',
):
    
    models_dir = os.path.expanduser(os.path.expandvars(models_dir))
    enc = encoder.get_encoder(model_name, models_dir)
    hparams = model.default_hparams()
    with open(os.path.join(models_dir, model_name, 'hparams.json')) as f:
        hparams.override_from_dict(json.load(f))

    if length is None:
        length = hparams.n_ctx
    elif length > hparams.n_ctx:
        raise ValueError("Can't get samples longer than window size: %s" % hparams.n_ctx)

    with tf.Session(graph=tf.Graph()) as sess:
        np.random.seed(seed)
        tf.set_random_seed(seed)

        output = sample.sample_sequence(
            hparams=hparams, length=length,
            start_token=enc.encoder['<|endoftext|>'],
            batch_size=batch_size,
            temperature=temperature, top_k=top_k, top_p=top_p
        )[:, 1:]

        saver = tf.train.Saver()
        ckpt = tf.train.latest_checkpoint(os.path.join(models_dir, model_name))
        saver.restore(sess, ckpt)

        generated = 0
        while nsamples == 0 or generated < nsamples:
            out = sess.run(output)
            for i in range(batch_size):
                generated += batch_size
                text = enc.decode(out[i])
                print("=" * 40 + " SAMPLE " + str(generated) + " " + "=" * 40)
                print(text)

if __name__ == '__main__':
    fire.Fire(sample_model)