tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.

Home Page:https://js.tensorflow.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Duplicate weight problem

OOFMAN29803 opened this issue · comments

here is my code that is apparently having duplicate weight problems when converting. this also happens in script and out

import tensorflow as tf
import numpy as np
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflowjs import converters

# Load your text data
text_data = "I am a chicken nugget that likes food and I ate a monkey yesterday, it was actually pretty good and I enjoyed it a lot, hey speaking of that, how is your new iPhone 11 going, I hope it is well"

# Tokenization and sequence generation
tokenizer = Tokenizer(char_level=True, lower=True)
tokenizer.fit_on_texts([text_data])
sequences = tokenizer.texts_to_sequences([text_data])[0]

# Prepare input and output sequences
sequence_length = 100
input_sequences = []
output_sequences = []

for i in range(len(sequences) - sequence_length):
  input_sequences.append(sequences[i:i + sequence_length])
  output_sequences.append(sequences[i + sequence_length])

input_sequences = np.array(pad_sequences(input_sequences, maxlen=sequence_length, padding="post"))
output_sequences = np.array(output_sequences)

# Define the model using the Sequential API
vocab_size = len(tokenizer.word_index) + 1

model = Sequential([
  Embedding(vocab_size, 16, input_shape=(sequence_length,), name='embedding_layer'),
  LSTM(128, dropout=0.2, recurrent_dropout=0.2, name='lstm_layer'),
  Dense(vocab_size, activation="softmax", name='dense_layer')
])

model.compile(loss="sparse_categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
model.summary()

# Training the model
epochs = 80
batch_size = 128
model.fit(input_sequences, output_sequences, epochs=epochs, batch_size=batch_size)

# Convert the model to TensorFlow.js format
converters.save_keras_model(model, 'tfjs_model')

# Create a reverse word index
reverse_word_index = dict([(value, key) for (key, value) in tokenizer.word_index.items()])

# Function to generate text
def generate_text(seed_text, model, tokenizer, sequence_length, num_chars_to_generate):
  generated_text = seed_text

  for _ in range(num_chars_to_generate):
    token_list = tokenizer.texts_to_sequences([generated_text])[0]
    token_list = pad_sequences([token_list], maxlen=sequence_length, padding="pre")
    predicted_probs = model.predict(token_list, verbose=0)
    predicted_token = np.argmax(predicted_probs, axis=-1)[0]

    output_char = reverse_word_index.get(predicted_token, '')
    generated_text += output_char

  return generated_text

# Generate text
seed_text = "What is a nugget?"
generated_text = generate_text(seed_text, model, tokenizer, sequence_length, 800)
print(generated_text)

I have used Google Cloud Shell and Kaggle and nothing works at this time

Tensorflow - 2.16.1
Tensorflowjs - 4.17

I always get errors like this:

/usr/bin/python /home/meoof2010/interact.py
2024-04-16 02:05:40.812208: I external/local_tsl/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-04-16 02:05:40.818757: I external/local_tsl/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-04-16 02:05:40.893898: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-04-16 02:05:42.141769: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
/usr/local/lib/python3.9/dist-packages/keras/src/layers/core/embedding.py:81: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
  super().__init__(**kwargs)
Model: "sequential"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type)                         ┃ Output Shape                ┃         Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ embedding_layer (Embedding)          │ (None, 100, 16)             │             400 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ lstm_layer (LSTM)                    │ (None, 128)                 │          74,240 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_layer (Dense)                  │ (None, 25)                  │           3,225 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
 Total params: 77,865 (304.16 KB)
 Trainable params: 77,865 (304.16 KB)
 Non-trainable params: 0 (0.00 B)
