MCPcopy
hub / github.com/tensorflow/tfjs-examples / getBaselineMeanAbsoluteError

Function getBaselineMeanAbsoluteError

jena-weather/models.js:52–82  ·  view source on GitHub ↗
(
    jenaWeatherData, normalize, includeDateTime, lookBack, step, delay)

Source from the content-addressed store, hash-verified

50 * prediction.
51 */
52export async function getBaselineMeanAbsoluteError(
53 jenaWeatherData, normalize, includeDateTime, lookBack, step, delay) {
54 const batchSize = 128;
55 const dataset = tf.data.generator(
56 () => jenaWeatherData.getNextBatchFunction(
57 false, lookBack, delay, batchSize, step, VAL_MIN_ROW, VAL_MAX_ROW,
58 normalize, includeDateTime));
59
60 const batchMeanAbsoluteErrors = [];
61 const batchSizes = [];
62 await dataset.forEachAsync(dataItem => {
63 const features = dataItem.xs;
64 const targets = dataItem.ys;
65 const timeSteps = features.shape[1];
66 batchSizes.push(features.shape[0]);
67 batchMeanAbsoluteErrors.push(tf.tidy(
68 () => tf.losses.absoluteDifference(
69 targets,
70 features.gather([timeSteps - 1], 1).gather([1], 2).squeeze([2]))));
71 });
72
73 const meanAbsoluteError = tf.tidy(() => {
74 const batchSizesTensor = tf.tensor1d(batchSizes);
75 const batchMeanAbsoluteErrorsTensor = tf.stack(batchMeanAbsoluteErrors);
76 return batchMeanAbsoluteErrorsTensor.mul(batchSizesTensor)
77 .sum()
78 .div(batchSizesTensor.sum());
79 });
80 tf.dispose(batchMeanAbsoluteErrors);
81 return meanAbsoluteError.dataSync()[0];
82}
83
84/**
85 * Build a linear-regression model for the temperature-prediction problem.

Callers 2

mainFunction · 0.90
models_test.jsFile · 0.90

Calls 1

getNextBatchFunctionMethod · 0.80

Tested by

no test coverage detected