tensorflow中 tf.add_to_collection、 tf.get_collection 和 tf.add_n函数
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函数的更多相关文章
- TensorFlow函数(九)tf.add_to_collection()、tf.get_collection() 和 tf.add_n()
tf.add_to_collection(name, value) 此函数将元素添加到列表中 参数: name:列表名.如果不存在,创建一个新的列表 value:元素 tf.get_collectio ...
- tf.add_to_collection,tf.get_collection简介
tf.add_to_collection:把变量放入一个集合,把很多变量变成一个列表 tf.get_collection:从一个结合中取出全部变量,是一个列表 tf.add_n:把一个列表的东西都依次 ...
- 【tf.keras】tf.keras使用tensorflow中定义的optimizer
Update:2019/09/21 使用 tf.keras 时,请使用 tf.keras.optimizers 里面的优化器,不要使用 tf.train 里面的优化器,不然学习率衰减会出现问题. 使用 ...
- [转载]tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定
tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置: config = tf.ConfigProto(allow_soft_placement=True ...
- tensorflow中 tf.train.slice_input_producer 和 tf.train.batch 函数(转)
tensorflow数据读取机制 tensorflow中为了充分利用GPU,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算. 具体来说就是使用一个线程源源不断的将硬盘中的图片数 ...
- TensorFlow 中的 tf.train.exponential_decay() 指数衰减法
exponential_decay(learning_rate, global_step, decay_steps, decay_rate, staircase=False, name=None) 使 ...
- Tensorflow中的tf.argmax()函数
转载请注明出处:http://www.cnblogs.com/willnote/p/6758953.html 官方API定义 tf.argmax(input, axis=None, name=None ...
- tensorflow中共享变量 tf.get_variable 和命名空间 tf.variable_scope
tensorflow中有很多需要变量共享的场合,比如在多个GPU上训练网络时网络参数和训练数据就需要共享. tf通过 tf.get_variable() 可以建立或者获取一个共享的变量. tf.get ...
- tensorflow中协调器 tf.train.Coordinator 和入队线程启动器 tf.train.start_queue_runners
TensorFlow的Session对象是支持多线程的,可以在同一个会话(Session)中创建多个线程,并行执行.在Session中的所有线程都必须能被同步终止,异常必须能被正确捕获并报告,会话终止 ...
随机推荐
- hdu 5668 Circle 中国剩余定理
Circle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- HTML元素1: 基本元素,标题,段落,链接,图像等
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- windows 2012 R2安装SqlServer2016提示缺少KB2919355
补丁的安装顺序如下: 1, 首先安装 Windows2012R2更新的先决条件KB2919442.2,按照如下顺序安装后续KB文件.顺序:clearcompressionflag.exe.KB2919 ...
- OpenGL入门程序一:绘制简单的矩形
#include <GL/glut.h> void MyDisplay(void); int main(int argc, char **argv) { //设置窗口的大小 glutIni ...
- jquery添加类
一.addClass() 方法向被选元素添加一个或多个类. 1.对P元素添加一个intro类.<!DOCTYPE html><html><head lang=" ...
- ASP.NET的内置对象 —— Response 对象
在 ASP.NET 中,页面实际上是一个输出 HTML 的可执行对象. 传统的 ASP 程序通常将 ASP 代码和 HTML 标签集成在一个文件中,或者将其分离于多个文 件. 当执行文件时,服务器回从 ...
- redis中的"HashMap"
redis是一个存储键值对的内存数据库,其存储键值的方式和java中的HashMap相似. 表征redis数据库的结构体是redisDb (在server.h文件中),redis服务器默认有16个数据 ...
- ThreadPoolExecutor类
首先分析内部类:ThreadPoolExecutor$Worker //Worker对线程和任务做了一个封装,同时它又实现了Runnable接口, //所以Worker类的线程跑的是自身的run方法 ...
- 在Ubuntu系统下设置永久性Swap交换空间(转帖)
http://blog.sina.com.cn/s/blog_6e4388910100tsk7.html swap 一般都在一个专有的swap区里,这可能是因为在安装的时候系统会提示你创建一个swap ...
- kernel build command
Uboot: make ARCH=arm CROSS_COMPILE=${CC} distclean make ARCH=arm CROSS_COMPILE=${CC} am335x_evm_defc ...