Get The Layers From One Model And Assign It To Another Model
Given a model created using tf.sequential(), is it possible to get the layers and to use them to create another model using tf.model() ? const model = tf.sequential(); model.add(tf
Solution 1:
To get the layers of the model created using tf.sequential
, one needs to use the property layers
of the model
const model = tf.sequential();
// first layer
model.add(tf.layers.dense({units: 32, inputShape: [50]}));
// second layer
model.add(tf.layers.dense({units: 4}));
// get all the layers of the modelconst layers = model.layers// second modelconst model2 = tf.model({
inputs: layers[0].input,
outputs: layers[1].output
})
model2.predict(tf.randomNormal([1, 50])).print()
<html><head><!-- Load TensorFlow.js --><scriptsrc="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.12.0"></script></head><body></body></html>
One can also use the apply method
const model = tf.sequential();
// first layer
model.add(tf.layers.dense({units: 32, inputShape: [50]}));
// second layer
model.add(tf.layers.dense({units: 4}));
var input = tf.randomNormal([1, 50])
var layers = model.layersfor (var i=0; i < layers.length; i++){
var layer = layers[i]
var output = layer.apply(input)
input = output
output.print()
}
<html><head><!-- Load TensorFlow.js --><scriptsrc="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.12.0"></script></head><body></body></html>
Post a Comment for "Get The Layers From One Model And Assign It To Another Model"