Categories
Misc

Access python argument inside a Keras custom metric defined on a file different from the launched python script

I’m working on a Machine Learning project with tf.keras, and, to start training, I launch the script via shell as:

python file1.py --coeff=2.0 

In file1.py, I parse the argument coeff such as:

parser = argparse.ArgumentParser() parser.add_argument("--coeff", type=float, default=1.0) arguments = parser.parse_args() 

In file2.py, I defined a tf.keras custom metric, as a function, such as:

def custom_metric(y_true, y_pred): y_true_new = tf.multiply(y_true, MYCOEFF) # .. additional stuff.. 

used by the function load_model (defined in the same file) used to load the model:

def load_model(model_name): model = tf.keras.models.load_model(model_name, custom_objects={'custom_metric': custom_metric}) 

Which is a good and easy way to have MYCOEFF equal to the argument coeff passed as argument when launching the script?

What I tried:

I tried to add, in file1.py, the line:

MYCOEFF = arguments.coeff 

and, then, importing it in file2.py as:

from file1 import MYCOEFF 

but in my case it doesn’t work, because I obtain an error regarding a “circular import”.

Are there any other (and better) ways?

submitted by /u/RainbowRedditForum
[visit reddit] [comments]

Leave a Reply

Your email address will not be published. Required fields are marked *