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