Please edit the function that loads the txt file so that it can load multiple files and…
- User
Please edit the function that loads the txt file so that it can load multiple files and merge their content into a single file to be passed for training, code: import tkinter as tk from tkinter import filedialog from keras.models import Sequential from keras.layers import Embedding, LSTM, Dense from keras.preprocessing.text import Tokenizer from keras.preprocessing.sequence import pad_sequences from keras.utils import to_categorical import numpy as np import os import pickle
Data loader and preprocessor function
def preprocess_data(file_path, sequence_length=5):
Initialize the tokenizer
tokenizer = Tokenizer()
Load your dataset
with open(file_path, 'r', encoding='utf-8') as file: text = file.read()
Split by lines or sentences (customize based on how your data is formatted)
sentences = text.split('\n')
Fit the tokenizer on the sentences
tokenizer.fit_on_texts(sentences)
Convert sentences to sequences
sequences = tokenizer.texts_to_sequences(sentences)
Flatten the list of sequences into one big sequence and create input-output pairs
big_sequence = [token for seq in sequences for token in seq] input_sequences, output_words = [], [] for i in range(0, len(big_sequence) - sequence_length): input_sequences.append(big_sequence[i:i + sequence_length]) output_words.append(big_sequence[i + sequence_length])
Pad the sequences
input_sequences = pad_sequences(input_sequences, maxlen=sequence_length)
Get the total number of unique tokens
vocab_size = len(tokenizer.word_index) + 1 # Plus 1 for zero padding
Convert output words to one-hot encoding
output_words = np.array(output_words) output_words = to_categorical(output_words, num_classes=vocab_size)
return input_sequences, output_words, vocab_size, tokenizer
Function to train and save the model
def train_model():
Get input values
code - Assistant