tf.contrib.legacy_seq2seq.basic_rnn_seq2seq 函数 example 最简单实现

tf.contrib.legacy_seq2seq.basic_rnn_seq2seq 函数 example 最简单实现

函数文档:https://www.tensorflow.org/api_docs/python/tf/contrib/legacy_seq2seq/basic_rnn_seq2seq


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import tensorflow as tf
import numpy as np

steps=10
batch_size=10
input_size=10

encoder_inputs = tf.placeholder("float", [None, steps, input_size])
decoder_inputs = tf.placeholder("float", [None, steps, input_size])

en_input=np.zeros(shape=[steps,batch_size,input_size])
de_input=np.zeros(shape=[steps,batch_size,input_size])

cell=tf.nn.rnn_cell.BasicLSTMCell(10)

def get_result(encoder_inputs,decoder_inputs,cell):
    encoder_inputs=tf.unstack(encoder_inputs,axis=1)
    decoder_inputs=tf.unstack(decoder_inputs,axis=1)
    result=tf.contrib.legacy_seq2seq.basic_rnn_seq2seq(
        encoder_inputs,
        decoder_inputs,
        cell,
        dtype=tf.float32,
        scope=None
    )
    return result
result=get_result(encoder_inputs,decoder_inputs,cell)

init=tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    result_value=sess.run(result,feed_dict={encoder_inputs:en_input,decoder_inputs:de_input})
    print(result_value)

Be the first to comment

Leave a Reply

Your email address will not be published.