def generate_sentences(self, len_sentences=60, load=True):
"""
Generate sentences given a trained LSTM model
:param len_sentences: the length of the sentences to be generated
:param load: whether to load in a model, or to use the trained one, only works if you ran train_model before
:return:
"""
model = lstm_model.create_model(word_coding)
if load:
model.load_weights('lstm-weights')
else:
if not ModelTrainer.model_trained:
raise Exception("The model hasn't been trained. Either train it or load in the weights from a file")
model = self.model
seedSrc = i_D
outSentences = []
while len(outSentences) < len_sentences:
start_index = random.randint(0, len(seedSrc) - 1)
sentence = seedSrc[start_index: start_index + 1]
sentOutput = ''
for iteration in range(500):
vecsentence = []
for vcode in sentence[0]:
vecsentence.append(self.vmodel[coded_word[vcode]])
vecsentence = np.reshape(vecsentence, (1, len(vecsentence), 300))
preds = model.predict({'input': vecsentence}, verbose=0)['output1'][0]
next_index = sample(preds, 0.2)
if next_index in coded_word:
next_char = coded_word[next_index]
sentence = np.append(sentence[0][1:], [next_index]).reshape(np.asarray(sentence).shape)
sentOutput += next_char + ' '
print(sentOutput)
lstm_trainer_generator.py 文件源码
python
阅读 20
收藏 0
点赞 0
评论 0
评论列表
文章目录