gpt4 book ai didi

python - 使用张量设置特定种子?

转载 作者:太空宇宙 更新时间:2023-11-03 10:49:58 25 4
gpt4 key购买 nike

我想构建其中包含一些随机值的图形。然后我想用不同的种子评估图表。这是我要实现的示例:

import tensorflow as tf
seed = tf.placeholder(dtype=tf.int32, shape=(), name="seed")
randoms = tf.random_normal(shape=[8], seed=seed)

然后我希望做这样的事情,前两个调用将返回相同的结果:

sess = tf.InteractiveSession()
sess.run(randoms, {seed: 1})
sess.run(randoms, {seed: 1})
sess.run(randoms, {seed: 2})

有什么办法可以解决这个问题吗?

最佳答案

这可以通过 https://www.tensorflow.org/api_docs/python/tf/random/stateless_normal 来完成

例子:

import tensorflow as tf

seed = tf.placeholder(dtype=tf.int32, shape=[2], name="seed")
randoms = tf.random.stateless_normal(shape=[3], seed=seed)

sess = tf.InteractiveSession()
print(sess.run(randoms, {seed: [1, 0]}))
print(sess.run(randoms, {seed: [1, 0]}))
print(sess.run(randoms, {seed: [2, 0]}))
[ 0.1266503  -0.49301657  0.6311907 ]
[ 0.1266503 -0.49301657 0.6311907 ]
[-0.6394294 -0.18700573 -0.82845527]

但是,文档中的警告注释:

The output is consistent across multiple runs on the same hardware (and between CPU and GPU), but may change between versions of TensorFlow or on non-CPU/GPU hardware.

关于python - 使用张量设置特定种子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52663313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com