tf.add_to_collection(name,value)

add_to_collection(name,value)
Stores value in the collection with the given name.
Note that collections are not sets, so it is possible to add a value to a collection several times.
Args:
name: The key for the collection. The GraphKeys class contains many standard names for collections.
value: The value to add to the collection.
  • tf.add_to_collection(‘list_name’, element):将元素element添加到列表list_name中
  • tf.get_collection(‘list_name’):返回名称为list_name的列表
  • tf.add_n(list):将列表元素相加并返回
  • import tensorflow as tf
    tf.add_to_collection('losses', tf.constant(2.2))
    tf.add_to_collection('losses', tf.constant(3.))
    with tf.Session() as sess:
        print(sess.run(tf.get_collection('losses')))
        print(sess.run(tf.add_n(tf.get_collection('losses'))
    结果:
    [2.2, 3.0] 
    5.2
    注意: 
    使用tf.add_n对列表元素进行相加时,列表内元素类型必须一致,否则会报错。
    

     

     

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值