What is different between tf.group and tensorflow collection?

Viewed 1717

I'm new in tensorflow

There are two ways we can run many tensor at once. use collection and tf.gruop but i'm not sure with two difference

For simple example

const0=tf.constant(8)
const1=tf.constant(11)

tf.add_to_collection('my_collection' , const0)
tf.add_to_collection('my_collection' , const1)

#or 
tf.group(const0 , const1)

The code below is Mandelbrot fractal implemented by tensorflow There was no difference btw the one executed using tf.group and collection

#step = tf.group(
#  tf.assign(zs, zs_add),
#  tf.assign_add(ns, zs_cast)
#)

#tf.add_to_collection('my_collection',tf.assign(zs, zs_add))
#tf.add_to_collection('my_collection',(ns, zs_cast))
#step = tf.get_collection('my_collection')
#

import tensorflow as tf
import numpy as np 
import matplotlib.pyplot as plt

# Concept
# input initial value
#Y, X = np.mgrid[-1.3:1.3:0.005, -2:1:0.005]
Y, X = np.mgrid[-1.3:1.3:0.005, -2:1:0.005]

init_value = X + 1j*Y

#
xs = tf.constant(init_value )
zs = tf.Variable(xs)
zs_zeros = tf.zeros_like(xs, tf.float32)
ns = tf.Variable(zs_zeros)

zs_squre = tf.multiply(zs,zs)
zs_add = tf.add(zs_squre , xs)
zs_abs = tf.abs(zs_add)

zs_less = tf.math.less(zs_abs , 4)
zs_cast = tf.cast(zs_less , tf.float32)

#
step = tf.group(
  tf.assign(zs, zs_add),
  tf.assign_add(ns, zs_cast)
)


#
sess = tf.Session()
sess.run(tf.global_variables_initializer())

for i in range(200):
    sess.run(step)
value = sess.run(ns)
plt.imshow(value)
plt.show()

SO THIS IS MY QUESTION
what is difference with tf.group and collection? thank you!

1 Answers

tf.group creates an operation inside the computational graph that once evaluated executes all the tensors in the group:

op = tf.group(a,b)

tf.add_to_collection instead, creates a group of operations not inside the computational graph, but only in the python script.

tf.add_to_collection('coll', a)
tf.add_to_collection('coll', b)

You can see this by looking at the description of op and `tf.get_collection('coll'):

  • op: <tf.Operation 'group_deps' type=NoOp>
  • tf.get_collection: [<tf.Tensor 'Const:0' shape=() dtype=int32>, <tf.Tensor 'Const_1:0' shape=() dtype=int32>]

In your example, using tf.group or tf.add_to_collection + tf.get_collection is the same: you just need all the operations executed in parallel, hence sess.run(op) and sess.run(tf.get_collection('coll')) have the same behaviour.

But in the case of the export of a computational graph (that's just an example to make you understand a possible scenario), you can't rely upon a python list, hence you have to use tf.group

Related