从collection中取op

本文介绍了使用TensorFlow进行变量定义及操作,并演示了如何通过名称空间管理变量以及如何使用集合来筛选特定名称的变量。这有助于理解TensorFlow中变量的作用域与管理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#coding=utf-8

import tensorflow as tf
with tf.name_scope("hello") as name_scope:
    arr1 = tf.get_variable("arr1", shape=[2,10],dtype=tf.float32)
    a = tf.Variable(0,name='a')

arr2 = tf.get_variable('arr2', shape=[1,2], dtype=tf.float32)

collection = tf.get_collection(tf.GraphKeys().GLOBAL_VARIABLES,'a')#选取集合中名称开头为'a'的op

print(*collection)


结果:

<tf.Variable 'arr1:0' shape=(2, 10) dtype=float32_ref> <tf.Variable 'arr2:0' shape=(1, 2) dtype=float32_ref>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值