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

Function modelFromJSON

tfjs-layers/src/models.ts:62–103  ·  view source on GitHub ↗
(
    modelAndWeightsConfig: ModelAndWeightsConfig|PyJsonDict,
    customObjects?: serialization.ConfigDict)

Source from the content-addressed store, hash-verified

60 * @returns A TensorFlow.js Layers `tf.LayersModel` instance (uncompiled).
61 */
62export async function modelFromJSON(
63 modelAndWeightsConfig: ModelAndWeightsConfig|PyJsonDict,
64 customObjects?: serialization.ConfigDict): Promise<LayersModel> {
65 if (!('modelTopology' in modelAndWeightsConfig)) {
66 modelAndWeightsConfig = {modelTopology: modelAndWeightsConfig};
67 }
68 modelAndWeightsConfig = modelAndWeightsConfig as ModelAndWeightsConfig;
69
70 let modelTopology = modelAndWeightsConfig.modelTopology;
71 if (modelTopology['model_config'] != null) {
72 // If the model-topology JSON contains a 'model_config' field, then it is
73 // a full model JSON (e.g., from `keras.Model.save()`), which contains
74 // not only the model's architecture in its 'model_config' field, but
75 // additional information such as the model's optimizer. We use only the
76 // 'model_config' field currently.
77 modelTopology = modelTopology['model_config'] as PyJsonDict;
78 }
79 const tsConfig =
80 convertPythonicToTs(modelTopology) as serialization.ConfigDict;
81 const model = deserialize(tsConfig, customObjects) as LayersModel;
82
83 if (modelAndWeightsConfig.weightsManifest != null) {
84 // Load the weight values keyed by the original tensor names in the model
85 // file that was loaded. These should match the keys of the weight
86 // manifest.
87 const weightValues = await io.loadWeights(
88 modelAndWeightsConfig.weightsManifest, modelAndWeightsConfig.pathPrefix,
89 model.weights.map(weight => weight.originalName));
90
91 // Map the weights to the unique tensor names generated during model loading
92 const uniqueWeightValues: NamedTensorMap = {};
93 for (const weight of model.weights) {
94 uniqueWeightValues[weight.originalName] =
95 weightValues[weight.originalName];
96 }
97
98 model.loadWeights(uniqueWeightValues);
99 // Dispose temporary weight values.
100 dispose(weightValues);
101 }
102 return model;
103}
104
105/**
106 * Options for loading a saved mode in TensorFlow.js format.

Callers 3

models_test.tsFile · 0.90
recurrent_test.tsFile · 0.90

Calls 4

convertPythonicToTsFunction · 0.90
deserializeFunction · 0.90
disposeFunction · 0.90
loadWeightsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…