ageron / handson-ml

⛔️ DEPRECATED – See https://github.com/ageron/handson-ml3 instead.
Apache License 2.0
25.18k stars 12.92k forks source link

Chapter11_exercise_10: slow code. #539

Open AbdullahDawud opened 4 years ago

AbdullahDawud commented 4 years ago

The generate_batch code in the solution provided is causing the learning process to be slower than usual.

The culprit I found to be the code:

rnd_idx1, rnd_idx2 = np.random.randint(0, len(images), 2)

The solution is to import the module random and use the function random.choices(range(len(images),k=2) instead of np.random.randint(0, len(images), 2).

I compared the performance of each with the following codes and here are the results:

t1= timeit.timeit("n=np.random.randint(0,100,2)", setup="import numpy as np", number=100000) # 5.364107837999654 s

t2=timeit.timeit("n=random.choices(range(len(arr)), k=2)", setup="import random;arr=range(100)",number=100000)` # 0.4697397750005621 s

[print(t) for t in [t1,t2]]

That's more than 10x faster!