Tensorflow种子不适用于LSTM模型

时间:2017-04-23 04:59:23

标签: tensorflow

tf.set_random_seed()无效,未找到选择种子。
对于LSTM中的许多参数,似乎在tf.nn.rnn_cell.BasicLSTMCell中找不到opt seed。因此,每次产生不同的结果。如何设置种子以产生相同的运行次数?

import numpy as np
import tensorflow as tf
from tensorflow.python.ops import rnn, rnn_cell 

if __name__ == '__main__':

np.random.seed(1234)


X = np.array(np.array(range(1,121)).reshape(4, 6, 5), dtype = float)

x0 = tf.placeholder(tf.float32, [4, 6, 5])
x = tf.reshape(x0, [-1, 5])
x = tf.split(0, 4, x)


with tf.variable_scope('lstm') as scope:

    lstm = tf.nn.rnn_cell.BasicLSTMCell(5, state_is_tuple = True)   

    outputs, states = tf.nn.rnn(lstm, x, dtype = tf.float32)

    scope.reuse_variables()

    outputs2, states2 = tf.nn.dynamic_rnn(lstm, x0, dtype=tf.float32,time_major = True)

    outputs3, states3 = tf.nn.rnn(lstm, x, dtype=tf.float32)

print(outputs3)   

with tf.Session() as sess:              

    tf.set_random_seed(1)

    init = tf.initialize_all_variables()
    sess.run(init)

    for var in tf.trainable_variables():
        print var.name 

    for i in range(3):
        result1, result2, result3 = sess.run([outputs, outputs2, outputs3], feed_dict = {x0: X})

        print result1
        print '---------------------------------------'

        print result2
        print '---------------------------------------'

        print result3
        print '---------------------------------------'

1 个答案:

答案 0 :(得分:0)

我认为这应该有效"正如所料"在tensorflow nightly builds。请尝试使用TF每晚构建并报告回来:

哦,也可以在创建任何操作之前调用tf.set_random_seed