Is there a way to access weights and biases in tfjs layers api?

1,641 views
Skip to first unread message

Nico Boh

unread,
Apr 4, 2018, 1:54:11 PM4/4/18
to TensorFlow.js Discussion

Hello,

is there a way to access weights and biases of a layer in tfjs with the layers api? To be more precisely I want to get and also set them to use a generic algorithm on my neural net.

If not, is it possible with just tfjs core?

Shanqing Cai

unread,
Apr 4, 2018, 1:58:44 PM4/4/18
to TensorFlow.js Discussion
The answer is yes.

Suppose you have defined your model as follows:

const model = tf.sequential();
model.add(tf.layers.dense({units: 2, inputShape: [10]}));
model.add(tf.layers.dense({units: 4}));
model.compile({loss: 'categoricalCrossentropy', optimizer: 'sgd'});

To access the weights (i.e., kernel and bias) of the first dense layer, for example:

// kernel:
model.layers[0].getWeights()[0].print()

// bias:
model.layers[0].getWeights()[1].print()

Likewise for the second dense layer.

Also note that this workflow is the same as Python Keras.

Shanqing Cai

unread,
Apr 4, 2018, 2:02:20 PM4/4/18
to TensorFlow.js Discussion
Sorry - overlooked the second part of your question. To set weights, use the setWeights() method. For example:

const model = tf.sequential();
model.add(tf.layers.dense({units: 2, inputShape: [10]}));
model.add(tf.layers.dense({units: 4}));
model.compile({loss: 'categoricalCrossentropy', optimizer: 'sgd'});

// To set the weights (kernels and bias) of the first dense layer to all ones:
model.layers[0].setWeights([tf.ones([10, 2]), tf.ones([2])]);

Nico Boh

unread,
Apr 4, 2018, 2:13:39 PM4/4/18
to TensorFlow.js Discussion
Ahh thank you so much <3

Tiago Vasconcelos

unread,
Apr 6, 2018, 11:37:27 AM4/6/18
to TensorFlow.js Discussion
Can this method be used to save the model? I'd like to train the model and then save for re use without having to do training every time...

Nikhil Thorat

unread,
Apr 6, 2018, 11:38:35 AM4/6/18
to Tiago Vasconcelos, TensorFlow.js Discussion
See this issue which is high-priority: https://github.com/tensorflow/tfjs/issues/13

--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+unsubscribe@tensorflow.org.
Visit this group at https://groups.google.com/a/tensorflow.org/group/tfjs/.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/tfjs/6be5e49e-a964-4326-b23f-22957714dd2f%40tensorflow.org.

tensorflow.r...@gmail.com

unread,
Apr 22, 2018, 12:42:31 AM4/22/18
to TensorFlow.js Discussion
This is great! I have spent one day by trying with the method get_weight() before posting the question in the other thread where you have replied to refer to this post.

now, How to know that such method already exists? I referred the API documentation and this method does not seem to be there in the documentation. Am I missing something very trivial

Thank you!

Jeremy Ellis

unread,
Apr 27, 2018, 7:31:22 PM4/27/18
to TensorFlow.js Discussion
Any hints on how to manipulate getWeights so that it can be imported into setWeights. 

Jeremy Ellis

unread,
Apr 28, 2018, 12:23:28 PM4/28/18
to TensorFlow.js Discussion


      model.layers[0].setWeights(model.layers[0].getWeights()) 


The above code works, but not after I have put getWeights into a textarea.  JSON.stringify  JSON.parse,  split() don't seem to help. Any suggestions for getting getWeights back from a string into a usable form for setWeights?


Jeremy Ellis

unread,
Apr 28, 2018, 12:55:23 PM4/28/18
to TensorFlow.js Discussion



Looks like this code does work to convert getWeights back from a textarea string into something setWeights can work with.


      model.layers[0].setWeights(model.layers[0].getWeights(document.getElementById('myArea01').value.split('\r\n|\n'))) 


 

Jeremy Ellis

unread,
Apr 28, 2018, 1:11:34 PM4/28/18
to TensorFlow.js Discussion
Sorry for posting so many times, but I am really impressed that this bit of code works to load your entire model from a textarea not just a layer at a time. 




      model.setWeights(model.getWeights(document.getElementById('myArea01').value.split('\r\n|\n'))) 


Jeremy Ellis

unread,
Apr 29, 2018, 1:29:09 AM4/29/18
to TensorFlow.js Discussion
oops. The above 2 posts are totally wrong. Still stuck on how to convert getWeights to a string and then back into setWeights.

Shanqing Cai

unread,
Apr 29, 2018, 11:24:42 AM4/29/18
to TensorFlow.js Discussion
The work on saving models including their weights from the browser is underway and being tracked by

Depending on your schedule, you might want to wait for the features to land instead of writing your
own saving logic.

Jeremy Ellis

unread,
Apr 29, 2018, 12:44:42 PM4/29/18
to TensorFlow.js Discussion
Reply all
Reply to author
Forward
0 new messages