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

Method testLoop

tfjs-layers/src/engine/training.ts:1226–1269  ·  view source on GitHub ↗

* Loop over some test data in batches. * @param f A Function returning a list of tensors. * @param ins Array of tensors to be fed to `f`. * @param batchSize Integer batch size or `null` / `undefined`. * @param verbose verbosity mode. * @param steps Total number of steps (batches of sa

(
      f: (data: Tensor[]) => Scalar[], ins: Tensor[], batchSize?: number,
      verbose = 0, steps?: number)

Source from the content-addressed store, hash-verified

1224 * @returns Array of Scalars.
1225 */
1226 private testLoop(
1227 f: (data: Tensor[]) => Scalar[], ins: Tensor[], batchSize?: number,
1228 verbose = 0, steps?: number): Scalar[] {
1229 return tfc.tidy(() => {
1230 const numSamples = this.checkNumSamples(ins, batchSize, steps, 'steps');
1231 const outs: Scalar[] = [];
1232 if (verbose > 0) {
1233 throw new NotImplementedError('Verbose mode is not implemented yet.');
1234 }
1235 // TODO(cais): Use `indicesForConversionToDense' to prevent slow down.
1236 if (steps != null) {
1237 throw new NotImplementedError(
1238 'steps mode in testLoop() is not implemented yet');
1239 } else {
1240 const batches = makeBatches(numSamples, batchSize);
1241 const indexArray = tensor1d(range(0, numSamples));
1242 for (let batchIndex = 0; batchIndex < batches.length; ++batchIndex) {
1243 const batchStart = batches[batchIndex][0];
1244 const batchEnd = batches[batchIndex][1];
1245 const batchIds =
1246 K.sliceAlongFirstAxis(
1247 indexArray, batchStart, batchEnd - batchStart) as Tensor1D;
1248 // TODO(cais): In ins, train flag can be a number, instead of an
1249 // Tensor? Do we need to handle this in tfjs-layers?
1250 const insBatch = sliceArraysByIndices(ins, batchIds) as Scalar[];
1251 const batchOuts = f(insBatch);
1252 if (batchIndex === 0) {
1253 for (let i = 0; i < batchOuts.length; ++i) {
1254 outs.push(scalar(0));
1255 }
1256 }
1257 for (let i = 0; i < batchOuts.length; ++i) {
1258 const batchOut = batchOuts[i];
1259 outs[i] =
1260 tfc.add(outs[i], tfc.mul(batchEnd - batchStart, batchOut));
1261 }
1262 }
1263 for (let i = 0; i < outs.length; ++i) {
1264 outs[i] = tfc.div(outs[i], numSamples);
1265 }
1266 }
1267 return outs;
1268 });
1269 }
1270
1271 protected getDedupedMetricsNames(): string[] {
1272 const outLabels = this.metricsNames;

Callers 2

evaluateMethod · 0.95
fitLoopMethod · 0.95

Calls 12

checkNumSamplesMethod · 0.95
makeBatchesFunction · 0.90
tensor1dFunction · 0.90
rangeFunction · 0.90
sliceArraysByIndicesFunction · 0.90
scalarFunction · 0.90
tidyMethod · 0.80
mulMethod · 0.80
divMethod · 0.80
addMethod · 0.65
fFunction · 0.50
pushMethod · 0.45

Tested by

no test coverage detected