4

The answer here says that one returns an operation while the other returns a tensor. That is pretty obvious from the name and from the documentation. However, suppose I do the following:

logits = tf.add(tf.matmul(inputs, weights), biases, name='logits')

I am following the pattern described in Tensorflow Mechanics 101. Should I restore it as an operation or as a tensor? I am afraid that if I restore it as a tensor I will only get the last computed values for the logits; nonetheless, the post here, seems to suggest that there is no difference or that I should just use get_tensor_by_name. The idea is to compute the logits for a new set of inputs and then make predictions accordingly.

Maxim
  • 52,561
  • 27
  • 155
  • 209
srcolinas
  • 497
  • 5
  • 13

1 Answers1

5

Short answer: you can use both, get_operation_by_name() and get_tensor_by_name(). Long answer:

tf.Operation

When you call

op = graph.get_operation_by_name('logits')

... it returns an instance of type tf.Operation, which is a node in the computational graph, which performs some op on its inputs and produces one or more outputs. In this case, it's a plus op.

One can always evaluate an op in a session, and if this op needs some placehoder values to be fed in, the engine will force you to provide them. Some ops, e.g. reading a variable, don't have any dependencies and can be executed without placeholders.

In your case, (I assume) logits are computed from the input placeholder x, so logits doesn't have any value without a particular x.

tf.Tensor

On the other hand, calling

tensor = graph.get_tensor_by_name('logits:0')

... returns an object tensor, which has the type tf.Tensor:

Represents one of the outputs of an Operation.

A Tensor is a symbolic handle to one of the outputs of an Operation. It does not hold the values of that operation's output, but instead provides a means of computing those values in a TensorFlow tf.Session.

So, in other words, tensor evaluation is the same as operation execution, and all the restrictions described above apply as well.

Why is Tensor useful? A Tensor can be passed as an input to another Operation, thus forming the graph. But in your case, you can assume that both entities mean the same.

Maxim
  • 52,561
  • 27
  • 155
  • 209