question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug in deserializing TF2.0 GRU layer's bias vector in tf.loadLayersModel()

See original GitHub issue

Dear tfjs-team,

I ran into an issue these days when I tried to import a former Keras .h5 model with GRU layers into tfjs, see https://github.com/tensorflow/tfjs/issues/2437

tldr: this error popped up:

 Uncaught (in promise) Error: Shape mismatch: [384] vs. [2,384]
     at variables.ts:135
     at t.write (variables.ts:98)
     at variables.ts:339
     at Array.forEach (<anonymous>)
     at sf (variables.ts:337)
     at e.loadWeights (container.ts:598)
     at models.ts:315
     at common.ts:14
     at Object.next (common.ts:14)
     at a (common.ts:14)

On further investigation, I figured out that there’s something wrong in models.ts:300 in the deserialize() function.

Turns out that the model object returned by deserialize() sets faulty shapes for the bias vectors of GRU layers (those should be of shape [2, x] but are set to [x], here [384):

tfjs_debug3

On the contrary, the weights loaded in models.ts:313 by io.decodeWeights() are set correctly (here [2, 384]):

tfjs_debug2

So there must be something wrong with deserialize() or some nested functions. I really tried to dig further, but I’m basically totally foreign to JS/TS so it’s really hard for me to figure it out any further.

This bug should be easy to reproduce, just create some model with GRU layers in Keras, like this:

model = keras.models.Sequential([
#    keras.layers.GRU(128, return_sequences=True, batch_input_shape=[batch_size, None, max_id+1]),
    keras.layers.GRU(128, return_sequences=True, input_shape=[ None, max_id+1]),
    keras.layers.GRU(128, return_sequences=True),
    keras.layers.GRU(128),
    keras.layers.Flatten(),
    keras.layers.Dense(output_size, activation="softmax")
])

I guess you don’t even need to train it, just initializing it should be fine.

Then convert it with the tfjs-converter, and load it with tf.loadLayersModel

I’d be really grateful for any fixes or quick workarounds. Thank you in advance!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:17

github_iconTop GitHub Comments

1reaction
elazargcommented, Mar 29, 2020

reset_after=True doesn’t work for me:

import tensorflow as tf

USE_BIAS = True  # Fails iff True
model = tf.keras.Sequential([
    tf.keras.layers.Embedding(5, 7),
    tf.keras.layers.GRU(3, use_bias=USE_BIAS, reset_after=True)
])

import tensorflowjs as tfjs
tfjs.converters.save_keras_model(model, 'bad_js/')

Version 1.6.0

0reactions
GUR9000commented, Jun 19, 2020

Can someone explain why the GRU would need twice as many biases in TF2.0 compared to all other implementations of GRUs (origninal paper, TF1, other frameworks, etc.? Is this a TF2 bug?

The original bias tensor already contained the non-recurrent and recurrent parts, so the (2, XX) seems superfluous.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tensorflow.js tf.loadLayersModel() Function - GeeksforGeeks
The function tf.loadLayersModel() is used to load a model composed of Layer objects, including its topology and optionally weights. Syntax:
Read more >
Why does the bias need to be a vector in a neural network?
In a simple feed-forward network, each artificial neuron has a separate bias value. This allows for greater flexibility for the output layer ......
Read more >
عقدة قاعدة غير شرعي tf layers dense - technetennis.com
خيط اطّلع عامل Bug in deserializing TF2.0 GRU layer's bias vector in tf.loadLayersModel() · Issue #2442 · tensorflow/tfjs · GitHub ...
Read more >
Getting weights from tensorflow.js neural network
const model = tf.sequential(); model.add(tf.layers.dense({units: 4, inputShape: [8]})) ... getWeights()[0].print() // bias: model.layers[0].
Read more >
TensorFlow.js - Loading the model into a neural network web ...
let model; (async function () { model = await tf.loadLayersModel("http://localhost:81/tfjs-models/VGG16/model.json"); $(".progress-bar") ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found