【Tensorflow练习】: tf.variable_scope、tf.get_variable、tf.get_variable_scope、arg_scope的简单使用示例

本文介绍了TensorFlow中变量管理的关键概念,包括`tf.variable_scope`, `tf.get_variable`, `tf.get_variable_scope`以及`arg_scope`的使用。通过示例展示了如何创建、重用和安全地管理变量,以及`arg_scope`在控制变量共享方面的作用。" 8806931,1403176,Apache Tez:优化Hadoop处理的DAG框架,"['Hadoop生态', '大数据处理', 'MapReduce优化', 'DAG框架', 'Apache组件']
摘要由CSDN通过智能技术生成

首先导入库:

#coding=utf-8
#如果出现编码上的问题,可能需要先写上面这行,保存一下文件,再写下面的部分
import tensorflow  as tf

创建新变量的一个实例:

#对应的参数(name_or_scope, default_name, values=None, ...,reuse=None,...,auxiliary_name_scope=True)
#其中,default_name: name_or_scope若为None,则用它,否则它将无用
with tf.variable_scope("foo"):
    with tf.variable_scope("bar"):
        v = tf.get_variable("v", [1])  #获取具有此名称"v"的现有变量(如果已有)或创建一个新变量(还没有该名称的变量); 参数(name, shape, dtype, initializer, ...)
        print(v.name)  #output: foo/bar/v:0
        s = tf.get_variable_scope()  #返回当前变量scopereuse=None
        print(s.name)  #output: foo/bar
        assert v.name == "foo/bar/v:0" 

若name_or_scope=None, 且default_name='fcy':(default_name: name_or_scope若为None,则用它,否则它将无用)

#若name_or_scope=None, 且default_name='fcy':
with tf.variable_scope(name_or_scope=None, default_name='fcy'):
    with tf.variable_scope("bar"):
        v = tf.get_variable("v", [1])  
        print(v.name)  #output: fcy/bar/v:0
        s = tf.get_variable_scope()  #返回当前变量scopereuse=None
        print(s.name)  #output: fcy/bar
        assert v.name == "fcy/bar/v:0"  
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>