Updated to load on new keras version

This commit is contained in:
2026-07-08 18:17:42 +00:00
parent b194af18b4
commit 6d35747bcc
+17 -4
View File
@@ -23,6 +23,7 @@ configure_xla_paths()
import tensorflow as tf import tensorflow as tf
import numpy as np import numpy as np
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
# ========================================== # ==========================================
# 1. MODEL ARCHITECTURE # 1. MODEL ARCHITECTURE
@@ -87,7 +88,12 @@ def produce_sample(model, vectorize_layer, vocab, seed, num_generate=300, temper
# 1. Safely tokenize the seed character-by-character using the vectorizer # 1. Safely tokenize the seed character-by-character using the vectorizer
# We split the string into individual characters so the vectorizer processes them properly # We split the string into individual characters so the vectorizer processes them properly
seed_chars = tf.strings.unicode_split(seed, input_encoding='UTF-8') seed_chars = tf.strings.unicode_split(seed, input_encoding='UTF-8')
input_ids = vectorize_layer(seed_chars).numpy().tolist()
input_ids = tf.squeeze(vectorize_layer(seed_chars))
if input_ids.ndim == 0: # Safeguard if the seed is a single character
input_ids = tf.expand_dims(input_ids, 0)
#input_ids = vectorize_layer(seed_chars).numpy().tolist()
generated_ids = [] generated_ids = []
states = None states = None
@@ -151,11 +157,12 @@ def main():
DEFAULT_NUM_LAYERS = 3 DEFAULT_NUM_LAYERS = 3
DEFAULT_DROPOUT_RATE = 0.4 DEFAULT_DROPOUT_RATE = 0.4
TEXT_FILE = "emily_post.txt" #TEXT_FILE = "emily_post.txt"
TEXT_FILE = "combined_training_data.txt"
VOCAB_FILE = "vocab.txt" VOCAB_FILE = "vocab.txt"
SEQ_LENGTH = 512 SEQ_LENGTH = 512
BATCH_SIZE = 128 BATCH_SIZE = 128
EPOCHS_PER_RUN = 20 EPOCHS_PER_RUN = 22
BUFFER_SIZE = 10000 BUFFER_SIZE = 10000
SEED_TEXT = "In the case of unlawful enterance" SEED_TEXT = "In the case of unlawful enterance"
@@ -208,11 +215,16 @@ def main():
# Initialize model dynamically using the determined metrics # Initialize model dynamically using the determined metrics
model = CharacterTextModel(vocab_size=vocab_size, embedding_dim=emb_dim, rnn_units=rnn_units, num_layers=num_layers, dropout_rate=dropout_rate) model = CharacterTextModel(vocab_size=vocab_size, embedding_dim=emb_dim, rnn_units=rnn_units, num_layers=num_layers, dropout_rate=dropout_rate)
model.build(input_shape=(BATCH_SIZE, SEQ_LENGTH))
dummy_input = tf.zeros((BATCH_SIZE, SEQ_LENGTH), dtype=tf.int32)
_ = model(dummy_input, training=False)
#model.build(input_shape=(BATCH_SIZE, SEQ_LENGTH))
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer='adam', loss=loss_fn) model.compile(optimizer='adam', loss=loss_fn)
if is_resuming: if is_resuming:
print(f"[Weights] Restoring model layer states from: {args.modelfile}") print(f"[Weights] Restoring model layer states from: {args.modelfile}")
model.load_weights(args.modelfile) model.load_weights(args.modelfile)
@@ -271,6 +283,7 @@ def main():
total_epochs = initial_epoch + EPOCHS_PER_RUN total_epochs = initial_epoch + EPOCHS_PER_RUN
print(f"[Train] Commencing training loop inside checkpoint tree: {checkpoint_dir}") print(f"[Train] Commencing training loop inside checkpoint tree: {checkpoint_dir}")
print(f"[Train] Running Epochs {initial_epoch + 1} through {total_epochs}...") print(f"[Train] Running Epochs {initial_epoch + 1} through {total_epochs}...")
print(f"[DEBUG] Verification -> initial_epoch: {initial_epoch}, total_epochs: {total_epochs}")
model.fit( model.fit(
dataset, dataset,