gpt4 book ai didi

python - 你如何使用 TensorFlow Graphkeys 获取所有权重?

转载 作者:太空狗 更新时间:2023-10-30 02:08:07 25 4
gpt4 key购买 nike

Tensorflow 定义了集合的预设,如下所示:https://www.tensorflow.org/versions/r0.12/api_docs/python/framework/graph_collections

我目前正在使用 tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) 获取所有变量[*已命名;如果不存在,则不会显示,即使它们存在]。

同样,我希望 tf.get_collection(tf.GraphKeys.WEIGHTS) 输出一个权重列表,但它是一个空数组。这也适用于 GraphKeys.BIASES.ACTIVATIONS

这是怎么回事?

在我看来,这里似乎有两种可能。首先,这些实际上从未自动定义,只是推荐的集合名称。其次,我的网络非常糟糕,但似乎并非如此。

谁有这方面的经验?

最佳答案

默认情况下,所有变量都绑定(bind)到 tf.GraphKeys.GLOBAL_VARIABLES 集合。方便的方法是将每个权重设置为集合 tf.GraphKeys.WEIGHTS,如下所示:

In [2]: w = tf.Variable([1,2,3], collections=[tf.GraphKeys.WEIGHTS], dtype=tf.float32)
In [3]: w2 = tf.Variable([11,22,32], collections=[tf.GraphKeys.WEIGHTS], dtype=tf.float32)

然后您可以通过以下方式获取它们:

tf.get_collection_ref(tf.GraphKeys.WEIGHTS)

这是权重:

[<tf.Variable 'Variable:0' shape=(3,) dtype=float32_ref>,
<tf.Variable 'Variable_1:0' shape=(3,) dtype=float32_ref>]

关于python - 你如何使用 TensorFlow Graphkeys 获取所有权重?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45256627/

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