MCPcopy
hub / github.com/tensorflow/tfjs / sparseCategoricalCrossentropy

Function sparseCategoricalCrossentropy

tfjs-layers/src/losses.ts:136–148  ·  view source on GitHub ↗
(
    target: Tensor, output: Tensor, fromLogits = false)

Source from the content-addressed store, hash-verified

134 * a tensor of logits.
135 */
136export function sparseCategoricalCrossentropy(
137 target: Tensor, output: Tensor, fromLogits = false): Tensor {
138 return tidy(() => {
139 const flatTarget =
140 tfc.cast(tfc.floor(K.flatten(target)), 'int32') as Tensor1D;
141 output = tfc.clipByValue(output, epsilon(), 1 - epsilon());
142 const outputShape = output.shape;
143 const oneHotTarget = tfc.reshape(
144 tfc.oneHot(flatTarget, outputShape[outputShape.length - 1]),
145 outputShape);
146 return categoricalCrossentropy(oneHotTarget, output, fromLogits);
147 });
148}
149
150/**
151 * From TensorFlow's implementation in nn_impl.py:

Callers

nothing calls this directly

Calls 9

tidyFunction · 0.90
epsilonFunction · 0.90
floorMethod · 0.80
flattenMethod · 0.80
clipByValueMethod · 0.80
reshapeMethod · 0.80
oneHotMethod · 0.80
categoricalCrossentropyFunction · 0.70
castMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…