do what ever you can to make this faster, this currently takes more than a day to…
- User
do what ever you can to make this faster, this currently takes more than a day to complete. my memory is at a 100 and cpu at 60 precent if that helps.
import numpy as np import gc import os.path from keras.models import Sequential, load_model from keras.layers import LSTM, Dense from keras import backend as K
Read and preprocess the dataset
def read_dataset(file_path): with open(file_path, "r", encoding="utf-8") as file: lines = file.read().split("\n") text = " ".join(lines) # Concatenate all lines into a single string
codeprint("Loaded {} characters.".format(len(text))) # Create a set of unique characters and map them to numerical indices chars = sorted(list(set(text))) char_to_idx = {char: idx for idx, char in enumerate(chars)} idx_to_char = {idx: char for idx, char in enumerate(chars)} return text, chars, char_to_idx, idx_to_chardef prepare_sequences(text, seq_length, char_to_idx): sequences = [] targets = [] for i in range(len(text) - seq_length): sequence = text[i:i + seq_length] target = text[i + seq_length] sequences.append([char_to_idx[char] for char in sequence]) targets.append(char_to_idx[target]) print(f"Processed sequence {i}/{len(text) - seq_length}")
codeprint("Sequences preparation completed.") return np.array(sequences), np.array(targets)Model configuration
seq_length = 10 hidden_units = 128 print("Configuration loaded.")
Read and preprocess the dataset
print("Reading dataset…") file_path = "rockyou.txt" text, chars, char_to_idx, idx_to_char = read_dataset(file_path) print(f"[{file_path}] Dataset loaded.")
Prepare training sequences and targets
print("Preparing training sequences…") sequences, targets = prepare_sequences(text, seq_length, char_to_idx)
Clear Keras session and release memory
print("Clearing Keras session…") K.clear_session() print("Garbage collecting…") gc.collect()
Check if model already exists
print("Checking if model exists…") model_path = "model.h5" if os.path.isfile(model_path): print("Loading existing model…") model = load_model(model_path) else: # Build the model print("Building the model…") model = Sequential() model.add(LSTM(hidden_units, input_shape=(seq_length,))) model.add(Dense(len(chars), activation="softmax"))
Configure training parameters
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy")
- Assistant
To make the code run faster, you can try the following optimizations:
-
Use a smaller sequence length: Decrease the value of
seq_lengthto reduce the number of sequences and targets generated. This will reduce the overall training time. -
Use a smaller hidden unit size: Decrease the value of
hidden_unitsto reduce the complexity and computational requirements of the LSTM layer. -
Use a larger batch size: Increase the value of the
batch_sizeparameter in themodel.fit()function. This will allow the model to process more sequences in parallel and potentially speed up training. -
Use GPU acceleration: If your system has a compatible GPU, consider enabling GPU acceleration for training the model. This can significantly improve training speed. You can refer to the Keras documentation for instructions on how to set up GPU acceleration.
-