-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement easier access to and manipulation of var inputs #202
Conversation
Discussion Notes
|
With the current update I added the following changes:
This means we now have these possibilities: Accessing inputsAccess named value inputs to a calculatora = Var(2.0, name="a")
y = Var(Calc(lambda x: x + 1.0, x=a))
y.value_node["x"] Access positional value inputs to a calculatora = Var(2.0, name="a")
y = Var(Calc(lambda x: x + 1.0, a))
y.value_node[0] Access named value inputs to a dista = Var(2.0, name="a")
y = Var(1.0, Dist(tfp.distributions.Normal, loc=a, scale=1.0))
y.dist_node["loc"] Access positional value inputs to a dista = Var(2.0, name="a")
y = Var(1.0, Dist(tfp.distributions.Normal, a, scale=1.0))
y.dist_node[0] Swapping out inputsSwap out inputs to a calculatorNote:
a = Var(2.0, name="a")
y = Var(Calc(lambda x: x + 1.0, x=a))
b = Var(3.0, name="b")
y.value_node["x"] = b Swap out inputs to a dista = Var(2.0, name="a")
y = Var(1.0, Dist(tfp.distributions.Normal, loc=a, scale=1.0))
b = Var(3.0, name="b")
y.dist_node["loc"] = b What would be left to doIf this PR moves forward, this is left to do:
|
Cases to cover in tests:
|
What happens when using
|
@jobrachem, should this already be reviewed or discussed next week? |
@wiep I need to add documentation before this can be reviewed. Also, the test actions currently time out, which is not nice for the review process. The timeout seems to be unrelated to our code changes, but the amount of additional time that the tests seem to use is quite drastic. |
See #213 regarding the tests timing out. |
@wiep @GianmarcoCallegher the merge conflicts are resolved and the tests for circular graph behavior updated :) Ready for review! |
@wiep @GianmarcoCallegher gentle reminder :) Would be meaningful for me to have this soon, since I want to use it in the current semester in teaching. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Just a little question
It's ready to be merged for me |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ready to be merged. Just two comments. One one wording in documentation and the second on more expressive names for tests. Feel free to ignore them if you disagree.
This PR contains only a few lines of changed code, but I think it can drastically improve quality of life while building and manipulating Liesel models.
Problem statement
I have found the work with variable inputs to be often quite cumbersome. I'll show what I mean in examples.
Example 1: Accessing parameters of a distribution
Let's say I want to access the
loc
andscale
of a variable's distribution, starting from the variable.The
Var.dist_node
'skwinputs
attribute does not give me that access. Instead, it returns a dict ofVarValue
nodes. That is node helpful.To actually get to the
loc
var, I have to call:I think this is unnecessarily cumbersome. I have to actually know a lot about Liesel's internals and/or dig in the source code to find what I need. Every time I need to perform this action, I have to look up what to do and extensively test my code in order to be sure that I get it right. It is quite annoying.
Example 2: Accessing inputs of a calculator
A very similar pattern holds when you use calculators:
Proposed Solution
I implemented
Node.__getitem__
andVar.__getitem__
as a remedy.The above tasks can now be solved like this:
Some details
Node.__getitem__
.a) If it receives an integer, it will essentially look up the searched item in
Node.all_input_nodes()
. This will find all inputs, including positional and keyword inputs.b) If it receives a string, it will essentially look up the searched item in
Node.kwinputs
. This will of course find only inputs that are actually keyword inputs.Var.__getitem__
will defer to its value node. To access inputs to the distribution, users can turn toVar.dist_node.__getitem__
.Setitem
The implementation also provides the possibility to replace inputs via
Node.__setitem__
. Example:The same works for
Dist
. The implementation is a thin quality-of-life wrapper around the existingNode.set_inputs()
method.Documentation
This is a draft PR for a first discussion. Even if it remains unchanged, documentation has to be added if it ends up being merged.