I am trying to generate text using an RNN from my custom dataset of tweets. i have trained the model on colab and i am trying to call the saved model on my windows pc but i get this error:
Traceback (most recent call last):
File "I:\PROGRAMMING\currentProjects\PROJECT-FRNN\CODE\genText.py", line 108, in <module>
oneStep2()
File "I:\PROGRAMMING\currentProjects\PROJECT-FRNN\CODE\genText.py", line 99, in oneStep2
next_char, states = one_step_model.generate_one_step(next_char, states=states)
File "I:\PROGRAMMING\currentProjects\PROJECT-FRNN\venv\lib\site-packages\tensorflow\python\util\traceback_utils.py", line 153, in error_handler
raise e.with_traceback(filtered_tb) from None
File "I:\PROGRAMMING\currentProjects\PROJECT-FRNN\venv\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1129, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
File "I:\PROGRAMMING\currentProjects\PROJECT-FRNN\CODE\genText.py", line 71, in generate_one_step *
predicted_logits, states = self.model(inputs=input_ids, states=states,
File "I:\PROGRAMMING\currentProjects\PROJECT-FRNN\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler **
raise e.with_traceback(filtered_tb) from None
ValueError: Exception encountered when calling layer "my_model" (type MyModel).
Could not find matching concrete function to call loaded from the SavedModel. Got:
Positional arguments (4 total):
* Tensor("inputs:0", shape=(1, None), dtype=int64)
* None
* True
* False
Keyword arguments: {}
Expected these arguments to match one of the following 4 option(s):
Option 1:
Positional arguments (4 total):
* TensorSpec(shape=(None, 100), dtype=tf.int64, name='inputs')
* None
* False
* False
Keyword arguments: {}
Option 2:
Positional arguments (4 total):
* TensorSpec(shape=(None, 100), dtype=tf.int64, name='inputs')
* None
* False
* True
Keyword arguments: {}
Option 3:
Positional arguments (4 total):
* TensorSpec(shape=(None, 100), dtype=tf.int64, name='input_1')
* None
* False
* False
Keyword arguments: {}
Option 4:
Positional arguments (4 total):
* TensorSpec(shape=(None, 100), dtype=tf.int64, name='input_1')
* None
* False
* True
Keyword arguments: {}
Call arguments received:
• args=('tf.Tensor(shape=(1, None), dtype=int64)',)
• kwargs={'states': 'None', 'return_state': 'True', 'training': 'None'}
What i expected to happen was for it to print about 100 - 150 characters, that would be what the ai has generated. I have looked on stackoverflow, tensorflow's documentation and google for help and i could not find anything helpful. if someone could help and / or point me in the right direction to fix this would be a big help! The code that generated this traceback is below:
def oneStep2():
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers.experimental import preprocessing
import time
print("TensorFlow Version: ", str(tf.__version__))
path_to_file = "I:\\PROGRAMMING\\currentProjects\\PROJECT-FRNN\\TrainingData.txt"
text = open(path_to_file, 'rb').read().decode(encoding='utf-8')
vocab = sorted(set(text))
ids_from_chars = preprocessing.StringLookup(
vocabulary=list(vocab), mask_token=None)
chars_from_ids = tf.keras.layers.experimental.preprocessing.StringLookup(
vocabulary=ids_from_chars.get_vocabulary(), invert=True, mask_token=None)
# Define a simple sequential model
def create_model():
model = tf.keras.models.Sequential([
keras.layers.Dense(512, activation='relu'),
keras.layers.Dropout(0.2),
keras.layers.Dense(10)
])
model.compile(optimizer='adam',
loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tf.metrics.SparseCategoricalAccuracy()])
return model
filepath = "I:\\PROGRAMMING\\currentProjects\\PROJECT-FRNN\\PROJECT_FRNN_MODEL"
# Create a basic model instance
model = create_model()
# Display the model's architecture
model = tf.keras.models.load_model(filepath)
model.summary()
class OneStep(tf.keras.Model):
def __init__(self, model, chars_from_ids, ids_from_chars, temperature=1.0):
super().__init__()
self.temperature = temperature
self.model = model
self.chars_from_ids = chars_from_ids
self.ids_from_chars = ids_from_chars
# Create a mask to prevent "[UNK]" from being generated.
skip_ids = self.ids_from_chars(['[UNK]'])[:, None]
sparse_mask = tf.SparseTensor(
# Put a -inf at each bad index.
values=[-float('inf')] * len(skip_ids),
indices=skip_ids,
# Match the shape to the vocabulary
dense_shape=[len(ids_from_chars.get_vocabulary())])
self.prediction_mask = tf.sparse.to_dense(sparse_mask)
@tf.function
def generate_one_step(self, inputs, states=None):
# Convert strings to token IDs.
input_chars = tf.strings.unicode_split(inputs, 'UTF-8')
input_ids = self.ids_from_chars(input_chars).to_tensor()
# Run the model.
# predicted_logits.shape is [batch, char, next_char_logits]
predicted_logits, states = self.model(inputs=input_ids, states=states,
return_state=True)
# Only use the last prediction.
predicted_logits = predicted_logits[:, -1, :]
predicted_logits = predicted_logits / self.temperature
# Apply the prediction mask: prevent "[UNK]" from being generated.
predicted_logits = predicted_logits + self.prediction_mask
# Sample the output logits to generate token IDs.
predicted_ids = tf.random.categorical(predicted_logits, num_samples=1)
predicted_ids = tf.squeeze(predicted_ids, axis=-1)
# Convert from token ids to characters
predicted_chars = self.chars_from_ids(predicted_ids)
# Return the characters and model state.
return predicted_chars, states
one_step_model = OneStep(model, chars_from_ids, ids_from_chars)
start = time.time()
states = None
next_char = tf.constant(['ROMEO:'])
result = [next_char]
for n in range(100):
next_char, states = one_step_model.generate_one_step(next_char, states=states)
result.append(next_char)
result = tf.strings.join(result)
end = time.time()
print(result[0].numpy().decode('utf-8'), '\n\n' + '_' * 80)
print('\nRun time:', end - start)
oneStep2()