深度學(xué)習(xí)TextRNN的tensorflow1.14實(shí)現(xiàn)示例
實(shí)現(xiàn)對下一個(gè)單詞的預(yù)測
RNN 原理自己找,這里只給出簡單例子的實(shí)現(xiàn)代碼
import tensorflow as tf
import numpy as np
tf.reset_default_graph()
sentences = ['i love damao','i like mengjun','we love all']
words = list(set(" ".join(sentences).split()))
word2idx = {v:k for k,v in enumerate(words)}
idx2word = {k:v for k,v in enumerate(words)}
V = len(words) # 詞典大小
step = 2 # 時(shí)間序列長度
hidden = 5 # 隱層大小
dim = 50 # 詞向量維度
# 制作輸入和標(biāo)簽
def make_batch(sentences):
input_batch = []
target_batch = []
for sentence in sentences:
words = sentence.split()
input = [word2idx[word] for word in words[:-1]]
target = word2idx[words[-1]]
input_batch.append(input)
target_batch.append(np.eye(V)[target]) # 這里將標(biāo)簽改為 one-hot 編碼,之后計(jì)算交叉熵的時(shí)候會用到
return input_batch, target_batch
# 初始化詞向量
embedding = tf.get_variable(shape=[V, dim], initializer=tf.random_normal_initializer(), name="embedding")
X = tf.placeholder(tf.int32, [None, step])
XX = tf.nn.embedding_lookup(embedding, X)
Y = tf.placeholder(tf.int32, [None, V])
# 定義 cell
cell = tf.nn.rnn_cell.BasicRNNCell(hidden)
# 計(jì)算各個(gè)時(shí)間點(diǎn)的輸出和隱層輸出的結(jié)果
outputs, hiddens = tf.nn.dynamic_rnn(cell, XX, dtype=tf.float32) # outputs: [batch_size, step, hidden] hiddens: [batch_size, hidden]
# 這里將所有時(shí)間點(diǎn)的狀態(tài)向量都作為了后續(xù)分類器的輸入(也可以只將最后時(shí)間節(jié)點(diǎn)的狀態(tài)向量作為后續(xù)分類器的輸入)
W = tf.Variable(tf.random_normal([step*hidden, V]))
b = tf.Variable(tf.random_normal([V]))
L = tf.matmul(tf.reshape(outputs,[-1, step*hidden]), W) + b
# 計(jì)算損失并進(jìn)行優(yōu)化
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=L))
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
# 預(yù)測
prediction = tf.argmax(L, 1)
# 初始化 tf
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
# 喂訓(xùn)練數(shù)據(jù)
input_batch, target_batch = make_batch(sentences)
for epoch in range(5000):
_, loss = sess.run([optimizer, cost], feed_dict={X:input_batch, Y:target_batch})
if (epoch+1)%1000 == 0:
print("epoch: ", '%04d'%(epoch+1), 'cost= ', '%04f'%(loss))
# 預(yù)測數(shù)據(jù)
predict = sess.run([prediction], feed_dict={X: input_batch})
print([sentence.split()[:2] for sentence in sentences], '->', [idx2word[n] for n in predict[0]])
結(jié)果打印
epoch: 1000 cost= 0.008979
epoch: 2000 cost= 0.002754
epoch: 3000 cost= 0.001283
epoch: 4000 cost= 0.000697
epoch: 5000 cost= 0.000406
[['i', 'love'], ['i', 'like'], ['we', 'love']] -> ['damao', 'mengjun', 'all']
以上就是深度學(xué)習(xí)TextRNN的tensorflow1.14實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于深度學(xué)習(xí)TextRNN tensorflow的資料請關(guān)注腳本之家其它相關(guān)文章!
- python循環(huán)神經(jīng)網(wǎng)絡(luò)RNN函數(shù)tf.nn.dynamic_rnn使用
- python人工智能tensorflow構(gòu)建循環(huán)神經(jīng)網(wǎng)絡(luò)RNN
- Python使用循環(huán)神經(jīng)網(wǎng)絡(luò)解決文本分類問題的方法詳解
- 基于循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)實(shí)現(xiàn)影評情感分類
- 基于循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的古詩生成器
- TensorFlow實(shí)現(xiàn)RNN循環(huán)神經(jīng)網(wǎng)絡(luò)
- 循環(huán)神經(jīng)網(wǎng)絡(luò)TextRNN實(shí)現(xiàn)情感短文本分類任務(wù)
相關(guān)文章
python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
這篇文章主要介紹了python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
詳解Django自定義圖片和文件上傳路徑(upload_to)的2種方式
這篇文章主要介紹了詳解Django自定義圖片和文件上傳路徑(upload_to)的2種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python中使用urllib2防止302跳轉(zhuǎn)的代碼例子
這篇文章主要介紹了Python中使用urllib2防止302跳轉(zhuǎn)的代碼例子,即避免302跳轉(zhuǎn)的實(shí)現(xiàn),需要的朋友可以參考下2014-07-07
python中使用PIL制作并驗(yàn)證圖片驗(yàn)證碼
本篇文章給大家分享了python中使用PIL制作并驗(yàn)證圖片驗(yàn)證碼的具體代碼以及說明,需要的朋友參考下吧。2018-03-03
python3實(shí)現(xiàn)名片管理系統(tǒng)(控制臺版)
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)名片管理系統(tǒng)控制臺版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
Python?調(diào)用GPT-3?API實(shí)現(xiàn)過程詳解
這篇文章主要為大家介紹了Python?調(diào)用GPT-3?API實(shí)現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
如何用python將文件夾內(nèi)多個(gè)excel表格合并成總表
前幾天遇見這么一個(gè)問題,手上有很多張表格,這些表格中都只有一個(gè)sheet,需要把這些表匯總到一張表,下面這篇文章主要給大家介紹了關(guān)于如何用python將文件夾內(nèi)多個(gè)excel表格合并成總表的相關(guān)資料,需要的朋友可以參考下2023-06-06

