How do I generate a random vector in TensorFlow and maintain it for further use?

前端 未结 2 905
感情败类
感情败类 2021-02-20 14:40

I am trying to generate a random variable and use it twice. However, when I use it the second time, the generator creates a second random variable that is not identical to the f

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-20 15:00

    Your question has the same issue as this question, in that if you call random_uniform twice you will get two results, and as such you need to set your second variable to the value of the first. That means that, assuming you are not changing rand_var_1 later, you can do this:

    rand_var_1 = tf.random_uniform([5],0,10, dtype = tf.int32, seed = 0)
    rand_var_2 = rand_var_1
    

    But, that said, if you want z1 and z2 to be equal, why have separate variables at all? Why not do:

    import numpy as np
    import tensorflow as tf
    
    # A random variable
    rand_var = tf.random_uniform([5],0,10, dtype = tf.int32, seed = 0)
    op = tf.add(rand_var,rand_var)
    
    init = tf.initialize_all_variables()
    
    with tf.Session() as sess:
        sess.run(init)
        z1_op = sess.run(op)
        z2_op = sess.run(op)
        print(z1_op,z2_op)
    

提交回复
热议问题