gpt4 book ai didi

python - 在 Tensorflow 中使用迭代器时如何正确设置 is_training

转载 作者:行者123 更新时间:2023-11-30 09:18:36 24 4
gpt4 key购买 nike

我有一个 is_training 变量,我仍然需要在我的 main.py 文件中定义它,如下所示:

is_training = tf.placeholder(tf.bool, name='is_training')

然后我从另一个文件调用我的 inference 方法,如下所示(x 是输入):

test = net.inference(x, is_training)

最后,在我的 session 中我这样做:

sess.run(test, feed_dict={x: test_x, is_training: True})

但是,我想将 is_training 放入我的推理函数中。这有可能吗?

最佳答案

如果您仅在推理中需要 is_training,我建议您 tf.placeholder_with_default 。这样你就可以只在 net.inference() 方法中定义它,并且在 session 中传递:

self.is_training = tf.placeholder_with_default(False, shape=(),name='is_training')

当您必须将其更改为True时,您可以简单地执行以下操作:

sess.run(test, feed_dict={x: test_x, net.is_training: True})

请注意,tensorflow 不关心 python 变量或字段的范围。定义占位符后,它就会出现在图表中。

关于python - 在 Tensorflow 中使用迭代器时如何正确设置 is_training,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48686528/

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