So I am trying to continuously update a tensor in my code in a loop by assigning it with a new value in each iteration. genRandMat function assigns the variable a1 with a random MxN matrix comprising 0 and 1 with frequency of 1 being decided with probability pt.
Here is the code I ran -
np.random.seed(0)
tf.set_random_seed(0)
def genRandMat(M,N,pt):
return tf.convert_to_tensor(np.random.choice([0, 1], size=(M,N), p=[1-pt, pt]), dtype=tf.float32)
a1=tf.Variable(genRandMat(1,10,0.5))
a2=a1.assign(genRandMat(1,10,0.5))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(5):
print(a1.eval())
sess.run(a2)
print(a1.eval())
print("*************")
The result I expected was a new random tensor after every second print statement (due to update statement a2), ie 2nd, 4th, 6th... matrices should be updated, new random matrices.
Here is what I got instead
[[0. 0. 1. 1. 0. 0. 1. 1. 1. 0.]]
[[1. 1. 1. 1. 0. 1. 0. 0. 1. 1.]]
*************
[[1. 1. 1. 1. 0. 1. 0. 0. 1. 1.]]
[[1. 1. 1. 1. 0. 1. 0. 0. 1. 1.]]
*************
[[1. 1. 1. 1. 0. 1. 0. 0. 1. 1.]]
[[1. 1. 1. 1. 0. 1. 0. 0. 1. 1.]]
*************
[[1. 1. 1. 1. 0. 1. 0. 0. 1. 1.]]
[[1. 1. 1. 1. 0. 1. 0. 0. 1. 1.]]
*************
[[1. 1. 1. 1. 0. 1. 0. 0. 1. 1.]]
[[1. 1. 1. 1. 0. 1. 0. 0. 1. 1.]]
*************
As you can see, the value of a1 changes once at the start with the 2nd print statement and then not anymore. I tried commenting out both random seeds but the result doesn't change. I want a new matrix after every update statement. How can I achieve this?
Aucun commentaire:
Enregistrer un commentaire