Selects `x` in train phase, and `alt` otherwise. Note that `alt` should have the *same shape* as `x`. Arguments: x: What to return in train phase (tensor or callable that returns a tensor). alt: What to return otherwise (tensor or callable that returns a tensor)
(x, alt, training=None)
| 4143 | |
| 4144 | @keras_export('keras.backend.in_train_phase') |
| 4145 | def in_train_phase(x, alt, training=None): |
| 4146 | """Selects `x` in train phase, and `alt` otherwise. |
| 4147 | |
| 4148 | Note that `alt` should have the *same shape* as `x`. |
| 4149 | |
| 4150 | Arguments: |
| 4151 | x: What to return in train phase |
| 4152 | (tensor or callable that returns a tensor). |
| 4153 | alt: What to return otherwise |
| 4154 | (tensor or callable that returns a tensor). |
| 4155 | training: Optional scalar tensor |
| 4156 | (or Python boolean, or Python integer) |
| 4157 | specifying the learning phase. |
| 4158 | |
| 4159 | Returns: |
| 4160 | Either `x` or `alt` based on the `training` flag. |
| 4161 | the `training` flag defaults to `K.learning_phase()`. |
| 4162 | """ |
| 4163 | if training is None: |
| 4164 | training = learning_phase() |
| 4165 | |
| 4166 | # TODO(b/138862903): Handle the case when training is tensor. |
| 4167 | if not tensor_util.is_tensor(training): |
| 4168 | if training == 1 or training is True: |
| 4169 | if callable(x): |
| 4170 | return x() |
| 4171 | else: |
| 4172 | return x |
| 4173 | |
| 4174 | elif training == 0 or training is False: |
| 4175 | if callable(alt): |
| 4176 | return alt() |
| 4177 | else: |
| 4178 | return alt |
| 4179 | |
| 4180 | # else: assume learning phase is a placeholder tensor. |
| 4181 | x = switch(training, x, alt) |
| 4182 | return x |
| 4183 | |
| 4184 | |
| 4185 | @keras_export('keras.backend.in_test_phase') |
no test coverage detected