113、TensorFlow变量集合

#一个tensorflow程序断开的部分可能要创建变量
# 如果有一种方法来访问所有的变量是非常有用的
#因为这个原因TensorFlow提供了集合,是一些张量的集合
#或者是其他的对象,就像tf.Variable 实例一样
# 默认情况下 tf.Variable 对象被放置在下面的两个集合中
# tf.GraphKeys.GLOBAL_VARIABLES
#变量可以在多个设备之间被分享
# tf.GraphKeys.TRAINABLE_VARIABLES 
# TensorFlow会自动对上面集中的变量进行计算,列如自动求导
# 如果你不想让变量被自动训练
# 可以将它添加到 tf.GraphKeys.LOCAL_VARIABLES集合中
# 下面的代码块解释了如何将my_local变量添加到这个集合中
import tensorflow as tf

my_local = tf.get_variable("my_local", shape=(), collections=[tf.GraphKeys.LOCAL_VARIABLES])
# Alternatively you can specify trainable=False as an argument to tf.get_variable
my_non_trainable = tf.get_variable("my_non_trainable", shape=(), trainable=False)
#You can also use your own collections, any string is a valid collection name
tf.add_to_collection("my_collection_name",my_local)
#And to retrieve a list of all the variables (or other objects) you've placed in a collection you can use
tf.get_collection("my_collection_name")

 

posted @ 2018-02-17 10:51  香港胖仔  阅读(138)  评论(0)    收藏  举报