Tensorflow JS error with model imported from Keras (Based on the provided shape, [2,256], the tensor should have 512 values but has 43)

1,678 views
Skip to first unread message

Timon Lukas

unread,
Aug 31, 2018, 8:32:51 AM8/31/18
to TensorFlow.js Discussion
Hey guys!

I have a big problem with Tensorflow JS right now. I trained an LSTM based model with Keras and I'm now trying to import it into TFJS. To do so, I converted it using tensorflowjs_converter, and it worked fine - no errors appeared. But when I try to open it in the browser, I get this error:

Based on the provided shape, [2,256], the tensor should have 512 values but has 43

Does anyone have a clue why this happens?

The model definition:

model = Sequential()

model
.add(LSTM(
   
32,
    batch_input_shape
=(30, 5, 3),
    return_sequences
=True,
    stateful
=True,
    activation
='tanh',
))
model
.add(Dropout(0.25))

model
.add(LSTM(
   
32,
    return_sequences
=True,
    stateful
=True,
    activation
='tanh',
))
model
.add(Dropout(0.25))

model
.add(LSTM(
   
32,
    return_sequences
=False,
    stateful
=True,
    activation
='tanh',
))
model
.add(Dropout(0.25))

model
.add(Dense(3, activation='tanh', kernel_initializer='lecun_uniform'))

model
.compile(loss='mse', optimizer=Adam())

This is the Keras model: https://drive.google.com/open?id=1ts8YsbRfYo9yaHYUraEcrKPgke5xkwwo

I am pretty sure that this is a problem with TFJS itself.

I am using the latest version of all involved libraries.

Thank you!

Nikhil Thorat

unread,
Aug 31, 2018, 11:52:01 AM8/31/18
to timon....@gmail.com, TensorFlow.js Discussion
Does this error happen when you try to make a prediction? Can you paste the javascript code you're using to load and execute the model?

--
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+uns...@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/084153fd-8bc1-4267-99b8-dd836cace89a%40tensorflow.org.

Daniel Smilkov

unread,
Aug 31, 2018, 12:54:51 PM8/31/18
to Nikhil Thorat, timon....@gmail.com, TensorFlow.js Discussion
Also if you can send us the stack trace, that would be great!

Daniel


Timon Lukas

unread,
Aug 31, 2018, 2:33:41 PM8/31/18
to TensorFlow.js Discussion, nsth...@google.com, timon....@gmail.com
Am Freitag, 31. August 2018 17:52:01 UTC+2 schrieb Nikhil Thorat:
Does this error happen when you try to make a prediction? Can you paste the javascript code you're using to load and execute the model?

The error happens while I'm loading the model, which makes the Promise not return a value - so I can't test predictions.

This is literally the whole code needed to create the error:
model = await tf.loadModel(`http://localhost:3000/model.json`)
(group1-shard1of1 of course also being accessible through the Web server, and loading fine)

Am Freitag, 31. August 2018 18:54:51 UTC+2 schrieb Daniel Smilkov:
Also if you can send us the stack trace, that would be great!

Yeah, no problem! I use Webpack for compiling my frontend, but I hope it's still useful to you guys.
Error: Based on the provided shape, [2,256], the tensor should have 512 values but has 43
    at assert (tf-core.esm.js?45ef:17)
    at new e (tf-core.esm.js?45ef:17)
    at Function.e.make (tf-core.esm.js?45ef:17)
    at tensor (tf-core.esm.js?45ef:17)
    at o (tf-core.esm.js?45ef:17)
    at Object.decodeWeights (tf-core.esm.js?45ef:17)
    at eval (tf-layers.esm.js?271e:17)
    at eval (tf-layers.esm.js?271e:17)
    at Object.eval [as next] (tf-layers.esm.js?271e:17)
    at o (tf-layers.esm.js?271e:17)


Shanqing Cai

unread,
Aug 31, 2018, 8:17:12 PM8/31/18
to TensorFlow.js Discussion
We have seen similar errors related to mismatch between the version of keras used to generate the saved h5 file and the version of keras that tensorflowjs depends on. 

Can you let me know what version of keras you used to train and save your keras model? 

Timon Lukas

unread,
Sep 1, 2018, 5:09:51 AM9/1/18
to TensorFlow.js Discussion
I am using Keras v2.2.2, which is the latest version available on conda. I'm training and saving the model on Windows with Python 3.6.6 :)

Shanqing Cai

unread,
Sep 2, 2018, 12:37:28 PM9/2/18
to TensorFlow.js Discussion
Thank for the reply, Timon.

I downloaded the "model.json" and "group1-shard1of1" files you kindly provided and tested loading them using "tf.loadModel". I used @tensorflow/tfjs version 0.12.6. The loading seems to work without any error in both the browser and node.js (with tfjs-node 0.1.14). Here is what model.summary() prints after the model is done loading:

_________________________________________________________________
Layer (type)                 Output shape              Param #   
=================================================================
lstm_1 (LSTM)                [30,5,32]                 4480      
_________________________________________________________________
dropout_1 (Dropout)          [30,5,32]                 0         
_________________________________________________________________
lstm_2 (LSTM)                [30,32]                   8320      
_________________________________________________________________
dropout_2 (Dropout)          [30,32]                   0         
_________________________________________________________________
dense_1 (Dense)              [30,3]                    99        
=================================================================
Total params: 12899
Trainable params: 12899
Non-trainable params: 0
_________________________________________________________________

I wonder what is different between your setup and mine. Could you be using an out-of-date version of @tensorflow/tfjs?

Best,
Shanqing

Timon Lukas

unread,
Sep 3, 2018, 2:50:54 AM9/3/18
to TensorFlow.js Discussion
Thank you for your reply and testing, Shanqing!

This is very strange - I also use @tensorflow/tfjs with version 0.12.6. I am using Webpack to build my frontend (which tfjs is a part of), could that be the problem? Have you imported tfjs directly or through building it with webpack?

Shanqing Cai

unread,
Sep 3, 2018, 10:15:31 AM9/3/18
to Timon Lukas, TensorFlow.js Discussion
I haven't tried tensorflow.js with webpack before. Others - please chime in if you have experience in that regard.

Timon - Can you check in the "network" tab of your browser's devtools? The tf.loadModel call should issue two HTTP/HTTPS requests for your model, one for the model.json and one for the binary weights file (group1-shard1of1). Can you check their contents are correct, i.e., the same as the files as seen on your local file system?

--
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+uns...@tensorflow.org.
Visit this group at https://groups.google.com/a/tensorflow.org/group/tfjs/.


--
---
Shanqing Cai
Software Engineer
Google

Timon Lukas

unread,
Sep 3, 2018, 1:34:22 PM9/3/18
to TensorFlow.js Discussion, timon....@gmail.com
Hey!

So I have tried around a little more. I was able to successfully load the file in a clean environment using TFJS directly, in Node and using Webpack. I have no idea why it did not work in the specific environment, but I suspect that something was wong with sending the data. I will further investigate and come back to you, but if you don't hear anything from me, it seems to work. Thank you!
Reply all
Reply to author
Forward
0 new messages