# 使用Keras 构建基于 LSTM 模型的故事生成器（二）

### Step2:导入数据分析库并进行分析

from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout, Bidirectional
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.models import Sequential
from tensorflow.keras import regularizers
import tensorflow.keras.utils as ku
import numpy as np
import tensorflow as tf
import pickle
data=open('stories.txt',encoding="utf8").read()

### Step3:使用 NLP 库预处理数据

# Converting the text to lowercase and splitting it
corpus = data.lower().split("\n")
# Tokenization
tokenizer = Tokenizer()
tokenizer.fit_on_texts(corpus)
total_words = len(tokenizer.word_index) + 1
print(total_words)

# create input sequences using list of tokens
input_sequences = []
for line in corpus:
token_list = tokenizer.texts_to_sequences([line])[0]
for i in range(1, len(token_list)):
n_gram_sequence = token_list[:i+1]
input_sequences.append(n_gram_sequence)
max_sequence_len = max([len(x) for x in input_sequences])
print(max_sequence_len)
# create predictors and label
predictors, label = input_sequences[:,:-1],input_sequences[:,-1]
label = ku.to_categorical(label, num_classes=total_words)

### Step 4:搭建模型

model = Sequential()
print(model.summary())

history = model.fit(predictors, label, epochs=200, verbose=0)

### Step 5:结果分析

import matplotlib.pyplot as plt
acc = history.history['accuracy']
loss = history.history['loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'b', label='Training accuracy')
plt.title('Training accuracy')
plt.figure()
plt.plot(epochs, loss, 'b', label='Training Loss')
plt.title('Training loss')
plt.legend()
plt.show()

### Step 6:保存模型

# serialize model to JSON
model_json=model.to_json()
with open("model.json","w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")

### Step 7:进行预测

seed_text = "As i walked, my heart sank"
next_words = 100
for _ in range(next_words):
token_list = tokenizer.texts_to_sequences([seed_text])[0]
predicted = model.predict_classes(token_list, verbose=0)
output_word = ""
for word, index in tokenizer.word_index.items():
if index == predicted:
output_word = word
break
seed_text += " " + output_word
print(seed_text)

As i walked, my heart sank until he was alarmed by the voice of the hunterand realised what could have happened with him he flew away the boy crunchedbefore it disguised herself as another effort to pull out the bush which he didthe next was a small tree which the child had to struggle a lot to pull outfinally the old man showed him a bigger tree and asked the child to pull itout the boy did so with ease and they walked on the morning she was askedhow she had slept as a while they came back with me

|
21天前
|

7.1.3.2、使用飞桨实现基于LSTM的情感分析模型的网络定义

50 0
|
21天前
|

7.1.3、使用飞桨实现基于LSTM的情感分析模型

29 0
|
3月前
|

**摘要：** 本文介绍了LSTM（长短期记忆网络）的发展背景和重要性，以及其创始人Sepp Hochreiter新推出的xLSTM。LSTM是为解决传统RNN长期依赖问题而设计的，广泛应用于NLP和时间序列预测。文章详细阐述了LSTM的基本概念、核心原理、实现方法和实际应用案例，包括文本生成和时间序列预测。此外，还讨论了LSTM与Transformer的竞争格局。最后，鼓励读者深入学习和探索AI领域。
45 7
|
3月前
|

RNN、LSTM、GRU神经网络构建人名分类器（三）

52 5
|
3月前
|

RNN、LSTM、GRU神经网络构建人名分类器（一）

58 4
|
2月前
|

Python实现循环神经网络RNN-LSTM回归模型项目实战(股票价格预测)
Python实现循环神经网络RNN-LSTM回归模型项目实战(股票价格预测)
105 0
|
4月前
|

【MATLAB】基于VMD-SSA-LSTM的回归预测模型
【MATLAB】基于VMD-SSA-LSTM的回归预测模型
120 4
|
3月前
|

【自然语言处理NLP】Bert预训练模型、Bert上搭建CNN、LSTM模型的输入、输出详解
【自然语言处理NLP】Bert预训练模型、Bert上搭建CNN、LSTM模型的输入、输出详解
72 0
|
4月前
|

76 1
|
1月前
|

【机器学习】面试题：LSTM长短期记忆网络的理解？LSTM是怎么解决梯度消失的问题的？还有哪些其它的解决梯度消失或梯度爆炸的方法？

40 2