Skip to content
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

use tf.debugging for search space assertions #299

Merged
merged 6 commits into from
Jul 19, 2021

Conversation

joelberkeley
Copy link
Contributor

@joelberkeley joelberkeley commented Jul 15, 2021

Use tf.debugging for assertions in space.py, so that error handling works in tf.function.

Also changed an implementation while I was at it


return space
tf.debugging.assert_positive(other, message="Exponent must be strictly positive")
return reduce(operator.mul, [self] * other)
Copy link
Contributor Author

@joelberkeley joelberkeley Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

felt like doing this, can undo if you'd prefer

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely more elegant!
am I missing something or assert_positive means >0 while here we want >=1?

Copy link
Contributor Author

@joelberkeley joelberkeley Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right. maybe i though positive meant strictly positive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scrap that ... > 0 is the same as >=1 for integers, and that's also what assert_positive does

trieste/space.py Outdated
Comment on lines 229 to 237
tf.debugging.assert_equal(
shapes_equal(value, self._lower),
True,
message="value must have same dimensionality as search space"
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit of a hack, to get around the fact that tf.debugging.assert_shapes considers 0 and [0] to have the same shape

@joelberkeley joelberkeley marked this pull request as ready for review July 15, 2021 23:16
Copy link
Collaborator

@hstojic hstojic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we should use tf.debugging asserts throughout the code, rather than plain asserts (and ifelse statements with raising errors/warnings/etc)?

@joelberkeley
Copy link
Contributor Author

so we should use tf.debugging asserts throughout the code, rather than plain asserts (and ifelse statements with raising errors/warnings/etc)?

tf.debugging works as expected with tf.function. raise doesn't (it will only raise when the function is compiled). assert is only used in source code for internal correctness checks. I don't believe trieste uses assert outside tests (assert is fine in tests, if you're not using tf.function)

trieste/space.py Outdated Show resolved Hide resolved
@hstojic
Copy link
Collaborator

hstojic commented Jul 19, 2021

so we should use tf.debugging asserts throughout the code, rather than plain asserts (and ifelse statements with raising errors/warnings/etc)?

tf.debugging works as expected with tf.function. raise doesn't (it will only raise when the function is compiled). assert is only used in source code for internal correctness checks. I don't believe trieste uses assert outside tests (assert is fine in tests, if you're not using tf.function)

I see, I didn't know about the difference with respect to compilation.
So assert has the same issue if tf.function is used?
Actually, I have noticed we are dominantly using if-else accompanied with raising an error, while to me it usually seems like it should be an assert statement?

Regarding tf.function, at the moment it is used only in model optimization, is that correct?

@joelberkeley
Copy link
Contributor Author

joelberkeley commented Jul 19, 2021

I see, I didn't know about the difference with respect to compilation.
So assert has the same issue if tf.function is used?

yes

Actually, I have noticed we are dominantly using if-else accompanied with raising an error, while to me it usually seems like it should be an assert statement?

I don't think assert is meant to be used for argument validation

Regarding tf.function, at the moment it is used only in model optimization, is that correct?

internally, yes, but users can wrap anything they like in tf.function

@hstojic
Copy link
Collaborator

hstojic commented Jul 19, 2021

Actually, I have noticed we are dominantly using if-else accompanied with raising an error, while to me it usually seems like it should be an assert statement?

I don't think assert is meant to be used for argument validation

Why not? It seems to me that plenty of people use it that way, e.g. https://wiki.python.org/moin/UsingAssertionsEffectively

Regarding tf.function, at the moment it is used only in model optimization, is that correct?

internally, yes, but users can wrap anything they like in tf.function

true... but then that would be an argument for using tf.debugging throughout the code

Copy link
Collaborator

@hstojic hstojic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regardless of the discussion above, content wise this is good to go in my opinion

@hstojic
Copy link
Collaborator

hstojic commented Jul 19, 2021

a thought on tf.function and users passing any piece of our code through it - it would be good to test that (all of) our code can be used within tf.function without errors. Some python code can cause errors within tf.function, we had that issue in Bellman.

@joelberkeley
Copy link
Contributor Author

a thought on tf.function and users passing any piece of our code through it - it would be good to test that (all of) our code can be used within tf.function without errors. Some python code can cause errors within tf.function, we had that issue in Bellman.

this crossed my mind before. I guess I never thought it high priority. Not sure why, since trieste has already had problems with tf.function

re asserts and exceptions, this Q&A might be of interest

@joelberkeley
Copy link
Contributor Author

btw i don't have merge permissions. feel free to merge

@hstojic hstojic merged commit b35555d into secondmind-labs:develop Jul 19, 2021
@joelberkeley joelberkeley deleted the space-errors branch July 19, 2021 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants