Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

converter breaks on basic lstm #8489

Open
nevakrien opened this issue Jan 9, 2025 · 1 comment
Open

converter breaks on basic lstm #8489

nevakrien opened this issue Jan 9, 2025 · 1 comment
Assignees
Labels
stat:awaiting response type:bug Something isn't working

Comments

@nevakrien
Copy link

so i am on ubuntu 20 and i have this in my html file i am opening on firefox with the fillowing link https://cdn.jsdelivr.net/npm/@tensorflow/tfjs

i then run model = await tf.loadLayersModel('js_model/model.json'); which causes the error
"Initialization error: An InputLayer should be passed either abatchInputShapeor aninputShape."

the model i am runing is a stupidly simple lstm all native tensorflow. runing conversion from both h5 and keras seems to work at first but then it gives that crush.

the full model code

vocab_size = len(vectorizer)  # Number of unique words in your vocabulary

embedding_dim = 340

seq_length = 340  # Max sequence length

num_classes = 1



inputs = Input(shape=(seq_length,))

embedding = Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=seq_length)(inputs)

cnn = Conv1D(filters=128, kernel_size=5, activation='relu', padding='same')(embedding)

cnn = BatchNormalization()(cnn)

cnn = MaxPooling1D(pool_size=4)(cnn)

cnn = Conv1D(filters=256, kernel_size=3, activation='relu', padding='same')(cnn)

cnn = BatchNormalization()(cnn)

cnn = MaxPooling1D(pool_size=2)(cnn)

lstm = LSTM(128, return_sequences=True, dropout=0.3, recurrent_dropout=0.3)(cnn)

lstm = LSTM(64, return_sequences=False, dropout=0.3, recurrent_dropout=0.3)(lstm)

dense = Dense(256, activation='relu')(lstm)

dense = Dropout(0.5)(dense)

dense = Dense(128, activation='relu')(dense)

dense = Dropout(0.5)(dense)

dense = Dense(64, activation='relu')(dense)

outputs = Dense(num_classes, activation='sigmoid')(dense)



model = Model(inputs=inputs, outputs=outputs)

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

model.summary()
@nevakrien nevakrien added the type:bug Something isn't working label Jan 9, 2025
@shmishra99 shmishra99 self-assigned this Jan 13, 2025
@shmishra99
Copy link
Contributor

Hi @nevakrien ,

This is a known issue with TFJS currently. Tfjs-converter does not support models created by Keras 3. When you try to load this model using model = await tf.loadLayersModel('js_model/model.json'), it will attempt to infer the input shape which is not present in the model.json file.

You can install Keras 2 (TensorFlow 2.15.0) to create your model and then use the tfjs-converter to convert the TensorFlow model to a TFJS model. Or alternatively, you can load your current model as a Graph Model instead of a Layer Model using const model = await tf.loadGraphModel(modelUrl);.

Let me know if it helps. Thank You!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting response type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants