MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / batch_jacobian

Method batch_jacobian

tensorflow/python/eager/backprop.py:1127–1246  ·  view source on GitHub ↗

Computes and stacks per-example jacobians. See [wikipedia article](http://en.wikipedia.org/wiki/jacobian_matrix_and_determinant) for the definition of a Jacobian. This function is essentially an efficient implementation of the following: `tf.stack([self.jacobian(y[i], x[i]) for i i

(self,
                     target,
                     source,
                     unconnected_gradients=UnconnectedGradients.NONE,
                     parallel_iterations=None,
                     experimental_use_pfor=True)

Source from the content-addressed store, hash-verified

1125 return nest.pack_sequence_as(sources, output)
1126
1127 def batch_jacobian(self,
1128 target,
1129 source,
1130 unconnected_gradients=UnconnectedGradients.NONE,
1131 parallel_iterations=None,
1132 experimental_use_pfor=True):
1133 """Computes and stacks per-example jacobians.
1134
1135 See [wikipedia article](http://en.wikipedia.org/wiki/jacobian_matrix_and_determinant) for the
1136 definition of a Jacobian. This function is essentially an efficient
1137 implementation of the following:
1138
1139 `tf.stack([self.jacobian(y[i], x[i]) for i in range(x.shape[0])])`.
1140
1141 Note that compared to `GradientTape.jacobian` which computes gradient of
1142 each output value w.r.t each input value, this function is useful when
1143 `target[i,...]` is independent of `source[j,...]` for `j != i`. This
1144 assumption allows more efficient computation as compared to
1145 `GradientTape.jacobian`. The output, as well as intermediate activations,
1146 are lower dimensional and avoid a bunch of redundant zeros which would
1147 result in the jacobian computation given the independence assumption.
1148
1149 Example usage:
1150
1151 ```python
1152 with tf.GradientTape() as g:
1153 x = tf.constant([[1., 2.], [3., 4.]], dtype=tf.float32)
1154 g.watch(x)
1155 y = x * x
1156 batch_jacobian = g.batch_jacobian(y, x)
1157 # batch_jacobian is [[[2, 0], [0, 4]], [[6, 0], [0, 8]]]
1158 ```
1159
1160 Args:
1161 target: A tensor with rank 2 or higher and with shape [b, y1, ..., y_n].
1162 `target[i,...]` should only depend on `source[i,...]`.
1163 source: A tensor with rank 2 or higher and with shape [b, x1, ..., x_m].
1164 unconnected_gradients: a value which can either hold 'none' or 'zero' and
1165 alters the value which will be returned if the target and sources are
1166 unconnected. The possible values and effects are detailed in
1167 'UnconnectedGradients' and it defaults to 'none'.
1168 parallel_iterations: A knob to control how many iterations are dispatched
1169 in parallel. This knob can be used to control the total memory usage.
1170 experimental_use_pfor: If true, uses pfor for computing the Jacobian. Else
1171 uses a tf.while_loop.
1172
1173 Returns:
1174 A tensor `t` with shape [b, y_1, ..., y_n, x1, ..., x_m] where `t[i, ...]`
1175 is the jacobian of `target[i, ...]` w.r.t. `source[i, ...]`, i.e. stacked
1176 per-example jacobians.
1177
1178 Raises:
1179 RuntimeError: If called on a non-persistent tape with eager execution
1180 enabled and without enabling experimental_use_pfor.
1181 ValueError: If vectorization of jacobian computation fails or if first
1182 dimension of `target` and `source` do not match.
1183 """
1184 target_shape = target.shape

Calls 15

is_compatible_withMethod · 0.95
_push_tapeMethod · 0.95
_pop_tapeMethod · 0.95
with_rank_at_leastMethod · 0.80
is_fully_definedMethod · 0.80
assert_equalMethod · 0.80
reshapeMethod · 0.80
executing_eagerlyMethod · 0.80
transposeMethod · 0.80
DimensionMethod · 0.45
num_elementsMethod · 0.45
shapeMethod · 0.45