zoukankan      html  css  js  c++  java
  • tf.name_scope tf.variable_scope学习

    1. 首先看看比较简单的 tf.name_scope(‘scope_name’).

    tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理。

    '''
    Signature: tf.name_scope(*args, **kwds)
    Docstring:
    Returns a context manager for use when defining a Python op.
    '''
    # 也就是说,它的主要目的是为了更加方便地管理参数命名。
    # 与 tf.Variable() 结合使用。简化了命名
    with tf.name_scope('conv1') as scope:
        weights1 = tf.Variable([1.0, 2.0], name='weights')
        bias1 = tf.Variable([0.3], name='bias')
    
    # 下面是在另外一个命名空间来定义变量的
    with tf.name_scope('conv2') as scope:
        weights2 = tf.Variable([4.0, 2.0], name='weights')
        bias2 = tf.Variable([0.33], name='bias')
    
    # 所以,实际上weights1 和 weights2 这两个引用名指向了不同的空间,不会冲突
    print (weights1.name) 
    print (weights2.name)


    #输出:
    conv1/weights:0
    conv2/weights:0
    

      

    # 注意,这里的 with 和 python 中其他的 with 是不一样的
    # 执行完 with 里边的语句之后,这个 conv1/ 和 conv2/ 空间还是在内存中的。这时候如果再次执行上面的代码
    # 就会再生成其他命名空间
    with tf.name_scope('conv1') as scope:
        weights1 = tf.Variable([1.0, 2.0], name='weights')
        bias1 = tf.Variable([0.3], name='bias')
    
    with tf.name_scope('conv2') as scope:
        weights2 = tf.Variable([4.0, 2.0], name='weights')
        bias2 = tf.Variable([0.33], name='bias')
    
    print (weights1.name)
    print (weights2.name)
    #输出
    conv1_1/weights:0
    conv2_1/weights:0

    2.下面来看看 tf.variable_scope(‘scope_name’)

    tf.variable_scope() 主要结合 tf.get_variable() 来使用,实现 变量共享。

    # 这里是正确的打开方式~~~可以看出,name 参数才是对象的唯一标识
    import tensorflow as tf
    with tf.variable_scope('v_scope') as scope1:
        Weights1 = tf.get_variable('Weights', shape=[2,3])
        bias1 = tf.get_variable('bias', shape=[3])
    
    # 下面来共享上面已经定义好的变量
    # note: 在下面的 scope 中的变量必须已经定义过了,才能设置 reuse=True,否则会报错
    with tf.variable_scope('v_scope', reuse=True) as scope2:
        Weights2 = tf.get_variable('Weights')
    
    print (Weights1.name)
    print (Weights2.name)
    # 可以看到这两个引用名称指向的是同一个内存对象
    #输出
    v_scope/Weights:0
    v_scope/Weights:0

    也可以结合 tf.Variable() 一块使用。

    import tensorflow as tf
    # 注意, bias1 的定义方式
    with tf.variable_scope('v_scope') as scope1:
        Weights1 = tf.get_variable('Weights', shape=[2,3])
    #     bias1 = tf.Variable([0.52], name='bias')
    
    # 下面来共享上面已经定义好的变量
    # note: 在下面的 scope 中的get_variable()变量必须已经定义过了,才能设置 reuse=True,否则会报错
    with tf.variable_scope('v_scope', reuse=True) as scope2:
        Weights2 = tf.get_variable('Weights')
        bias2 = tf.Variable([0.52], name='bias')
    
    print (Weights1.name)
    print (Weights2.name)
    print (bias2.name)
    #输出:
    v_scope/Weights:0 v_scope/Weights:0 v_scope_1/bias:0#新的命名空间

    如果 reuse=True 的scope中的变量没有已经定义,会报错!!

    import tensorflow as tf
    # 注意, bias1 的定义方式
    with tf.variable_scope('v_scope') as scope1:
        Weights1 = tf.get_variable('Weights', shape=[2,3])
        bias1 = tf.Variable([0.52], name='bias')
    
    print (Weights1.name)
    print (bias1.name)
    
    # 下面来共享上面已经定义好的变量
    # note: 在下面的 scope 中的get_variable()变量必须已经定义过了,才能设置 reuse=True,否则会报错
    with tf.variable_scope('v_scope', reuse=True) as scope2:
        Weights2 = tf.get_variable('Weights')
        bias2 = tf.get_variable('bias', [1])  # ‘bias未定义,所以会报错
    
    print (Weights2.name)
    print (bias2.name)
    
    # 这样子的话就会报错
    # Variable v_scope/bias does not exist, or was not created with tf.get_variable()
    #输出
    v_scope/Weights:0
    v_scope/bias:0

    本文代码:https://github.com/yongyehuang/Tensorflow-Tutorial

    转载https://www.cnblogs.com/adong7639/p/8136273.html

  • 相关阅读:
    第四章 基础命令的学习
    7-7命令总结
    第三章 虚拟机的简单使用及其xshell远程工具的使用
    第二章 虚拟机的软件及其安装
    在VMware中安装CentOS系统步骤
    Pytest02-用法和调用
    Pytest01-从官方文档开始学习
    浅谈mock技术
    C/C++ malloc、calloc、realloc和alloca
    Go 知识汇总
  • 原文地址:https://www.cnblogs.com/gaofighting/p/9626584.html
Copyright © 2011-2022 走看看