如何通过tensor名称来查询对应变量的具体值呢?

2026-04-20 05:080阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计437个文字,预计阅读时间需要2分钟。

如何通过tensor名称来查询对应变量的具体值呢?

需求:有时使用slim这种封装好的工具,或在做滑动平均时,系统会帮你自动建立一些变量,但这些变量只有名字,没有明显的变量名,所以我们需要使用那个名字。

需求:

有时候使用slim这种封装好的工具,或者是在做滑动平均时,系统会帮你自动建立一些变量,但是这些变量只有名字,而没有显式的变量名,所以这个时候我们需要使用那个名字来获取其对应的值。

如下:

input = np.random.randn(4,3) net = slim.fully_connected(input,2,weights_initializer=tf.ones_initializer(dtype = tf.float32))

这段代码看似简单,但其实帮你生成了一个w和一个b。如果你运行下面代码:

with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for v in tf.global_variables(): print(v)

你会发现里面还有

<tf.Variable 'fully_connected/weights:0' shape=(3, 2) dtype=float64_ref> <tf.Variable 'fully_connected/biases:0' shape=(2,) dtype=float64_ref>

这样两个变量,但是由于没有显式声明,所以我们要从其名字取值。

阅读全文

本文共计437个文字,预计阅读时间需要2分钟。

如何通过tensor名称来查询对应变量的具体值呢?

需求:有时使用slim这种封装好的工具,或在做滑动平均时,系统会帮你自动建立一些变量,但这些变量只有名字,没有明显的变量名,所以我们需要使用那个名字。

需求:

有时候使用slim这种封装好的工具,或者是在做滑动平均时,系统会帮你自动建立一些变量,但是这些变量只有名字,而没有显式的变量名,所以这个时候我们需要使用那个名字来获取其对应的值。

如下:

input = np.random.randn(4,3) net = slim.fully_connected(input,2,weights_initializer=tf.ones_initializer(dtype = tf.float32))

这段代码看似简单,但其实帮你生成了一个w和一个b。如果你运行下面代码:

with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for v in tf.global_variables(): print(v)

你会发现里面还有

<tf.Variable 'fully_connected/weights:0' shape=(3, 2) dtype=float64_ref> <tf.Variable 'fully_connected/biases:0' shape=(2,) dtype=float64_ref>

这样两个变量,但是由于没有显式声明,所以我们要从其名字取值。

阅读全文