mardi 19 janvier 2016

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

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 which is not identical to the first. Here is some code to demonstrate what I mean:

import numpy as np
import tensorflow as tf

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

#Op1
z1 = tf.add(rand_var_1,rand_var_2)

#Op2
z2 = tf.add(rand_var_1,rand_var_2)

init = tf.initialize_all_variables()

with tf.Session() as sess:
    sess.run(init)
    z1_op = sess.run(z1)
    z2_op = sess.run(z2)
    print(z1_op,z2_op)

I just want z1_op and z2_op to be equal. I think this is because the random_uniform op gets called twice. Is there a way to use TensorFlow (without using NumPy) to achieve this?

(My use case is more complicated, but I suppose this is the distilled question).




Aucun commentaire:

Enregistrer un commentaire