Find the principle online, here only a simple example to show the principle, input a sentence, use bi-LSTM to predict the next word.

import tensorflow as tf import numpy as np sentences = 'Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam quis nostrud exercitation' # UNK Word2idx = {v:k for k,v in. Word2idx = {v:k for k,v in } idx2word = {k:v for k,v in enumerate(words)} v = len(words) # Def make_batch(sentence): hidden = 50 # dim = 50 # input_batch, target_batch = [],[] words = sentence.split() for i,word in enumerate(words[:-1]): Input = [word2idx[word] for word in words[: I +1]] Target = word2idx[words[I +1]] # Target = word2idx[words[I +1]] # Input_batch.append (INPUT) target_Batch.append (Np.eye (V)[target]) # Convert target to one-hot code, Later softmax_cross_ENTROpy_with_logits_v2 will use return input_batch, Target_batch tf.reset_default_graph() # embedding = tf.get_variable(name="embedding", shape=[V, dim], initializer=tf.random_normal_initializer) X = tf.placeholder(tf.int32, [None, Embedding_lookup (embedd.embedding_lookup (embedd.embedding_lookup (embedd.embedding_lookup (embedd.embedding_lookup)) Y = tf.placeholder(embedd.int32, [None, placeholder, placeholder, placeholder) V]) # LSTM lSTM_FW_cell = tf.nn.rnN_cell.lstmcell (hidden) # LSTM LSTM_BW_cell = tF.nn.rnn_cell.lSTmcell (hidden) # Outputs: (fw=[batch_size, step, hidden], bw=[batch_size, step, hidden]) (fw=(c=[batch_size, hidden], h=[batch_size, hidden]), bw=(c=[batch_size, hidden], h=[batch_size, hidden])) outputs, states = tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell, lstm_bw_cell, XX, Dtype = tf.concat([outputs[0], outputs[1]], 2) # 2*hidden] outputs = tf. Transpose (outputs, [1, 2])[-1] # [batch_size, W = tf.variable (tf.random_normal([2*hidden, RTD = RTD, RTD = RTD, RTD = RTD, RTD = RTD, RTD = RTD V] # Cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2) Labels =Y)) optimizer = tf.train.adamoptimizer (0.001). Minimize (cost) # Predictive prediction = tf.argmax(logits, 1) init = tf.global_variables_initializer() sess = tf.session () sess.run(init) # Production input and tag input_batch, Target_batch = make_batch(sentence) # training model for epoch in range(3000): _, loss = sess.run([optimizer, cost], feed_dict={X:input_batch, Y:target_batch}) if (epoch+1)%500 == 0: Print (" epoch = ", '04% d % (epoch + 1), "loss =",' 04 f % % (loss) # forecasts predict = sess. Run ((prediction), feed_dict={X:input_batch}) for i,idxs in enumerate(input_batch): Print (". Join ([idx2word[idx] for idx in idxs if idx!=0]),idx2word[predict[0]], idx2word[np.argmax(target_batch[i])])Copy the code

Print the results:

Epoch = 0500 loss= 0.001467 EPOCH = 1000 loss= 0.000409 EPOCH = 1500 loss= 0.000199 epoch= 2000 loss= 0.000116 epoch= 2500 Loss = 0.000074 (epoch) = 3000 loss= 0.000050 (epoch) Dolor Lorem ipsum dolor is used to predict the onset of the disease. Consectetur Lorem ipsum dolor sit Amet Consectetur Lorem ipsum dolor sit amet consectetur adipisicing Elit Lorem ipsum Dolor sit amet consectetur adipisicing Sed Lorem ipsum dolor sit amet consectetur adipisicing elit sed Do Lorem ipsum dolor sit amet consectetur adipisicing elit sed do Eiusmod Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod Tempor Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor Incididunt Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt Ut Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut Labore Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore Et Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et Real words: dolore Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore Predicted word: Magna magna Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna Predictive words: Aliqua aliqua Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore Magna Aliqua predicted words: Ut Ut Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna Aliqua Ut Predicted words: enim Real words: enim Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna Aliqua Ut enim ad Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna Aliqua Ut enim AD minim Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna Aliqua Ut enim AD minim veniam Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore Magna Aliqua Ut enim AD minim veniam quis Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna Aliqua Ut enim AD minim veniam quis nostrud Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore Magna aliqua Ut enim AD minim veniam quis nostrud Predicted word: exercitation real word: exercitationCopy the code