前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tf.get_collection()

tf.get_collection()

作者头像
狼啸风云
修改2022-09-02 20:42:03
8460
修改2022-09-02 20:42:03
举报

此函数有两个参数,key和scope。

Args:

  • 1.key: The key for the collection. For example, the GraphKeys class contains many standard names for collections.
  • 2.scope: (Optional.) If supplied, the resulting list is filtered to include only items whose name attribute matches using re.match. Items without a name attribute are never returned if a scope is supplied and the choice or re.match means that a scope without special tokens filters by prefix.

举个例子:

代码语言:javascript
复制
# 在'My-TensorFlow-tutorials-master/02 CIFAR10/cifar10.py'代码中

  variables = tf.get_collection(tf.GraphKeys.VARIABLES)
  for i in variables:
  print(i)

>>>   <tf.Variable 'conv1/weights:0' shape=(3, 3, 3, 96) dtype=float32_ref>
      <tf.Variable 'conv1/biases:0' shape=(96,) dtype=float32_ref>
      <tf.Variable 'conv2/weights:0' shape=(3, 3, 96, 64) dtype=float32_ref>
      <tf.Variable 'conv2/biases:0' shape=(64,) dtype=float32_ref>
      <tf.Variable 'local3/weights:0' shape=(16384, 384) dtype=float32_ref>
      <tf.Variable 'local3/biases:0' shape=(384,) dtype=float32_ref>
      <tf.Variable 'local4/weights:0' shape=(384, 192) dtype=float32_ref>
      <tf.Variable 'local4/biases:0' shape=(192,) dtype=float32_ref>
      <tf.Variable 'softmax_linear/softmax_linear:0' shape=(192, 10) dtype=float32_ref>
      <tf.Variable 'softmax_linear/biases:0' shape=(10,) dtype=float32_ref>

tf.get_collection会列出key里所有的值。

进一步地:

tf.GraphKeys的点后可以跟很多类, 比如VARIABLES类(包含所有variables), 比如REGULARIZATION_LOSSES。

具体tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)的使用:

代码语言:javascript
复制
def easier_network(x, reg):
  """ A network based on tf.contrib.learn, with input `x`. """
  with tf.variable_scope('EasyNet'):
     out = layers.flatten(x)
     out = layers.fully_connected(out,
                                  num_outputs=200,
                                  weights_initializer = layers.xavier_initializer(uniform=True),
                                  weights_regularizer = layers.l2_regularizer(scale=reg),
                                  activation_fn = tf.nn.tanh)
     out = layers.fully_connected(out,
                                  num_outputs=200,
                                  weights_initializer = layers.xavier_initializer(uniform=True),
                                  weights_regularizer = layers.l2_regularizer(scale=reg),
                                  activation_fn = tf.nn.tanh)
     out = layers.fully_connected(out,
                                  num_outputs=10, # Because there are ten digits!
                                  weights_initializer = layers.xavier_initializer(uniform=True),
                                  weights_regularizer = layers.l2_regularizer(scale=reg),
                                  activation_fn = None)
     return out

 def main(_):
  mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
  x = tf.placeholder(tf.float32, [None, 784])
  y_ = tf.placeholder(tf.float32, [None, 10])

  # Make a network with regularization
  y_conv = easier_network(x, FLAGS.regu)
  weights = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'EasyNet')
  print("")
  for w in weights:
     shp = w.get_shape().as_list()
     print("- {} shape:{} size:{}".format(w.name, shp, np.prod(shp)))
     print("")
     reg_ws = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES, 'EasyNet')
  for w in reg_ws:
     shp = w.get_shape().as_list()
     print("- {} shape:{} size:{}".format(w.name, shp, np.prod(shp)))
     print("")

  # Make the loss function `loss_fn` with regularization.
  cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
  loss_fn = cross_entropy + tf.reduce_sum(reg_ws)
  train_step = tf.train.AdamOptimizer(1e-4).minimize(loss_fn)

main()

>>>   - EasyNet/fully_connected/weights:0 shape:[784, 200] size:156800
      - EasyNet/fully_connected/biases:0 shape:[200] size:200
      - EasyNet/fully_connected_1/weights:0 shape:[200, 200] size:40000
      - EasyNet/fully_connected_1/biases:0 shape:[200] size:200
      - EasyNet/fully_connected_2/weights:0 shape:[200, 10] size:2000
      - EasyNet/fully_connected_2/biases:0 shape:[10] size:10

      - EasyNet/fully_connected/kernel/Regularizer/l2_regularizer:0 shape:[] size:1.0
      - EasyNet/fully_connected_1/kernel/Regularizer/l2_regularizer:0 shape:[] size:1.0
      - EasyNet/fully_connected_2/kernel/Regularizer/l2_regularizer:0 shape:[] size:1.0

据:

代码语言:javascript
复制
for w in reg_ws:
     shp = ....

这段代码的输出可知, 在图上的所有regularization都会集中保存到tf.GraphKeys.REGULARIZATION_LOSSES去。

关于collection的详情请参见: http://blog.csdn.net/shenxiaolu1984/article/details/52815641

关于tf.GraphKeys.REGULARIZATION_LOSSES的详情参见: https://gxnotes.com/article/178205.html

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-10-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 进一步地:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档