深度学习TextLSTM的tensorflow1.14实现示例

作者:我是王大你是谁 时间:2022-07-12 06:26:46 

对单词最后一个字母的预测

LSTM 的原理自己找,这里只给出简单的示例代码,就是对单词最后一个字母的预测。

# LSTM 的原理自己找,这里只给出简单的示例代码
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
# 预测最后一个字母
words = ['make','need','coal','word','love','hate','live','home','hash','star']
# 字典集
chars = [c for c in 'abcdefghijklmnopqrstuvwxyz']
# 生成字符索引字典
word2idx = {v:k for k,v in enumerate(chars)}
idx2word = {k:v for k,v in enumerate(chars)}
V = len(chars) # 字典大小
step = 3 # 时间步长大小
hidden = 50 # 隐藏层大小
dim = 32 # 词向量维度
def make_batch(words):
   input_batch, target_batch = [], []
   for word in words:
       input = [word2idx[c] for c in word[:-1]] # 除最后一个字符的所有字符当作输入
       target = word2idx[word[-1]] # 最后一个字符当作标签
       input_batch.append(input)
       target_batch.append(np.eye(V)[target]) # 这里将标签转换为 one-hot ,后面计算 softmax_cross_entropy_with_logits_v2 的时候会用到
   return input_batch, target_batch
# 初始化词向量
embedding  = tf.get_variable("embedding", shape=[V, dim], initializer=tf.random_normal_initializer)
X = tf.placeholder(tf.int32, [None, step])
# 将输入进行词嵌入转换
XX = tf.nn.embedding_lookup(embedding, X)
Y = tf.placeholder(tf.int32, [None, V])
# 定义 LSTM cell
cell = tf.nn.rnn_cell.BasicLSTMCell(hidden)
# 隐层计算结果
outputs, states = tf.nn.dynamic_rnn(cell, XX, dtype=tf.float32)   # output:  [batch_size, step, hidden]  states: (c=[batch_size, hidden], h=[batch_size, hidden])
# 隐层连接分类器的权重和偏置参数
W = tf.Variable(tf.random_normal([hidden, V]))
b = tf.Variable(tf.random_normal([V]))
# 这里只用到了最后输出的 c 向量 states[0] (也可以用所有时间点的输出特征向量)
feature = tf.matmul(states[0], W) + b   # [batch_size, n_class]
# 计算损失并进行迭代优化
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=feature, labels=Y))
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
# 预测
prediction = tf.argmax(feature, 1)
# 初始化 tf
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
# 生产输入和标签
input_batch, target_batch = make_batch(words)
# 训练模型
for epoch in range(1000):
   _, loss = sess.run([optimizer, cost], feed_dict={X:input_batch, Y:target_batch})
   if (epoch+1)%100 == 0:
       print('epoch: ', '%04d'%(epoch+1), 'cost=', '%04f'%(loss))
# 预测结果
predict = sess.run([prediction], feed_dict={X:input_batch})
print([words[i][:-1]+' '+idx2word[c] for i,c in enumerate(predict[0])])  

结果打印

epoch:  0100 cost= 0.003784
epoch:  0200 cost= 0.001891
epoch:  0300 cost= 0.001122
epoch:  0400 cost= 0.000739
epoch:  0500 cost= 0.000522
epoch:  0600 cost= 0.000388
epoch:  0700 cost= 0.000300
epoch:  0800 cost= 0.000238
epoch:  0900 cost= 0.000193
epoch:  1000 cost= 0.000160
['mak e', 'nee d', 'coa l', 'wor d', 'lov e', 'hat e', 'liv e', 'hom e', 'has h', 'sta r'] 

来源:https://juejin.cn/post/6949412997903155230

标签:tensorflow,TextLSTM,深度学习
0
投稿

猜你喜欢

  • python神经网络使用Keras构建RNN训练

    2021-07-19 21:12:15
  • Python使用dict.fromkeys()快速生成一个字典示例

    2022-05-10 08:13:23
  • 使用Python来开发Markdown脚本扩展的实例分享

    2023-06-02 06:48:06
  • python OpenCV学习笔记之绘制直方图的方法

    2023-06-04 02:34:38
  • Python 2种方法求某个范围内的所有素数(质数)

    2022-09-07 08:43:40
  • sogou地图API用法实例教程

    2024-04-16 10:30:08
  • JS版的date函数(和PHP的date函数一样)

    2023-11-15 02:02:11
  • Python中的tkinter库简单案例详解

    2021-01-20 14:25:16
  • python中的计时器timeit的使用方法

    2023-04-24 14:50:56
  • sublime text配置node.js调试(图文教程)

    2023-07-04 14:07:57
  • Python concurrent.futures模块使用实例

    2023-10-06 07:30:22
  • linux环境中没有网络怎么下载python

    2021-01-30 04:42:12
  • Python浮点型(float)运算结果不正确的解决方案

    2023-10-04 16:57:44
  • Python3.5 Pandas模块缺失值处理和层次索引实例详解

    2021-05-20 00:35:50
  • 在Golang中使用Redis的方法示例

    2024-04-28 09:16:01
  • python 直接赋值和copy的区别详解

    2023-12-14 04:08:49
  • python 经典数字滤波实例

    2022-08-26 10:23:22
  • Python中使用 Selenium 实现网页截图实例

    2022-07-04 06:28:16
  • JavaScript也谈内存优化

    2024-02-25 16:33:17
  • Python语言生成水仙花数代码示例

    2022-11-16 18:17:48
  • asp之家 网络编程 m.aspxhome.com