【问题标题】:Tensorflow seed not working with LSTM modelTensorflow 种子不适用于 LSTM 模型
【发布时间】:2017-05-03 07:40:47
【问题描述】:

tf.set_random_seed() 不起作用,并且找不到 opt 种子。
对于 LSTM 中的许多参数,在 tf.nn.rnn_cell.BasicLSTMCell 中似乎没有找到 opt 种子。因此,每次它产生不同的结果。如何设置种子以产生相同的运行多次结果?

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 '---------------------------------------'

【问题讨论】:

  • 您找到解决方案了吗?提前致谢

标签: tensorflow


【解决方案1】:

我相信这应该在tensorflow nightly builds 中“按预期”工作。请尝试使用 TF 每晚构建并报告:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-10
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2021-04-18
    • 2022-12-03
    相关资源
    最近更新 更多