Code Monkey home page Code Monkey logo

Comments (5)

vierja avatar vierja commented on August 19, 2024

@Shuolongbj you mean changing the docs or change sonnet in order to behave the same way for both examples? The latter I'm not sure how to do, any insight is much appreciated.

from sonnet.

vierja avatar vierja commented on August 19, 2024

@tfgg any feedback on this?

from sonnet.

malcolmreynolds avatar malcolmreynolds commented on August 19, 2024

@vierja The reason for the discrepancy is that _enter_variable_scope vs defining submodules in _build only unifies the variable_scopes entered, meaning that the variables have the same name whichever way round you do it. However, every time you enter the variable_scope you get a new name scope which will be uniquified if necessary by adding _N. The Tensorboard graph visualises namescopes, rather than variable scopes per se, although there is obviously overlap.

In your second example, the variables (and the ops associated with them - read, assign, init etc) are inside the first time you enter name_scope, which is main_module at the bottom. When you call _build, you reenter a name_scope with the same name and TF uniquifies it to main_module_1, and you see the add and matmul inside the smaller block up top. If you connect the module a second time you would get another small block, main_module_2, with again just a Matmul and Add inside. Similarly, if you connect the module a second as you have it written in the first module, you also get a second block as the name_scope is entered a second time.

I don't think it's possible to change this name_scope behaviour without some hacking of TF, and I don't think that would necessarily be desirable - when connecting modules multiple times you probably want that to show as separate name_scopes in Tensorboard. Otherwise, a module shared lots of times would end up looking extremely confusing as all the input / output connections go through one block.

I hope this explanation helps - please let me know if you have other comments or questions.

from sonnet.

fastturtle avatar fastturtle commented on August 19, 2024

Thanks for bringing this to our attention. The issue here is that _enter_variable_scope() enters a tf.variable_scope(), which creates a name scope. This ultimately increments the counter the tf.Graph object uses to uniquify name scopes. The result is that if you use _enter_variable_scope() to define variables inside __init__, then when _build() is called for the first time the name scope has "_1" appended to it.

This is common behavior in TensorFlow and occurs when variables are created before they are used, for example:

with tf.variable_scope("scope"):
  v = tf.get_variable("v", shape=[])
  
with tf.variable_scope("scope", reuse=True):
  # We have now entered the name_scope "scope_1"
  x = v * 2

image

It sounds like you want to enter a variable scope without entering a name scope. I would advise against this, because it can lead to even weirder graphs (what happens if you accidentally create an Op?) and isn't possible with public TensorFlow functions. Did you have a different solution in mind?

from sonnet.

vierja avatar vierja commented on August 19, 2024

@malcolmreynolds and @fastturtle, thank you guys for your explanations and for making me understand why it wouldn't be necessarily an undesirable behavior. I can't think of a different solution and mostly it seems that this is the desired visualization. I appreciate your time.

from sonnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.