Explain algorithm, code for training time

it is super wierd that start= time.time()

time.time() minus time.time() =0?

but the result do not equal 0 why?

import time

start = time.time()

model.train() # Training statement

print("Total time: ", time.time() - start, "seconds")

This code is wrong,

Hi @shangguan91, the code is correct.
Calling model.train() doesn’t train your model, it just sets your model in training mode, so virtually no cost in time.
For more information, please refer to here.

time.time() will return your current system time in each call. Since the model.train() operation will take a bit of time to execute, your total time will not be 0.

Hi @ptrblck, I also tried timing model.train() using time.time() which returned 0, too. So, my initial thought was that as model was already in training mode by default, so basically running model.train() would not incur cost in time, hence 0.
Would be interested to know why it’s returning 0, though.

Even if the operation won’t change any internal flags, still some Python code would be executed, so I don’t know why it shows a zero. Maybe the timer has not enough resolution to properly time the fast execution. On my laptop, I’ll get sometimes a result of 0.0005006 and other times 0.0.
Running multiple iterations and calculating the average time solves this issue usually.

1 Like