I have a class/wrapper for a training a neural-network using PyTorch. To simplify the question say the class looks something like
class Wrapper:
def __init__(self,validate,n_epochs =100_000_000):
self.validate = validate
self.val_score = 0
def run(self):
for _ in self.n_epochs:
if self.validate:
self.val_score += val(self.X,self.y)
as you can see the check if self.validate has to be run 100.000.000 times, but the statement is always the same i.e it is always the same (it cannot suddenly switch from True to False).
I know I can write a function for each case of validate and then call the correct function in run. The issue is tho, that having, say, 3 of such "static" boolean variables would result in 9 functions in total where 99% of the code in each function is the same as the other 8 functions.
Is there a way to, somehow, define a "static" path in the "if-statement" without writing a function for each case to reduce the check-overhead? I have read some where, that some compilers tries to guess the direction of the if-statements to speed up the processes. If that's the case I assume the check-overhead of our run function is rather fast limited since it would start guessing correctly relative fast.
Aucun commentaire:
Enregistrer un commentaire