0

I have a TensorFlow Keras model (TensorFlow 2.6.0); here's a basic example:

import tensorflow as tf

x = inp = tf.keras.Input((5,))
x = tf.keras.layers.Dense(7, activation="relu")(x)
x = tf.keras.layers.Dense(1)(x)
model = tf.keras.Model(inp, x)

I would like to get all the tf.Operation objects in the graph for the model, select specific operations, then create a new tf.function or tf.keras.Model to output the values of those tensors on arbitrary inputs.

For example, in my simple model above, I might want to get the outputs of all relu operators. I know in that case, I could redefine the model to include the output of that layer as another output of the model, but the point here is that I already have the model (it's much more complicated than above), and there are specific operators that I want to find to get the outputs of.

hunse
  • 3,175
  • 20
  • 25
  • I have had some success with `model.make_predict_function()._concrete_stateful_fn.graph.get_operations()`. The problem is that the resulting tensors are now in the context of that function, and I don't know whether they can be used to create a new function that will output the values of those tensors for an arbitrary input. – hunse Oct 08 '21 at 16:41

1 Answers1

0

Have you tried this:

all_ops = tf.get_default_graph().get_operations()

If you got an empty list and you use Tensorflow 2.x , you may try this:

import tensorflow as tf
print(tf.__version__)
tf.compat.v1.disable_eager_execution() # disable eager execution

a = tf.constant([1],name='aa')
print(tf.compat.v1.get_default_graph().get_operations())
print(tf.compat.v1.get_default_graph().get_tensor_by_name('aa:0'))
abdou_dev
  • 805
  • 1
  • 10
  • 28
  • Running `tf.compat.v1.get_default_graph().get_operations()` returns an empty list for me. Evidently, there is nothing in the model creation that makes any internal graphs the "default" graph. – hunse Oct 08 '21 at 16:29
  • I edit my answer – abdou_dev Oct 08 '21 at 16:32
  • I would like to avoid turning off eager execution if possible, since it will make changes throughout my whole program. If there's not another way to do it, though, I may turn to it as a last resort. – hunse Oct 08 '21 at 16:36
  • Also, see my specific use-case as described in my original question. First of all, I'm using a `tf.keras.Model`, not just some stand-alone TensorFlow commands. Second, I need to be able to use the output tensors of the operations to create a new function that will give me those values for arbitrary inputs. – hunse Oct 08 '21 at 16:46
  • But you need first to get operations, for that reason you should divide your problem to two problems , you should solve the first , I mean you should get the ops of the graphs – abdou_dev Oct 08 '21 at 16:49
  • But are all graphs amenable to being used in the way I want, to get those operation output tensor values? I've found another way to get a graph, but I haven't been able to use it to get tensor output values: `model.make_predict_function()._concrete_stateful_fn.graph.get_operations()`. In your example, how would I use that graph to get the output of one of the operations for a given input? – hunse Oct 08 '21 at 16:59
  • Did you see this link? https://stackoverflow.com/questions/36883949/in-tensorflow-get-the-names-of-all-the-tensors-in-a-graph – abdou_dev Oct 08 '21 at 17:07
  • The other problem with using the default graph like this is that it appears that all `tf.keras.Model` objects that get created get added to the default graph. My code may have multiple models in it, which will make it hard to separate out the parts I want related to a specific model. – hunse Oct 08 '21 at 17:33