深度學(xué)習(xí)TextLSTM的tensorflow1.14實(shí)現(xiàn)示例
對(duì)單詞最后一個(gè)字母的預(yù)測
LSTM 的原理自己找,這里只給出簡單的示例代碼,就是對(duì)單詞最后一個(gè)字母的預(yù)測。
# LSTM 的原理自己找,這里只給出簡單的示例代碼
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
# 預(yù)測最后一個(gè)字母
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 # 時(shí)間步長大小
hidden = 50 # 隱藏層大小
dim = 32 # 詞向量維度
def make_batch(words):
input_batch, target_batch = [], []
for word in words:
input = [word2idx[c] for c in word[:-1]] # 除最后一個(gè)字符的所有字符當(dāng)作輸入
target = word2idx[word[-1]] # 最后一個(gè)字符當(dāng)作標(biāo)簽
input_batch.append(input)
target_batch.append(np.eye(V)[target]) # 這里將標(biāo)簽轉(zhuǎn)換為 one-hot ,后面計(jì)算 softmax_cross_entropy_with_logits_v2 的時(shí)候會(huì)用到
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])
# 將輸入進(jìn)行詞嵌入轉(zhuǎn)換
XX = tf.nn.embedding_lookup(embedding, X)
Y = tf.placeholder(tf.int32, [None, V])
# 定義 LSTM cell
cell = tf.nn.rnn_cell.BasicLSTMCell(hidden)
# 隱層計(jì)算結(jié)果
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])
# 隱層連接分類器的權(quán)重和偏置參數(shù)
W = tf.Variable(tf.random_normal([hidden, V]))
b = tf.Variable(tf.random_normal([V]))
# 這里只用到了最后輸出的 c 向量 states[0] (也可以用所有時(shí)間點(diǎn)的輸出特征向量)
feature = tf.matmul(states[0], W) + b # [batch_size, n_class]
# 計(jì)算損失并進(jìn)行迭代優(yōu)化
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=feature, labels=Y))
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
# 預(yù)測
prediction = tf.argmax(feature, 1)
# 初始化 tf
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
# 生產(chǎn)輸入和標(biāo)簽
input_batch, target_batch = make_batch(words)
# 訓(xùn)練模型
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))
# 預(yù)測結(jié)果
predict = sess.run([prediction], feed_dict={X:input_batch})
print([words[i][:-1]+' '+idx2word[c] for i,c in enumerate(predict[0])])
結(jié)果打印
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']
以上就是深度學(xué)習(xí)TextLSTM的tensorflow1.14實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于深度學(xué)習(xí)TextLSTM tensorflow的資料請關(guān)注腳本之家其它相關(guān)文章!
- Tensorflow2.4從頭訓(xùn)練Word?Embedding實(shí)現(xiàn)文本分類
- Tensorflow 2.4 搭建單層和多層 Bi-LSTM 模型
- 深度學(xué)習(xí)Tensorflow?2.4?完成遷移學(xué)習(xí)和模型微調(diào)
- 深度學(xué)習(xí)Tensorflow2.8?使用?BERT?進(jìn)行文本分類
- 深度學(xué)習(xí)Tensorflow2.8實(shí)現(xiàn)GRU文本生成任務(wù)詳解
- 深度學(xué)習(xí)TextRNN的tensorflow1.14實(shí)現(xiàn)示例
- Tensorflow2.10實(shí)現(xiàn)圖像分割任務(wù)示例詳解
- 使用TensorFlow創(chuàng)建生成式對(duì)抗網(wǎng)絡(luò)GAN案例
相關(guān)文章
利用pyecharts讀取csv并進(jìn)行數(shù)據(jù)統(tǒng)計(jì)可視化的實(shí)現(xiàn)
這篇文章主要介紹了利用pyecharts讀取csv并進(jìn)行數(shù)據(jù)統(tǒng)計(jì)可視化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
利用Python實(shí)現(xiàn)網(wǎng)絡(luò)測試的示例代碼
Speedtest CLI 為命令行帶來 Speedtest 背后的可信技術(shù)和全球服務(wù)器網(wǎng)絡(luò)。本文將利用它進(jìn)行網(wǎng)絡(luò)測試,感興趣的小伙伴可以了解一下2022-04-04
局域網(wǎng)內(nèi)python socket實(shí)現(xiàn)windows與linux間的消息傳送
這篇文章主要介紹了局域網(wǎng)內(nèi)python socket實(shí)現(xiàn)windows與linux間的消息傳送的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04

