0
$\begingroup$

I am using keras custom model with custom train_step and test_step methods overwritten. Also, have a need to change certain margin used in the loss function, only for test dataset. In other words I want to use one value of margin for training and validation datasets, but a little relaxed margin for test datasets. I think I can accomplish this if the test_step method gets to know(some how) if the function call has come from model.fit(validation part) and model.evaluate(test part).

$\endgroup$

2 Answers 2

0
$\begingroup$

Yeah, you can do that. You can just use a flag or variable that you can set based on the context in which the method is being called and in this way you can make the custom test_step method aware of the call being made from model.fit and model.evaluate.

Well, what you can do is that you could define a variable is_training in your custom model, and set it to True in the train_step method and to False in the test_step method. Then, you can use this variable to determine whether the model is being trained or evaluated in the test_step method.

$\endgroup$
1
  • $\begingroup$ I think, I didn't get it entirely. How setting is_training inside train_step or test_step helps this condition! I am guessing such kind of setting has to be done outside of test_step, that way test_step would know where the call has come from. My apologies, if I didn't get it correctly, can you rephrase it please. $\endgroup$
    – Nik
    Dec 16, 2022 at 10:09
0
$\begingroup$

There is a nice keras.backend.in_train_phase() function for that. It is not documented (intentionally) but its signature is:

def in_train_phase(
    x: Any,
    alt: Any,
    training: Any | None = None
) -> Any

Selects x in train phase, and alt otherwise.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.