0

As mentioned in this question, in Tensorflow v1 you could retrieve a tensor or operation output via its Graph within a session:

with tf.Session() as sess:
    # ...
    tf.get_default_graph().get_tensor_by_name("example:0")

In Tensorflow 2 direct use of the graph is deprecated, and thus not the preferred approach.

The get_tensor_by_name method still exists, but to get the implicit graph you would have to call something like tf.compat.v1.get_default_graph(), which essentially is a way of making TF1 code work in TF2.

Is there some canonical way in Tensorflow 2 of retrieving a tensor which exists in the implicit computation graph, but which you do not have any reference to except for by name?

M.T
  • 4,917
  • 4
  • 33
  • 52
  • Name based variable access is highly discouraged in TF2 and most likely there is another way to achieve what you want. Are you looking to retrieve a tensor which is a layer weight or input traveling through the network? Are you using the `tf.keras` API? – bluesummers Aug 25 '20 at 08:28
  • @bluesummers Yes, I am using the tf.keras API. Specifically, I would like to access a tensor defined within the optimizer for logging purposes without having to edit the optimizer code. – M.T Aug 25 '20 at 08:30
  • Great, then we are one step closer to solution; every optimizer has a `.get_weights()` method - is that what you are looking for? Since we are not accessing by name, the specifics of what you are trying to access is critical to find a solution, so if you can share what exactly you are looking for I might be able to help – bluesummers Aug 25 '20 at 08:46
  • 1
    Also, if the `.get_weights()` is not what you are looking for, try `.get_slots_names()` followed by `.get_slot()` where you can access other variables that the optimizer uses. A third way is using `.variables()`... Essentially you have access to anything, it depends on what you are trying to achieve – bluesummers Aug 25 '20 at 08:54

0 Answers0