20.03.2021 Views

Deep-Learning-with-PyTorch

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Running the training script

307

>>> for i, _ in enumerateWithEstimate(list(range(234)), "sleeping"):

... time.sleep(random.random())

...

11:12:41,892 WARNING sleeping ----/234, starting

11:12:44,542 WARNING sleeping 4/234, done at 2020-01-01 11:15:16, 0:02:35

11:12:46,599 WARNING sleeping 8/234, done at 2020-01-01 11:14:59, 0:02:17

11:12:49,534 WARNING sleeping 16/234, done at 2020-01-01 11:14:33, 0:01:51

11:12:58,219 WARNING sleeping 32/234, done at 2020-01-01 11:14:41, 0:01:59

11:13:15,216 WARNING sleeping 64/234, done at 2020-01-01 11:14:43, 0:02:01

11:13:44,233 WARNING sleeping 128/234, done at 2020-01-01 11:14:35, 0:01:53

11:14:40,083 WARNING sleeping ----/234, done at 2020-01-01 11:14:40

>>>

That’s 8 lines of output for over 200 iterations lasting about 2 minutes. Even given the

wide variance of random.random(), the function had a pretty decent estimate after 16

iterations (in less than 10 seconds). For loop bodies with more constant timing, the

estimates stabilize even more quickly.

In terms of behavior, enumerateWithEstimate is almost identical to the standard

enumerate (the differences are things like the fact that our function returns a generator,

whereas enumerate returns a specialized <enumerate object at 0x…>).

Listing 11.20

util.py:143, def enumerateWithEstimate

def enumerateWithEstimate(

iter,

desc_str,

start_ndx=0,

print_ndx=4,

backoff=None,

iter_len=None,

):

for (current_ndx, item) in enumerate(iter):

yield (current_ndx, item)

However, the side effects (logging, specifically) are what make the function interesting.

Rather than get lost in the weeds trying to cover every detail of the implementation,

if you’re interested, you can consult the function docstring (https://github

.com/deep-learning-with-pytorch/dlwpt-code/blob/master/util/util.py#L143) to get

information about the function parameters and desk-check the implementation.

Deep learning projects can be very time intensive. Knowing when something is

expected to finish means you can use your time until then wisely, and it can also clue

you in that something isn’t working properly (or an approach is unworkable) if the

expected time to completion is much larger than expected.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!