Kindly resolve this error: [code] TypeError Traceback (most recent call last)…
- User
Kindly resolve this error:
# Imports import os import json import numpy as np import torch import GPyOpt import time import math import torch.nn as nn import torch.optim as optim from torch.nn import functional as F from sklearn.cluster import KMeans from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction # Define hyperparameters batch_size = 64 # Number of samples processed before the model is updated block_size = 256 # Length of sequences processed by the model max_iters = 5000 # Maximum number of training iterations eval_interval = 500 # Interval at which the model is evaluated learning_rate = 3e-4 # Learning rate for the optimizer device = 'cuda' if torch.cuda.is_available() else 'cpu' # Use GPU if available eval_iters = 200 # Number of iterations for evaluation n_embd = 384 # Dimension of the embeddings n_head = 6 # Number of attention heads n_layer = 6 # Number of transformer layers dropout = 0.2 # Dropout rate to prevent overfitting n_clusters = 4 # Number of clusters in the MoE layer experts_per_cluster = 2 # Number of experts per cluster aux_loss_weight = 0.01 # Weight of auxiliary loss from MoE mechanism # Set random seed for reproducibility torch.manual_seed(1337) # Load data and define data processing functions with open('input.txt', 'r', encoding='utf-8') as f: text = f.read() # Create a character-level vocabulary chars = sorted(list(set(text))) vocab_size = len(chars) # Create mappings from characters to indices and vice versa stoi = {ch: i for i, ch in enumerate(chars)} itos = {i: ch for i, ch in enumerate(chars)} # Encode and decode functions to convert between text and indices encode = lambda s: [stoi[c] for c in s] decode = lambda l: ''.join([itos[i] for i in l]) # Convert entire text into a tensor of indices data = torch.tensor(encode(text), dtype=torch.long) # Split the data into training and validation sets n = int(0.9 * len(data)) train_data = data[:n] val_data = data[n:] # Function to generate a batch of data for training or validation def get_batch(split): data = train_data if split == 'train' else val_data ix = torch.randint(len(data) - block_size, (batch_size,)) x = torch.stack([data[i:i + block_size] for i in ix]) y = torch.stack([data[i + 1:i + block_size + 1] for i in ix]) x, y = x.to(device), y.to(device) return x, y # Function to estimate the model's loss on training and validation data @torch.no_grad() def estimate_loss(model): out = {} model.eval() # Set model to evaluation mode for split in ['train', 'val']: total_loss = 0.0 total_ce_loss = 0.0 total_aux_loss = 0.0 for k in range(eval_iters): X, Y = get_batch(split) # Unpack all outputs from the model logits, loss, ce_loss, aux_loss_value = model(X, Y) total_loss += loss.item() total_ce_loss += ce_loss.item() total_aux_loss += aux_loss_value.item() if aux_loss_value is not None else 0 avg_loss = total_loss / eval_iters avg_ce_loss = total_ce_loss / eval_iters avg_aux_loss = total_aux_loss / eval_iters out[split] = { 'avg_loss': avg_loss, 'avg_ce_loss': avg_ce_loss, 'avg_aux_loss': aux_loss_weight * avg_aux_loss } model.train() # Set model back to training mode return out def compute_metrics(model): model.eval() total_bleu = 0.0 correct_predictions = 0 total_tokens = 0 with torch.no_grad(): for _ in range(eval_iters): inputs, targets = get_batch('val') inputs = inputs.to(device) targets = targets.to(device) logits, *_ = model(inputs, targets) # Flatten predictions and targets predictions = torch.argmax(logits, dim=-1) # Shape: (batch_size, seq_length) pred_list = predictions.view(-1).tolist() # Convert to list target_list = targets.view(-1).tolist() # Convert to list # BLEU Score computation total_bleu += sentence_bleu( [target_list], pred_list, smoothing_function=SmoothingFunction().method1 ) # Accuracy computation correct_predictions += sum(p == t for p, t in zip(pred_list, target_list)) total_tokens += len(target_list) # Compute final metrics avg_bleu = total_bleu / eval_iters accuracy = correct_predictions / total_tokens return {'bleu_score': avg_bleu, 'accuracy': accuracy} class LiquidExpert(nn.Module): def __init__(self, input_dim, output_dim, hidden_dim, tau=0.1): super(LiquidExpert, self).__init__() self.hidden_dim = hidden_dim self.tau = tau # Weights and layers self.W = nn.Parameter(torch.randn(hidden_dim, hidden_dim) * 0.1) self.U = nn.Parameter(torch.randn(hidden_dim, input_dim) * 0.1) self.b = nn.Parameter(torch.zeros(hidden_dim)) self.output_layer = nn.Linear(hidden_dim, output_dim) def forward(self, x): B, T, _ = x.shape h = torch.zeros(B, self.hidden_dim, device=x.device) # Initial hidden state for t in range(T): # Process each timestep u_t = x[:, t, :] # Input at timestep t h_dot = -h + torch.tanh(self.W @ h.T + self.U @ u_t.T + self.b.unsqueeze(1)) h = h + self.tau * h_dot.T output = self.output_layer(h) return output class ClusteredSwitchLiquid(nn.Module): def __init__(self, n_clusters, experts_per_cluster, n_embd, output_dim): super().__init__() self.n_clusters = n_clusters self.experts_per_cluster = experts_per_cluster self.total_experts = n_clusters * experts_per_cluster self.n_embd = n_embd # Initialize experts self.experts = nn.ModuleList([ LiquidExpert(n_embd, output_dim, hidden_dim=64) for _ in range(self.total_experts) ]) # Assign experts to clusters self.cluster_experts = { cluster_id: self.experts[ cluster_id * experts_per_cluster:(cluster_id + 1) * experts_per_cluster ] for cluster_id in range(n_clusters) } def assign_tokens_to_clusters(self, x): B, T, D = x.shape cluster_assignments = torch.arange(T, device=x.device) % self.n_clusters return cluster_assignments def forward(self, x): B, T, D = x.shape cluster_assignments = self.assign_tokens_to_clusters(x) outputs_per_cluster = [] for cluster_id in range(self.n_clusters): assigned_indices = (cluster_assignments == cluster_id).nonzero(as_tuple=True)[0] if len(assigned_indices) == 0: continue cluster_inputs = x[:, assigned_indices, :] experts = self.cluster_experts[cluster_id] # Split tokens evenly across cluster even_split_inputs = torch.chunk(cluster_inputs, len(experts), dim=1) cluster_outputs = [] for idx, expert in enumerate(experts): cluster_outputs.append(expert(even_split_inputs[idx])) combined_cluster_output = sum(cluster_outputs) outputs_per_cluster.append(combined_cluster_output) if outputs_per_cluster: final_output = torch.cat(outputs_per_cluster, dim=1) else: final_output = torch.zeros(B, T, D, device=x.device) return final_output class LiquidHead(nn.Module): """One head of self-attention with liquid dynamics.""" def __init__(self, head_size, dropout): super().__init__() self.key = nn.Linear(n_embd, head_size, bias=False) self.query = nn.Linear(n_embd, head_size, bias=False) self.value = nn.Linear(n_embd, head_size, bias=False) self.register_buffer('tril', torch.tril(torch.ones(block_size, block_size))) self.dropout = nn.Dropout(dropout) # Liquid neural dynamics parameters self.alpha_k = nn.Parameter(torch.randn(head_size)) self.beta_k = nn.Parameter(torch.randn(head_size)) self.alpha_q = nn.Parameter(torch.randn(head_size)) self.beta_q = nn.Parameter(torch.randn(head_size)) def forward(self, x): B, T, C = x.shape k = self.key(x) q = self.query(x) k = self.alpha_k * k + self.beta_k * torch.tanh(k) q = self.alpha_q * q + self.beta_q * torch.tanh(q) wei = q @ k.transpose(-2, -1) * C**-0.5 wei = wei.masked_fill(self.tril[:T, :T] == 0, float('-inf')) wei = F.softmax(wei, dim=-1) wei = self.dropout(wei) v = self.value(x) out = wei @ v return out class MultiHeadAttention(nn.Module): """Multiple heads of self-attention in parallel.""" def __init__(self, num_heads, head_size, dropout): super().__init__() self.heads = nn.ModuleList([LiquidHead(head_size, dropout) for _ in range(num_heads)]) self.proj = nn.Linear(head_size * num_heads, n_embd) self.dropout = nn.Dropout(dropout) def forward(self, x): out = torch.cat([h(x) for h in self.heads], dim=-1) out = self.dropout(self.proj(out)) return out class Block_ClusteredSwitch(nn.Module): """Transformer block with Clustered Mixture-of-Experts FeedForward layer.""" def __init__(self, n_embd, n_head, dropout): super().__init__() self.ln1 = nn.LayerNorm(n_embd) self.ln2 = nn.LayerNorm(n_embd) self.sa = MultiHeadAttention(n_head, (n_embd // n_head), dropout) # Calculate total_experts here total_experts = n_clusters * experts_per_cluster self.ffwd = ClusteredSwitchLiquid(n_clusters, total_experts, n_embd, dropout) def forward(self, x): x = x + self.sa(self.ln1(x)) x = self.ffwd(self.ln2(x)) return x class Clustered_ST(nn.Module): def __init__(self, n_embd, n_head, n_layer, dropout): super().__init__() self.token_embedding_table = nn.Embedding(vocab_size, n_embd) self.position_embedding_table = nn.Embedding(block_size, n_embd) self.blocks = nn.ModuleList([ Block_ClusteredSwitch(n_embd, n_head, dropout) for _ in range(n_layer) ]) self.ln_f = nn.LayerNorm(n_embd) self.lm_head = nn.Linear(n_embd, vocab_size) self.apply(self._init_weights) def _init_weights(self, module): if isinstance(module, nn.Linear): torch.nn.init.normal_(module.weight, mean=0.0, std=0.02) if module.bias is not None: torch.nn.init.zeros_(module.bias) elif isinstance(module, nn.Embedding): torch.nn.init.normal_(module.weight, mean=0.0, std=0.02) def forward(self, idx, targets=None): B, T = idx.shape tok_emb = self.token_embedding_table(idx) pos_emb = self.position_embedding_table(torch.arange(T, device=idx.device)) x = tok_emb + pos_emb for block in self.blocks: x = block(x) x = self.ln_f(x) logits = self.lm_head(x) if targets is None: return logits, None, None, None else: B, T, C = logits.shape logits_flat = logits.view(B * T, C) targets_flat = targets.view(B * T) ce_loss = F.cross_entropy(logits_flat, targets_flat) return logits, ce_loss, ce_loss, None def generate(self, idx, max_new_tokens): for _ in range(max_new_tokens): idx_cond = idx[:, -block_size:] outputs = self(idx_cond) logits = outputs[0] logits = logits[:, -1, :] probs = F.softmax(logits, dim=-1) idx_next = torch.multinomial(probs, num_samples=1) idx = torch.cat((idx, idx_next), dim=1) return idx # Initialize the model and optimizer model = Clustered_ST(n_embd, n_head, n_layer, dropout).to(device) optimizer = optim.AdamW(model.parameters(), lr=learning_rate) start_time = time.time() # Training Loop for iter in range(max_iters + 1): if iter % eval_interval == 0: losses = estimate_loss(model) print( f"Step {iter}: train loss {losses['train']['avg_loss']:.4f}, " f"val loss {losses['val']['avg_loss']:.4f}" ) xb, yb = get_batch('train') logits, loss, *_ = model(xb, yb) optimizer.zero_grad(set_to_none=True) loss.backward() optimizer.step() elapsed_time = time.time() - start_time metrics = compute_metrics(model) print("\nTraining completed.") print(f"Elapsed time: {elapsed_time:.2f} seconds") print(f"Accuracy: {metrics['accuracy'] * 100:.2f}%") print(f"BLEU Score: {metrics['bleu_score'] * 100:.2f}%") - Assistant
- User
- Assistant
- User
Display full code of changes.
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
- User
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.