-
Notifications
You must be signed in to change notification settings - Fork 2k
Closed
Labels
Description
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.