Epoch 1/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 2s 2s/step - accuracy: 0.0543 - loss: 3.2190
Epoch 2/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 239ms/step - accuracy: 0.2174 - loss: 3.2131
Epoch 3/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 293ms/step - accuracy: 0.2174 - loss: 3.2067
Epoch 4/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 308ms/step - accuracy: 0.2174 - loss: 3.1991
Epoch 5/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 303ms/step - accuracy: 0.2174 - loss: 3.1888
Epoch 6/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 160ms/step - accuracy: 0.2174 - loss: 3.1726
Epoch 7/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 310ms/step - accuracy: 0.2174 - loss: 3.1477
Epoch 8/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 304ms/step - accuracy: 0.2174 - loss: 3.0994
Epoch 9/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 298ms/step - accuracy: 0.2174 - loss: 3.0179
Epoch 10/10
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 303ms/step - accuracy: 0.2174 - loss: 2.8978
WARNING:absl:You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`. 
failed to lookup keras version from the file,
    this is likely a weight only file
Traceback (most recent call last):
  File "/home/meoof2010/interact.py", line 47, in <module>
    converters.save_keras_model(model, 'tfjs_model')
  File "/usr/local/lib/python3.9/dist-packages/tensorflowjs/converters/keras_h5_conversion.py", line 467, in save_keras_model
    write_artifacts(
  File "/usr/local/lib/python3.9/dist-packages/tensorflowjs/converters/keras_h5_conversion.py", line 418, in write_artifacts
    weights_manifest = write_weights.write_weights(
  File "/usr/local/lib/python3.9/dist-packages/tensorflowjs/write_weights.py", line 118, in write_weights
    _assert_no_duplicate_weight_names(weight_groups)
  File "/usr/local/lib/python3.9/dist-packages/tensorflowjs/write_weights.py", line 346, in _assert_no_duplicate_weight_names
    raise Exception(
Exception: Error dumping weights, duplicate weight name kernel

i have been trying for a week, and nothing is working.

Hi, @OOFMAN29803

Thank you for bringing this issue to our attention and It's known issue with Tensorflow version 2.16.1 because latest version of Tensorflow.js downloading Tensorflow version 2.16.1 and it seems like tfjs_converter is not working as expected so at the moment temporary workaround is to downgrade Tensorflow version to 2.15.0 after installing Tensorflow.js pip install tensorflowjs by using this command pip install tensorflow==2.15.0, please give it try from your end with temporary workaround and see is it working as expected or not ?

If issue still persists after downgrading the Tensorflow version to 2.15.0 after installing latest version of TensorFlow.js 4.17.0 please let us know with error log to investigate this issue further from our end.

Meanwhile our developer team will work on this issue to make tfjs_converter compatible with Tensorflow version 2.16.1 in near future.

EDIT : I have tried from my end and after downgrading the TensorFlow version to 2.15.0 tfjs_converter is working as expected for your reference I have added gist-file

If you face any error w.r.t version compatibility between tensorflow-decision-forests and TensorFlow 2.15.0 then please downgrade tensorflow-decision-forests version to 1.8.1 by using pip install tensorflow-decision-forests==1.8.1 command and tfjs_converter should work as expected.

Thank you for your cooperation and patience

Hi, @OOFMAN29803

To confirm, were you able to successfully run your code by following the provided instructions and workaround? If the code is running as expected after downgrading the TensorFlow version to 2.15.0 after installing the latest TensorFlow.js , please feel free to close this issue.

If you're still encountering problems, please provide us with a new error log after attempting the instructions and workaround. This will help us investigate the issue further on our end.

Thank you for your cooperation and patience in resolving this matter.

Hey sorry for the late response I have been training the AI and as far as I am concerned, it's working very well I think

Hi, @OOFMAN29803

No problem, I'm happy to hear the issue has been resolved. If you don't require further assistance, please don't hesitate to close this issue now.

Thank you for your understanding and patience.

Hi, @OOFMAN29803

I haven't got any confirmation from your end but after downgrading the TensorFlow version to 2.15.0, tfjs_converter is working as expected for your reference I have added gist-file but your issue still persists please feel free to post your comment with new error log so I'll go ahead and re-open this issue again for further assistance, At the moment I'm closing this issue.

Thank you for your cooperation and patience.

Are you satisfied with the resolution of your issue?
Yes
No

Yes I am so sorry, a lot has been going on in my life, I've barely looked at any emails and stuff so I did get it working but I have decided to drop it and go for a cloud AI instead using Google cloud functions etc