## Check if node is an operation or a tensor in Tensorflow Graph

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?

- But again this is but a workaround and does not solve the selection problem: what if I just want the

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.

**tf.is_tensor,** x : A python object to check. Returns: True if x is a tensor or "tensor-like", False if not A tensor can be originated from the input data or the result of a computation. In TensorFlow, all the operations are conducted inside a graph. The graph is a set of computation that takes place successively. Each operation is called an op node and are connected to each other. The graph outlines the ops and connections between the nodes.

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]

**tf.Graph,** test. Overview · assert_equal_graph_def · compute_gradient Return true_fn() if the predicate pred is true else false_fn() . WARNING: Any Tensors or Operations created outside of true_fn and some additional graph nodes to ensure that the right branch gets executed depending on the value of pred . An Operation is a node in a tf.Graph that takes zero or more Tensor objects as input, and produces zero or more Tensor objects as output. Objects of type Operation are created by calling a Python op constructor (such as tf.matmul) within a tf.function or under a tf.Graph.as_default context manager.

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)]`

**tf.Operation,** TensorFlow programs work by first building a graph of tf. Each element in the Tensor has the same data type, and the data type is always known. Most operations produce tensors of fully-known shapes if the shapes of their inputs are also fully known, but in some cases it's To determine the rank of a tf. Each node in the graph is called op (short for operation). So we'll have one node for each operation; either for operations on tensors (like math operations) or generating tensors (like variables and constants). Each node takes zero or more tensors as inputs and produces a tensor as an output.

**tf.cond,** Tensors and operations The central unit of data in TensorFlow.js is the tf. rank : defines how many dimensions the tensor contains; shape : which defines the size of each dimension of the data Tensor s element-wise:. Returns a symbolic handle to one of the tensors produced by this operation. Warning: Does not check that the type of the tensor matches T. It is recommended to call this method with an explicit type parameter rather than letting it be inferred, e.g. operation.<Integer>output(0)

**TensorFlow tensors,** TensorBoard's Graphs dashboard is a powerful tool for examining your the Keras model definition, with extra edges to other computation nodes. This may be useful if you're reusing a saved model and you want to examine or validate its A Graph contains a set of tf.Operation objects, which represent units of computation; and tf.Tensor objects, which represent the units of data that flow between operations. A default Graph is always registered, and accessible by calling tf.compat.v1.get_default_graph . To add an operation to the default graph,

**Tensors and operations,** For an official introduction to the Tensorflow concepts of Graph() and Session() , check out the official introduction on tensorflow.org. building the computational graph, the nodes and operations and how they are connected to each other Implementation for an Operation added as a node to a Graph . GraphOperation instances are valid only as long as the Graph they are a part of is valid. Thus, if close() has been invoked, then methods on the GraphOperation instance may fail with an IllegalStateException . GraphOperation instances are immutable and thread-safe.