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
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:
The solution is to import the module
random
and use the functionrandom.choices(range(len(images),k=2)
instead ofnp.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 st2=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!