Look at this code and don't review it: import torch import torch.nn as nn from…
- User
Look at this code and don't review it: import torch import torch.nn as nn from torch.utils.data import DataLoader, TensorDataset from tqdm import tqdm import matplotlib.pyplot as plt import string import json import os
Define parameters and directory paths
DATASET_CHUNKS = 10240 # New hyperparameter to control dataset size (number of chunks) SEQUENCE_LENGTH = 64 BATCH_SIZE = 512 HIDDEN_SIZE = 384 NUM_LAYERS = 2 LEARNING_RATE = 0.001 NUM_EPOCHS = 2 GRAD_CLIP = 5.0 # Gradient clipping to prevent exploding gradients CHECKPOINT_FOLDER = "checkpoints" os.makedirs(CHECKPOINT_FOLDER, exist_ok=True) model_name = "Talk-model-1"
Replace ‘your_file.txt’ with the path to your actual text file.
file_path = "Long-Run-Talk-Cutdown.txt"
Print the start of data preparation:
print("\n--- Starting data preparation ---")
Read the file and filter out blank lines
with open(file_path, "r", encoding="utf-8") as file: lines = file.readlines() text = "".join(line for line in lines if not line.isspace())
Generate vocabulary with all printable ASCII characters except the non-printable ones
excluded_characters = "\x0b\x0c\n\r\t\x0b\x0c" # Explicitly specify which characters to exclude vocab_chars = "".join(c for c in string.printable if c not in excluded_characters) VOCAB_SIZE = len(vocab_chars) # Update the size of the vocabulary
Map each character to a unique index
char_to_index = {char: idx for idx, char in enumerate(vocab_chars)}
Convert the text to indices using the char_to_index mapping
ascii_indices = [char_to_index[c] for c in text if c in char_to_index]
Calculate the total number of sequences based on the sequence length
total_num_sequences = len(ascii_indices) - SEQUENCE_LENGTH
Calculate the number of sequences to use per chunk,
so that it divides evenly for all chunks
sequences_per_chunk = total_num_sequences // DATASET_CHUNKS usable_sequences = sequences_per_chunk * DATASET_CHUNKS
inputs, targets = [], [] for i in range(0, usable_sequences, SEQUENCE_LENGTH): input_seq = ascii_indices[i:i + SEQUENCE_LENGTH] target_seq = ascii_indices[i + 1:i + SEQUENCE_LENGTH + 1] inputs.append(torch.tensor(input_seq, dtype=torch.long)) targets.append(torch.tensor(target_seq, dtype=torch.long))
input_tensor = torch.stack(inputs) target_tensor = torch.stack(targets) print(f"Created input tensor: {input_tensor.shape}") print(f"Created target tensor: {target_tensor.shape}")
Load data into DataLoader for batch processing
dataset = TensorDataset(input_tensor, target_tensor) data_loader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True) print("\n--- Data preparation complete ---")
Define the sophisticated RNN model architecture
- Assistant
Understood. If you have any specific questions or need assistance with the code you've provided or a related topic, please feel free to ask!
- User
- Assistant