Computes the jacobian using operations recorded in context of this tape. See [wikipedia article](http://en.wikipedia.org/wiki/jacobian_matrix_and_determinant) for the definition of a Jacobian. Example usage: ```python with tf.GradientTape() as g: x = tf.constant([1.0, 2
(self,
target,
sources,
unconnected_gradients=UnconnectedGradients.NONE,
parallel_iterations=None,
experimental_use_pfor=True)
| 1021 | return grad |
| 1022 | |
| 1023 | def jacobian(self, |
| 1024 | target, |
| 1025 | sources, |
| 1026 | unconnected_gradients=UnconnectedGradients.NONE, |
| 1027 | parallel_iterations=None, |
| 1028 | experimental_use_pfor=True): |
| 1029 | """Computes the jacobian using operations recorded in context of this tape. |
| 1030 | |
| 1031 | See [wikipedia article](http://en.wikipedia.org/wiki/jacobian_matrix_and_determinant) for the |
| 1032 | definition of a Jacobian. |
| 1033 | |
| 1034 | Example usage: |
| 1035 | |
| 1036 | ```python |
| 1037 | with tf.GradientTape() as g: |
| 1038 | x = tf.constant([1.0, 2.0]) |
| 1039 | g.watch(x) |
| 1040 | y = x * x |
| 1041 | jacobian = g.jacobian(y, x) |
| 1042 | # jacobian value is [[2., 0.], [0., 4.]] |
| 1043 | ``` |
| 1044 | |
| 1045 | Args: |
| 1046 | target: Tensor to be differentiated. |
| 1047 | sources: a list or nested structure of Tensors or Variables. `target` |
| 1048 | will be differentiated against elements in `sources`. |
| 1049 | unconnected_gradients: a value which can either hold 'none' or 'zero' and |
| 1050 | alters the value which will be returned if the target and sources are |
| 1051 | unconnected. The possible values and effects are detailed in |
| 1052 | 'UnconnectedGradients' and it defaults to 'none'. |
| 1053 | parallel_iterations: A knob to control how many iterations are dispatched |
| 1054 | in parallel. This knob can be used to control the total memory usage. |
| 1055 | experimental_use_pfor: If true, vectorizes the jacobian computation. Else |
| 1056 | falls back to a sequential while_loop. Vectorization can sometimes fail |
| 1057 | or lead to excessive memory usage. This option can be used to disable |
| 1058 | vectorization in such cases. |
| 1059 | |
| 1060 | Returns: |
| 1061 | A list or nested structure of Tensors (or None), one for each element in |
| 1062 | `sources`. Returned structure is the same as the structure of `sources`. |
| 1063 | Note if any gradient is sparse (IndexedSlices), jacobian function |
| 1064 | currently makes it dense and returns a Tensor instead. This may change in |
| 1065 | the future. |
| 1066 | |
| 1067 | |
| 1068 | Raises: |
| 1069 | RuntimeError: If called on a non-persistent tape with eager execution |
| 1070 | enabled and without enabling experimental_use_pfor. |
| 1071 | ValueError: If vectorization of jacobian computation fails. |
| 1072 | """ |
| 1073 | flat_sources = nest.flatten(sources) |
| 1074 | target_static_shape = target.shape |
| 1075 | target_shape = array_ops.shape(target) |
| 1076 | # Note that we push and pop the tape here and below. This is needed since we |
| 1077 | # need gradients through the enclosed operations. |
| 1078 | self._push_tape() |
| 1079 | target = array_ops.reshape(target, [-1]) |
| 1080 | self._pop_tape() |