tf.add_to_collection(name, value)  用来把一个value放入名称是‘name’的集合,组成一个列表;

tf.get_collection(key, scope=None) 用来获取一个名称是‘key’的集合中的所有元素,返回的是一个列表,列表的顺序是按照变量放入集合中的先后;   scope参数可选,表示的是名称空间(名称域),如果指定,就返回名称域中所有放入‘key’的变量的列表,不指定则返回所有变量。

tf.add_n(inputs, name=None), 把所有 ‘inputs’列表中的所有变量值相加,name可选,是操作的名称。

## coding: utf-8 ##
import tensorflow as tf v1 = tf.get_variable(name='v1', shape=[1], initializer=tf.constant_initializer(1))
tf.add_to_collection('output', v1) # 把变量v1放入‘output’集合中
v2 = tf.get_variable(name='v2', shape=[1], initializer=tf.constant_initializer(2))
tf.add_to_collection('output', v2)
v3 = tf.get_variable(name='v3', shape=[1], initializer=tf.constant_initializer(3))
tf.add_to_collection('output',v3) with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print tf.get_collection('output') # 获取'output'列表内容
print sess.run(tf.add_n(tf.get_collection('output'))) # tf.add_n把列表中所有内容一次性相加 # print:
# [<tf.Variable 'v1:0' shape=(1,) dtype=float32_ref>, <tf.Variable 'v2:0' shape=(1,) dtype=float32_ref>, <tf.Variable 'v3:0' shape=(1,) dtype=float32_ref>]
# [ 6.]

tensorflow中 tf.add_to_collection、 tf.get_collection 和 tf.add_n函数的更多相关文章

  1. TensorFlow函数(九)tf.add_to_collection()、tf.get_collection() 和 tf.add_n()

    tf.add_to_collection(name, value) 此函数将元素添加到列表中 参数: name:列表名.如果不存在,创建一个新的列表 value:元素 tf.get_collectio ...

  2. tf.add_to_collection,tf.get_collection简介

    tf.add_to_collection:把变量放入一个集合,把很多变量变成一个列表 tf.get_collection:从一个结合中取出全部变量,是一个列表 tf.add_n:把一个列表的东西都依次 ...

  3. 【tf.keras】tf.keras使用tensorflow中定义的optimizer

    Update:2019/09/21 使用 tf.keras 时,请使用 tf.keras.optimizers 里面的优化器,不要使用 tf.train 里面的优化器,不然学习率衰减会出现问题. 使用 ...

  4. [转载]tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定

    tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置: config = tf.ConfigProto(allow_soft_placement=True ...

  5. tensorflow中 tf.train.slice_input_producer 和 tf.train.batch 函数(转)

    tensorflow数据读取机制 tensorflow中为了充分利用GPU,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算. 具体来说就是使用一个线程源源不断的将硬盘中的图片数 ...

  6. TensorFlow 中的 tf.train.exponential_decay() 指数衰减法

    exponential_decay(learning_rate, global_step, decay_steps, decay_rate, staircase=False, name=None) 使 ...

  7. Tensorflow中的tf.argmax()函数

    转载请注明出处:http://www.cnblogs.com/willnote/p/6758953.html 官方API定义 tf.argmax(input, axis=None, name=None ...

  8. tensorflow中共享变量 tf.get_variable 和命名空间 tf.variable_scope

    tensorflow中有很多需要变量共享的场合,比如在多个GPU上训练网络时网络参数和训练数据就需要共享. tf通过 tf.get_variable() 可以建立或者获取一个共享的变量. tf.get ...

  9. tensorflow中协调器 tf.train.Coordinator 和入队线程启动器 tf.train.start_queue_runners

    TensorFlow的Session对象是支持多线程的,可以在同一个会话(Session)中创建多个线程,并行执行.在Session中的所有线程都必须能被同步终止,异常必须能被正确捕获并报告,会话终止 ...

随机推荐

  1. python 元组切片

    #create a tuple tuplex = (, , , , , , , , , ) #used tuple[start:stop] the start index is inclusive a ...

  2. ros 编译指定包

    一.编译到devel目录在工作空间下(有devel文件夹的那个目录)执行 catkin_make --pkg 包名 如果报错,执行catkin_make -DCATKIN_WHITELIST_PACK ...

  3. Flex布局兼容知识点总结

    转载,原文http://www.cnblogs.com/tugenhua0707/p/5180841.html,部分截取 假设父容器class为 box,子项目为item.旧版语法如下:一:定义容器的 ...

  4. 团队项目用户验收评审——《WAP团队》

    团队项目用户验收评审——<WAP团队> 1.验收准备的相关文档链接:https://github.com/LVowe999/xiangmubaogao.git                ...

  5. Java JDK5新特性-可变参数

    2017-10-31 00:19:07 可变参数:定义方法的时候不知道该定义多少个参数 格式:修饰符 返回值类型 方法名(数据类型... 变量名){} 注意:这里的变量其实是一个数组          ...

  6. English trip V1 - 辅导课 VOCABULARY BRUSH UP(1-6) 词汇刷新 SA:Winona

    1.How Do you Feel Now?            形容词  adj.  = adjective                     Describe people and thi ...

  7. mysql查询出来的某一列合并成一个字段

    查询所有用户及角色,一个用户会有多个角色 select u.*,sr.* from users u left join sys_user_role sur on sur.userCode=u.id l ...

  8. PHP函数总结 (七)

    <?php /** * 匿名函数(闭包函数): * php>=5.3 * 允许临时创建一个没有指定名称的函数,常作为回调函数参数的值 * * 闭包的另一个概念: * 在内部函数中可以使用外 ...

  9. CentOS7 install apache

    1. yum install httpd 2. config /etc/httpd/conf/httpd.conf <VirtualHost *:80>  ServerName www.l ...

  10. 检测Linux glibc幽灵漏洞和修补漏洞

    1.首先安装rpm : sudo apt-get install rpm   wget -OGHOST-test.sh http://www.antian365.com/lab/linux0day/G ...