2

Using names = [n.name for n in graph.as_graph_def().node] I can get all the node names in the graph.

For instance, say this prints:

['model/classifier/dense/kernel/Initializer/random_uniform/shape',
'model/classifier/dense/kernel/Initializer/random_uniform/min',
'model/classifier/dense/kernel/Initializer/random_uniform/max',
'model/classifier/dense/kernel/Initializer/random_uniform/RandomUniform',
'model/classifier/dense/kernel/Initializer/random_uniform/sub',
'model/classifier/dense/kernel/Initializer/random_uniform/mul',
'model/classifier/dense/kernel/Initializer/random_uniform',
'model/classifier/dense/kernel',
'model/classifier/dense/kernel/Assign',
'model/classifier/dense/kernel/read',
'model/classifier/dense/bias/Initializer/zeros/shape_as_tensor',
'model/classifier/dense/bias/Initializer/zeros/Const',
'model/classifier/dense/bias/Initializer/zeros',
'model/classifier/dense/bias',
'model/classifier/dense/bias/Assign',
'model/classifier/dense/bias/read',
'model/classifier/dense/MatMul',
'model/classifier/dense/BiasAdd'] 

How can I select only the operations or only the tensors?

I'm aware of the following workarounds, which will work in specific situations but are not general enough and would not scale to a large graph:

  • string manipulations from the above names

    • for instance to get model/classifier/dense/kernel:

      tensor = [graph.get_tensor_by_name(n + ":0") 
          for n in names if 'classifier' in n and
          'kernel' in name and 
          not n.split('kernel')[-1]
      ][0]
      
    • As you can imagine this may be very error prone and is very tensor specific
  • try/except I could get the tensors which are outputs of these operation with:

    tensors = []
    for name in names:
        try:
            tensors.append(graph.get_tensor_by_name(name + ":0"))
        except KeyError:
            pass
    
    • But again this is but a workaround and does not solve the selection problem: what if I just want the kernel Tensor?
ted
  • 13,596
  • 9
  • 65
  • 107

3 Answers3

1

if you want a better feel of operations and nodes, try running tensorboard. You can write summary files with tf.summary.FileWriter("folder_name", sess.graph).

My tensorflow knowledge is limited but I think that tensor names and operator names are almost the same. An operator can have multiple outputs, and each of these outputs is called a tensor. The tensor name is therefore just operator_name:output_index, the output_index is often 0, since most operators have a single output. So give running sess.graph.get_tensor_by_name("model/classifier/dense/kernel/Initializer/random_uniform/mul:0") a chance. I'm not sure if having such long names is practical though.

Sorry if the provided information is not 100% correct, I'm just a beginner.

ted
  • 13,596
  • 9
  • 65
  • 107
T. Kelher
  • 1,188
  • 10
  • 9
  • You are right, it partially answers my question (therefore upvoted but not accepted). But my issue is also that some may have no output at all, such as an initializing op. The workaround would be to try/except KeyError but it sounds like something better should exist. – ted Jun 15 '18 at 23:26
1

Ok I found the answer. It lied mainly in that what I was really looking for are Variables not just regular Tensors.

Therefore it's as easy as:

with graph.as_default():
    kernel = [v for v in tf.global_variables()
        if 'optimization' not in v.name and
        'classifier' in v.name
        and 'kernel' in v.name
    ][0]
piet.t
  • 11,718
  • 21
  • 43
  • 52
ted
  • 13,596
  • 9
  • 65
  • 107
-1

You can use the isinstance(item, class) and compare the nodes with the tf.Operation class like so [n.name for n in graph.as_graph_def().node if isinstance(n, tf.Operation)]

bountrisv
  • 55
  • 1
  • 7
  • 1
    No it does not work, returns empty list as nodes won't be either (`tf.Operation` nor `tf.Tensor`) – ted Jun 15 '18 at 16:06
  • can you check their \_\_class\_\_? – bountrisv Jun 15 '18 at 16:58
  • 1
    Sorry for misunderstanding your question, maybe this can be of help: https://stackoverflow.com/questions/42168977/tensorflow-manual-construction-of-graphdef – bountrisv Jun 15 '18 at 17:23