iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python人工智能tensorflow函数tf.get_collection使用方法
  • 283
分享到

python人工智能tensorflow函数tf.get_collection使用方法

2024-04-02 19:04:59 283人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录参数数量及其作用例子参数数量及其作用 该函数共有两个参数,分别是key和scope。 def get_collection(key, scope=None) Wrapper

参数数量及其作用

该函数共有两个参数,分别是key和scope。

 def get_collection(key, scope=None)
 Wrapper for Graph.get_collection() using the default graph.
 See tf.Graph.get_collection for more details.
 Args:
   key: The key for the collection. For example, the `GraphKeys` class  
     contains many standard names for collections.  
   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.  
 Returns:
   The list of values in the collection with the given `name`, or  
   an empty list if no value has been added to that collection. The  
   list contains the values in the order under which they were  
   collected.  

该函数的作用是从一个collection中取出全部变量,形成列个列表,key参数中输入的是collection的名称。

该函数常常与tf.get_variable和tf.add_to_collection配合使用。

例子

该例子将分别举例tf.get_collection与tf.get_variable和tf.add_to_collection的配合使用方法。

 import Tensorflow as tf;  
 import numpy as np;  
 c1 = ['c1', tf.GraphKeys.GLOBAL_VARIABLES]
 v1 = tf.get_variable('v1', [1], initializer=tf.constant_initializer(1),collections=c1)
 v2 = tf.get_variable('v2', [1], initializer=tf.constant_initializer(2))
 tf.add_to_collection('c2', v2)
 with tf.Session() as sess:
     sess.run(tf.global_variables_initializer())
     print(tf.get_collection('c1'))
     print(tf.get_collection('c2'))

其输出为:

 [<tf.Variable 'v1:0' shape=(1,) dtype=float32_ref>]
 [<tf.Variable 'v2:0' shape=(1,) dtype=float32_ref>]

tf.get_variable的用法可以参照我的另一篇博文:

python人工智能tensorflow函数tf.get_variable使用方法

以上就是Python人工智能tensorflow函数tf.get_collection使用方法的详细内容,更多关于tensorflow函数tf.get_collection的资料请关注编程网其它相关文章!

--结束END--

本文标题: python人工智能tensorflow函数tf.get_collection使用方法

本文链接: https://www.lsjlt.com/news/117633.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作