(*inputs)
| 247 | inputs = input.get_input_tensors() |
| 248 | |
| 249 | def compute_grad_from_inputs(*inputs): |
| 250 | cost = get_cost_fn(*inputs) |
| 251 | assert isinstance(cost, tf.Tensor), \ |
| 252 | "Expect the given function to return a cost, but got {} instead".format(str(cost)) |
| 253 | assert cost.shape.ndims == 0, "Cost must be a scalar, but found {}!".format(cost) |
| 254 | |
| 255 | if not ctx.is_training: |
| 256 | return None # this is the tower function, could be called for inference |
| 257 | |
| 258 | if ctx.has_own_variables: |
| 259 | varlist = ctx.get_collection_in_tower(tfv1.GraphKeys.TRAINABLE_VARIABLES) |
| 260 | else: |
| 261 | varlist = tfv1.trainable_variables() |
| 262 | opt = get_opt_fn() |
| 263 | if is_tfv2() and isinstance(opt, tf.optimizers.Optimizer): |
| 264 | grads = opt.get_gradients(cost, varlist) |
| 265 | grads = list(zip(grads, varlist)) |
| 266 | else: |
| 267 | grads = opt.compute_gradients( |
| 268 | cost, var_list=varlist, |
| 269 | gate_gradients=self.GATE_GRADIENTS, |
| 270 | colocate_gradients_with_ops=self.COLOCATE_GRADIENTS_WITH_OPS, |
| 271 | aggregation_method=self.AGGREGATION_METHOD) |
| 272 | grads = FilterNoneGrad().process(grads) |
| 273 | return grads |
| 274 | |
| 275 | if not self.XLA_COMPILE: |
| 276 | return compute_grad_from_inputs(*inputs) |
nothing calls this directly
no test coverage detected