欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深度學(xué)習(xí)TextRNN的tensorflow1.14實(shí)現(xiàn)示例

 更新時(shí)間:2023年01月05日 15:29:32   作者:我是王大你是誰(shuí)  
這篇文章主要介紹了深度學(xué)習(xí)TextRNN的tensorflow1.14實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

實(shí)現(xiàn)對(duì)下一個(gè)單詞的預(yù)測(cè)

RNN 原理自己找,這里只給出簡(jiǎn)單例子的實(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í)間序列長(zhǎng)度
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í)候會(huì)用到
    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ù)分類(lèi)器的輸入(也可以只將最后時(shí)間節(jié)點(diǎn)的狀態(tài)向量作為后續(xù)分類(lèi)器的輸入)
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ù)測(cè)
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ù)測(cè)數(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的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Django自定義列表 models字段顯示方式

    Django自定義列表 models字段顯示方式

    這篇文章主要介紹了Django自定義列表 models字段顯示方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • 關(guān)于nn.BatchNorm1d()用法及說(shuō)明

    關(guān)于nn.BatchNorm1d()用法及說(shuō)明

    這篇文章主要介紹了關(guān)于nn.BatchNorm1d()用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python 自動(dòng)化辦公之批量修改文件名實(shí)操

    python 自動(dòng)化辦公之批量修改文件名實(shí)操

    這篇文章主要介紹了python 自動(dòng)化辦公之批量修改文件名實(shí)操,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • 使用python-pptx操作PPT的示例詳解

    使用python-pptx操作PPT的示例詳解

    python對(duì)PPT演示文檔讀寫(xiě),是通過(guò)第三方庫(kù)python-pptx實(shí)現(xiàn)的,python-pptx是用于創(chuàng)建和更新?PowerPoint文件的?Python?庫(kù)。本文主要介紹了python-pptx操作PPT的相關(guān)示例,希望對(duì)大家有所幫助
    2023-01-01
  • Python3.6.2調(diào)用ffmpeg的方法

    Python3.6.2調(diào)用ffmpeg的方法

    今天小編就為大家分享一篇Python3.6.2調(diào)用ffmpeg的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • pandas loc iloc ix用法詳細(xì)分析

    pandas loc iloc ix用法詳細(xì)分析

    pandas處理數(shù)據(jù)時(shí),我們會(huì)經(jīng)常看到dataframe結(jié)構(gòu)使用loc, iloc, ix等方法,那么這些方法到底有啥區(qū)別,下面我們來(lái)進(jìn)行詳細(xì)分析,感興趣的朋友跟隨小編一起看看吧
    2023-01-01
  • python非對(duì)稱(chēng)加密算法RSA實(shí)現(xiàn)原理與應(yīng)用詳解

    python非對(duì)稱(chēng)加密算法RSA實(shí)現(xiàn)原理與應(yīng)用詳解

    RSA加密算法是一種非對(duì)稱(chēng)加密算法,RSA算法的安全性基于大數(shù)分解的困難性,即已知兩個(gè)大素?cái)?shù)p和q的乘積n,求解p和q非常困難,RSA算法廣泛應(yīng)用于數(shù)據(jù)加密和數(shù)字簽名等領(lǐng)域,本文將詳細(xì)介紹如何在Python中使用RSA算法進(jìn)行加密和解密,需要的朋友可以參考下
    2024-09-09
  • Python使用pywebview開(kāi)發(fā)桌面應(yīng)用的全過(guò)程

    Python使用pywebview開(kāi)發(fā)桌面應(yīng)用的全過(guò)程

    當(dāng)使用桌面應(yīng)用程序的時(shí)候,有沒(méi)有那么一瞬間,想學(xué)習(xí)一下桌面應(yīng)用程序開(kāi)發(fā)?下面這篇文章主要給大家介紹了關(guān)于Python使用pywebview開(kāi)發(fā)桌面應(yīng)用的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • 使用DataFrame刪除行和列的實(shí)例講解

    使用DataFrame刪除行和列的實(shí)例講解

    下面小編就為大家分享一篇使用DataFrame刪除行和列的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Python如何查看兩個(gè)數(shù)據(jù)庫(kù)的同名表的字段名差異

    Python如何查看兩個(gè)數(shù)據(jù)庫(kù)的同名表的字段名差異

    這篇文章主要介紹了Python如何查看兩個(gè)數(shù)據(jù)庫(kù)的同名表的字段名差異,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05

最新評(píng)論