使用numpy随机函数时,TensorFlow返回相同的值
我使用的是带有numpy随机函数的Tensorflow,但输出值是相同的。如何生成不同的值?您可能建议使用本机tf随机函数,但我需要使用numpy随机函数使用numpy随机函数时,TensorFlow返回相同的值,numpy,tensorflow,random,Numpy,Tensorflow,Random,我使用的是带有numpy随机函数的Tensorflow,但输出值是相同的。如何生成不同的值?您可能建议使用本机tf随机函数,但我需要使用numpy随机函数 import tensorflow as tf import random def get_rand(): return random.randint(0,5) a = get_rand() tfprint = tf.Print(a, [a]) for i in range(10): print(print(get_r
import tensorflow as tf
import random
def get_rand():
return random.randint(0,5)
a = get_rand()
tfprint = tf.Print(a, [a])
for i in range(10):
print(print(get_rand()))
with tf.Session() as sess:
for i in range(10):
sess.run(tfprint)
使用tf.py_func,将Numpy函数转换为Tensorflow函数
import tensorflow as tf
import random
def get_rand():
return random.randint(0,5)
a = tf.py_func(get_rand, [], tf.int64)
tfprint = tf.Print(a, [a])
for i in range(10):
print(get_rand())
with tf.Session() as sess:
for i in range(10):
sess.run(tfprint)
您需要使用占位符和feed_dict变量提供数据:
import tensorflow as tf
import random
def get_rand():
return random.randint(0,5)
a = tf.placeholder(tf.int32)
tfprint = tf.Print(a, [a])
with tf.Session() as sess:
for i in range(10):
sess.run(tfprint, feed_dict={a: get_rand()})
您可以阅读更多有关占位符的内容谢谢您的回答,但在我的问题中,我无法使用提要。是什么阻止您使用占位符?我最初的问题比给出示例更复杂。这是关于数据扩充的。我刚刚运行了你的代码,它打印了10个不同的随机数。问题是什么?Numpy部分确实会生成随机数,但Tensorflow部分不会。