欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

使用 keras 构建 GRU 神经网络,创作莎士比亚小说

最编程 2024-10-09 07:09:00
...

复现环境:

完整代码如下:

import tensorflow as tf
import keras
import datetime

# 将输入文本tokenize为token形成词典vocabulary,使用词典中token的索引id对文本每个token进行编码.
data = 'I love you and me ?'  # 数据
text_layer = keras.layers.TextVectorization(
    split="character",
    standardize="lower"
)  # 创建 TextVectorization 层
text_layer.adapt(data)  # 数据加入 TextVectorization 层
vocabulary = text_layer.get_vocabulary()  # 得到所有单词字典(字典里多了 '' '[UNK]')
print('vocabulary=', vocabulary)
encoded_tf = text_layer(data)  # 得到 data 中字典下标组成的数组
print('encod