Implementation:Tensorflow Tfjs LayersModel Evaluate And Save For Transfer
Metadata
| Field | Value |
|---|---|
| Implementation Name | Tensorflow Tfjs LayersModel Evaluate And Save For Transfer |
| Library | TensorFlow.js |
| Domains | Transfer_Learning, Deployment |
| Type | API Doc (transfer learning evaluation and deployment) |
| Implements | Principle:Tensorflow_Tfjs_Model_Evaluation_And_Deployment |
| Source | TensorFlow.js |
| Last Updated | 2026-02-10 00:00 GMT |
Environment:Tensorflow_Tfjs_Browser_Runtime
Overview
LayersModel.evaluate and LayersModel.save are the TensorFlow.js APIs for measuring a fine-tuned transfer learning model's performance on held-out test data and serializing the complete model for production deployment. The evaluate method computes loss and metric values on test inputs/outputs, while save persists the entire model (frozen base + trained task head) to a storage backend such as IndexedDB, localStorage, or the filesystem.
Description
After fine-tuning a transfer learning model, these two methods complete the pipeline:
- evaluate runs a forward pass on the test data, computes the loss and any compiled metrics (e.g., accuracy), and returns the results as scalar tensors. This provides an unbiased estimate of the model's generalization performance on unseen data from the target domain.
- save serializes the model's topology (JSON) and weight values (binary shards) to a specified destination. The saved model is self-contained and can be loaded later with tf.loadLayersModel() for inference. Both the frozen base layers and the trained task head are saved together as a single model.
Code Reference
Source file: tfjs-layers/src/training.ts
- evaluate: Lines 840-863
- save: Lines 2112-2166
API Signatures
// Evaluate the model on test data
evaluate(
x: Tensor | Tensor[],
y: Tensor | Tensor[],
args?: ModelEvaluateArgs
): Scalar | Scalar[]
// Save the model to a storage backend
async save(
handlerOrURL: io.IOHandler | string,
config?: io.SaveConfig
): Promise<io.SaveResult>
Parameters
evaluate
| Parameter | Type | Required | Description |
|---|---|---|---|
| x | Tensor[] | Yes | Test input data. Must match the model's input shape (e.g., [numSamples, 224, 224, 3] for image classification). |
| y | Tensor[] | Yes | Test target data. Must match the model's output shape and the loss function's expectations. |
| args.batchSize | number |
No | Batch size for evaluation. Default: 32. Affects memory usage but not results. |
| args.verbose | number |
No | Verbosity mode. 0 = silent, 1 = progress bar. |
| args.sampleWeight | Tensor |
No | Optional weights for each sample, used to weight the loss. |
save
| Parameter | Type | Required | Description |
|---|---|---|---|
| handlerOrURL | string | Yes | Destination for the saved model. String schemes: indexeddb:// (browser), localstorage:// (browser, small models), file:// (Node.js), downloads:// (browser file download), or a custom IOHandler. |
| config.trainableOnly | boolean |
No | If true, only trainable weights are saved. Default: false. For transfer learning, keep false to save the complete model including frozen base weights. |
| config.includeOptimizer | boolean |
No | If true, the optimizer state is saved alongside the model. Useful if training may be resumed later. Default: false. |
Return Values
| Method | Return Type | Description |
|---|---|---|
| evaluate | Scalar[] | If the model has a single output and no metrics, returns a single Scalar (the loss). If the model has metrics, returns an array of Scalars: [loss, metric1, metric2, ...]. Use .dataSync() to extract the numeric value. |
| save | Promise<io.SaveResult> |
An object containing modelArtifactsInfo with metadata about the saved model (date, size, type, etc.). |
I/O Contract
| Direction | Description |
|---|---|
| Inputs | For evaluate: the fine-tuned transfer model, test input tensors, and test target tensors. For save: the fine-tuned model and a destination URL/handler. |
| Outputs | For evaluate: Scalar or Scalar[] containing loss and metric values. For save: Promise<SaveResult> with metadata about the saved artifact. |
| Side Effects | evaluate allocates GPU memory for forward pass computations (tensors should be disposed after reading values). save writes model topology (JSON) and weight shards (binary) to the specified storage backend. |
| Errors | evaluate throws if input shapes do not match the model's expected shapes or if the model has not been compiled. save throws if the storage backend is unavailable or if there is insufficient storage space. |
Usage Examples
Example 1: Evaluate on Test Set
// Evaluate on test set
const [testLoss, testAcc] = transferModel.evaluate(testXs, testYs);
console.log('Test loss:', testLoss.dataSync()[0]);
console.log('Test accuracy:', testAcc.dataSync()[0]);
// Dispose scalar tensors to free memory
testLoss.dispose();
testAcc.dispose();
Example 2: Save the Complete Transfer Learning Model
// Save the complete transfer learning model to IndexedDB (browser)
await transferModel.save('indexeddb://my-transfer-model');
// Or save for Node.js filesystem
await transferModel.save('file://./my-transfer-model');
Example 3: Full Evaluation and Deployment Pipeline
// Evaluate on held-out test set
const evalResult = transferModel.evaluate(testXs, testYs, {
batchSize: 32,
verbose: 1
});
// Handle both single-output and multi-metric cases
if (Array.isArray(evalResult)) {
const [loss, accuracy] = evalResult;
console.log('Test loss:', loss.dataSync()[0].toFixed(4));
console.log('Test accuracy:', (accuracy.dataSync()[0] * 100).toFixed(2) + '%');
loss.dispose();
accuracy.dispose();
} else {
console.log('Test loss:', evalResult.dataSync()[0].toFixed(4));
evalResult.dispose();
}
// Save the model if performance meets threshold
const testAccValue = transferModel.evaluate(testXs, testYs)[1].dataSync()[0];
if (testAccValue > 0.90) {
const saveResult = await transferModel.save('indexeddb://my-transfer-model-v1');
console.log('Model saved. Size:', saveResult.modelArtifactsInfo.modelTopologyBytes, 'bytes (topology)');
console.log('Weight data:', saveResult.modelArtifactsInfo.weightDataBytes, 'bytes');
} else {
console.log('Model accuracy below threshold. Not saving.');
}
Example 4: Save with Optimizer State for Resumable Training
// Save the model with optimizer state so training can be resumed
const saveResult = await transferModel.save('file://./my-transfer-model', {
includeOptimizer: true
});
console.log('Model saved with optimizer state');
console.log('Artifacts info:', JSON.stringify(saveResult.modelArtifactsInfo, null, 2));
Example 5: Load and Verify the Saved Model
// Load the saved transfer model for inference
const loadedModel = await tf.loadLayersModel('indexeddb://my-transfer-model-v1');
// Verify the loaded model produces the same results
const [origLoss, origAcc] = transferModel.evaluate(testXs, testYs);
loadedModel.compile({
optimizer: tf.train.adam(0.0001),
loss: 'categoricalCrossentropy',
metrics: ['accuracy']
});
const [loadedLoss, loadedAcc] = loadedModel.evaluate(testXs, testYs);
console.log('Original test accuracy:', origAcc.dataSync()[0]);
console.log('Loaded test accuracy:', loadedAcc.dataSync()[0]);
// Clean up
origLoss.dispose();
origAcc.dispose();
loadedLoss.dispose();
loadedAcc.dispose();
Example 6: Browser Download for Sharing
// Trigger a browser download of the model files
// This downloads model.json and weight shard .bin files
await transferModel.save('downloads://my-flower-classifier');
Usage
In the transfer learning workflow, evaluate and save are the final steps:
- Evaluate on the held-out test set to obtain unbiased performance metrics.
- Interpret the results -- compare against baselines, check for overfitting (training accuracy >> test accuracy).
- Save the model if performance meets requirements.
- Verify the saved model by loading it and confirming equivalent evaluation results.
Important memory management: The evaluate method returns Scalar tensors that allocate GPU memory. Always call .dispose() on these tensors after reading their values with .dataSync() to prevent memory leaks, especially in long-running browser applications.
Related Pages
- Principle:Tensorflow_Tfjs_Model_Evaluation_And_Deployment -- The principle this implementation realizes
- Implementation:Tensorflow_Tfjs_LayersModel_Compile_And_Fit_For_Transfer -- Training the model prior to evaluation
- Implementation:Tensorflow_Tfjs_Tf_LoadLayersModel_For_Transfer -- Loading a saved model for inference (the inverse of saving)
- Implementation:Tensorflow_Tfjs_Tf_Model_Functional -- The model architecture that is evaluated and saved
Environments
- Environment:Tensorflow_Tfjs_Browser_Runtime -- Browser runtime (WebGL / WebGPU / WASM / CPU backends)