Make each of the parameter_positions in args a unique ops.Tensor object. Ensure that each parameter is treated independently. For example: def f(x, y): return x * y g = gradients_function(f) one = tf.constant(1.) g(one, one) should return [1., 1.] (even though the two arguments are
(parameter_positions, args)
| 372 | |
| 373 | |
| 374 | def _ensure_unique_tensor_objects(parameter_positions, args): |
| 375 | """Make each of the parameter_positions in args a unique ops.Tensor object. |
| 376 | |
| 377 | Ensure that each parameter is treated independently. |
| 378 | For example: |
| 379 | |
| 380 | def f(x, y): return x * y |
| 381 | g = gradients_function(f) |
| 382 | one = tf.constant(1.) |
| 383 | |
| 384 | g(one, one) should return [1., 1.] |
| 385 | (even though the two arguments are the same Tensor object). |
| 386 | |
| 387 | Args: |
| 388 | parameter_positions: List of indices into args defining the arguments to |
| 389 | differentiate against. |
| 390 | args: A list of arguments to the function to be differentiated. |
| 391 | |
| 392 | Returns: |
| 393 | args, possibly edited in-place. |
| 394 | """ |
| 395 | s = set() |
| 396 | for (i, t) in enumerate(args): |
| 397 | if i in parameter_positions: |
| 398 | tid = ops.tensor_id(t) |
| 399 | if tid in s: |
| 400 | args[i] = gen_array_ops.identity(args[i]) |
| 401 | else: |
| 402 | s.add(tid) |
| 403 | return args |
| 404 | |
| 405 | |
| 406 | def val_and_grad_function(f, params=None): |