天天看点

tf.add_to_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对列表元素进行相加时,列表内元素类型必须一致,否则会报错。
           

参考:

https://www.jianshu.com/p/6612f368e8f4

继续阅读