Reputation: 130
I'm reading the codes about a TF official example about cifar10 on https://github.com/tensorflow/models/blob/master/official/resnet/cifar10_main.py and I have some questions:
input_fn
, what does num_images = is_training and _NUM_IMAGES['train'] or _NUM_IMAGES['validation']
... mean? How can we get the right size of data while training and validating through this function?
main
, there's a similar oneinput_function = FLAGS.use_synthetic_data and get_synth_input_fn() or input_fn
Again, I don't know how it works.
Upvotes: 1
Views: 119
Reputation: 372
num_images = is_training and _NUM_IMAGES['train'] or _NUM_IMAGES['validation']
is equivalent to
if is_training:
num_images = _NUM_IMAGES['train']
else:
num_images = _NUM_IMAGES['validation']
In the same vein:
input_function = FLAGS.use_synthetic_data and get_synth_input_fn() or input_fn
is equivalent to:
if FLAGS.use_synthetic_data:
input_function = get_synth_input_fn()
else:
input_function = input_fn()
While my given more verbose variants may be more readable, the original tensorflow version is more compact.
The and
operator short circuits, e.g in
(A and B)
B
is only evaluated if A
is true.
This means that in:
A and B or C
If A
is true, then B is evaluated and or
never gets to evaluate C
,
so the result is B
. If A
is false, then B
is never evaluated and the result is C
.
For more information study the docs:
https://docs.python.org/2/library/stdtypes.html#boolean-operations-and-or-not
Upvotes: 1
Reputation: 77484
You are the victim of the bad code style of TensorFlow in this case. The tutorial is written by using a particular Python anti-trick in which you use and
to select the final object of two objects that evaluate to True
in a Boolean context, and you use or
to select the final object in the case when the first object evaluates to False
.
You can see it more easily with some simpler examples:
In [9]: 3 and "hello"
Out[9]: 'hello'
In [10]: 3 and False or "foobar"
Out[10]: 'foobar'
So these lines are selecting the necessary function or data size by chaining together these two anti-tricks.
This makes for needless obfuscated and unreadable code, and the minute someone defends it as OK just because the language supports it or "it's Pythonic" you can mostly just stop trusting their advice.
Upvotes: 0
Reputation: 53768
This is a clever application of boolean operations with integers (or actually any objects) in python (see this question for more details). Example:
>>> True and 10 or 20
Out[11]: 10
>>> False and 10 or 20
Out[12]: 20
>>> a = False and (lambda x: x) or (lambda x: 2*x)
>>> a(1)
Out[14]: 2
So the result of num_images
is an integer (first or second one depending on is_training
), the result of input_function
is a function (again, the first or the second one depending on the flag use_synthetic_data
).
Upvotes: 0