深度學(xué)習(xí)TextLSTM的tensorflow1.14實現(xiàn)示例
對單詞最后一個字母的預(yù)測
LSTM 的原理自己找,這里只給出簡單的示例代碼,就是對單詞最后一個字母的預(yù)測。
# LSTM 的原理自己找,這里只給出簡單的示例代碼 import tensorflow as tf import numpy as np tf.reset_default_graph() # 預(yù)測最后一個字母 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]] # 除最后一個字符的所有字符當(dāng)作輸入 target = word2idx[word[-1]] # 最后一個字符當(dāng)作標(biāo)簽 input_batch.append(input) target_batch.append(np.eye(V)[target]) # 這里將標(biāo)簽轉(zhuǎn)換為 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]) # 將輸入進行詞嵌入轉(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) # 隱層計算結(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] (也可以用所有時間點的輸出特征向量) feature = tf.matmul(states[0], W) + b # [batch_size, n_class] # 計算損失并進行迭代優(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實現(xiàn)示例的詳細內(nèi)容,更多關(guān)于深度學(xué)習(xí)TextLSTM tensorflow的資料請關(guān)注腳本之家其它相關(guān)文章!
- Tensorflow2.4從頭訓(xùn)練Word?Embedding實現(xiàn)文本分類
- Tensorflow 2.4 搭建單層和多層 Bi-LSTM 模型
- 深度學(xué)習(xí)Tensorflow?2.4?完成遷移學(xué)習(xí)和模型微調(diào)
- 深度學(xué)習(xí)Tensorflow2.8?使用?BERT?進行文本分類
- 深度學(xué)習(xí)Tensorflow2.8實現(xiàn)GRU文本生成任務(wù)詳解
- 深度學(xué)習(xí)TextRNN的tensorflow1.14實現(xiàn)示例
- Tensorflow2.10實現(xiàn)圖像分割任務(wù)示例詳解
- 使用TensorFlow創(chuàng)建生成式對抗網(wǎng)絡(luò)GAN案例
相關(guān)文章
celery4+django2定時任務(wù)的實現(xiàn)代碼
這篇文章主要介紹了celery4+django2定時任務(wù)的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12PyTorch解決ModuleNotFoundError: No module named
本文主要介紹了PyTorch解決ModuleNotFoundError: No module named ‘torch’,這個錯誤意味著我們的Python環(huán)境中沒有安裝PyTorch庫,無法正常使用其功能,下面就來具體介紹一下2024-03-03Django的URLconf中使用缺省視圖參數(shù)的方法
這篇文章主要介紹了Django的URLconf中使用缺省視圖參數(shù)的方法,Django是最著名的Python的web開發(fā)框架,需要的朋友可以參考下2015-07-07Python編程異步爬蟲之a(chǎn)iohttp模塊的基本用法
aiohttp?模塊是一個基于?asyncio?的?HTTP?客戶端和服務(wù)器框架,可以用于異步處理?HTTP?請求和響應(yīng),這篇文章給大家介紹Python編程異步爬蟲之a(chǎn)iohttp模塊的基本用法,感興趣的朋友一起看看吧2024-03-03