def generate_text(model, seed, length=512, top_n=None): """ generates text of specified length from trained model with given seed character sequence. """ logger.info("generating %s characters from top %s choices.", length, top_n) logger.info('generating with seed: "%s".', seed) generated = seed encoded = encode_text(seed, get_CHAR2ID()) model.reset_states() for idx in encoded[:-1]: x = np.array([[idx]]) # input shape: (1, 1) # set internal states model.predict(x) next_index = encoded[-1] for i in range(length): x = np.array([[next_index]]) # input shape: (1, 1) probs = model.predict(x) # output shape: (1, 1, vocab_size) next_index = sample_from_probs(probs.squeeze(), top_n) # append to sequence generated += get_ID2CHAR()[next_index] logger.info("generated text: \n%s\n", generated) return generated
def generate_text(model, seed, length=512, top_n=10): """ generates text of specified length from trained model with given seed character sequence. """ logger.info("generating %s characters from top %s choices.", length, top_n) logger.info('generating with seed: "%s".', seed) generated = seed encoded = encode_text(seed).astype(np.int32) model.predictor.reset_state() with chainer.using_config("train", False), chainer.no_backprop_mode(): for idx in encoded[:-1]: x = Variable(np.array([idx])) # input shape: [1] # set internal states model.predictor(x) next_index = encoded[-1] for i in range(length): x = Variable(np.array([next_index], dtype=np.int32)) # input shape: [1] probs = F.softmax(model.predictor(x)) # output shape: [1, vocab_size] next_index = sample_from_probs(probs.data.squeeze(), top_n) # append to sequence generated += ID2CHAR[next_index] logger.info("generated text: \n%s\n", generated) return generated
def generate_text(model, seed, length=512, top_n=2): """ Generates text of specified length from trained model with given seed (e.g. the prefix string). """ logger.info("generating %s characters from top %s choices.", length, top_n) logger.info('generating with seed: "%s".', seed) generated = seed encoded = encode_text(seed) model.reset_states() for idx in encoded[:-1]: x = np.array([[idx]]) # Input shape: (1, 1) # Set internal states model.predict(x) next_index = encoded[-1] for i in range(length): x = np.array([[next_index]]) # Input shape: (1, 1) probs = model.predict(x) # Output shape: (1, 1, vocab_size) next_index = sample_from_probs(probs.squeeze(), top_n) # Append to sequence if ID2CHAR[next_index] in [".", "!", "?"]: generated += ID2CHAR[next_index] break elif ID2CHAR[next_index] == "\n": break generated += ID2CHAR[next_index] logger.info("generated text: \n%s\n", generated) return generated
def generate_text(model, sess, seed, length=512, top_n=10): """ generates text of specified length from trained model with given seed character sequence. """ logger.info("generating %s characters from top %s choices.", length, top_n) logger.info('generating with seed: "%s".', seed) generated = seed encoded = encode_text(seed) x = np.expand_dims(encoded[:-1], 0) # input shape: [1, seq_len] # get rnn state due to seed sequence state = sess.run(model["output_state"], feed_dict={model["X"]: x}) next_index = encoded[-1] for i in range(length): x = np.array([[next_index]]) # input shape: [1, 1] feed_dict = {model["X"]: x, model["input_state"]: state} probs, state = sess.run([model["probs"], model["output_state"]], feed_dict=feed_dict) # output shape: [1, 1, vocab_size] next_index = sample_from_probs(probs.squeeze(), top_n) # append to sequence generated += ID2CHAR[next_index] logger.info("generated text: \n%s\n", generated) return generated
def generate_text(model, seed, length=512, top_n=10): """ generates text of specified length from trained model with given seed character sequence. """ logger.info("generating %s characters from top %s choices.", length, top_n) logger.info('generating with seed: "%s".', seed) generated = seed encoded = mx.nd.array(encode_text(seed)) seq_len = encoded.shape[0] x = F.expand_dims(encoded[:seq_len - 1], 1) # input shape: [seq_len, 1] state = model.begin_state() # get rnn state due to seed sequence _, state = model(x, state) next_index = encoded[seq_len - 1].asscalar() for i in range(length): x = mx.nd.array([[next_index]]) # input shape: [1, 1] logit, state = model(x, state) # output shape: [1, vocab_size] probs = F.softmax(logit) next_index = sample_from_probs(probs.asnumpy().squeeze(), top_n) # append to sequence generated += ID2CHAR[next_index] logger.info("generated text: \n%s\n", generated) return generated