tf.GraphKeys

在计算图中(默认计算图或者自定义计算图)可以通过tf.add_to_collection()函数将资源加入一个或多个集合中,然后通过tf.get_collection()函数获取一个集合里面的所有资源。

import tensorflow as tf

g = tf.Graph()
with g.as_default():
    v1 = tf.Variable(initial_value=tf.zeros(shape=[2,3], dtype=tf.float32),trainable=False,name='v_g')
    collection_g = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
    for coll in collection_g:
        print(coll)
        print(coll is v1)
with g.device('/gpu:0'):
    with tf.Session(graph=g) as sess:
        sess.run(tf.global_variables_initializer())
        print(sess.run(v1))

##################################

f = tf.get_default_graph()
with f.as_default():
    v1 = tf.Variable(initial_value=tf.ones(shape=[1], dtype=tf.int32),trainable=False,name='v1')
    print(v1)
with tf.Session(graph = f) as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(v1))  # 输出: [1]
with f.as_default():
    collections = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES)
    for coll in collections:
        print(coll)
        print(coll is v1)

Standard names to use for graph collections.tf.GraphKeys中的集合名称

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值