Example entry: {'instruction': 'Identify the correct spelling of the following word.', 'input': 'Ocassion', 'output': "The correct spelling is 'Occasion.'"}
# 一个辅助函数,用于构建 Prompt 部分(instruction + optional input) defformat_input(entry): # Instruction 文本(每条样本必须包含) instruction_text = ( f"Below is an instruction that describes a task. " f"Write a response that appropriately completes the request." f"\n\n### Instruction:\n{entry['instruction']}" )
defcalc_loss_batch(input_batch, target_batch, model, device): input_batch, target_batch = input_batch.to(device), target_batch.to(device) logits = model(input_batch) loss = torch.nn.functional.cross_entropy(logits.flatten(0, 1), target_batch.flatten()) return loss
defcalc_loss_loader(data_loader, model, device, num_batches=None): total_loss = 0. iflen(data_loader) == 0: returnfloat("nan") elif num_batches isNone: num_batches = len(data_loader) else: # Reduce the number of batches to match the total number of batches in the data loader # if num_batches exceeds the number of batches in the data loader num_batches = min(num_batches, len(data_loader)) for i, (input_batch, target_batch) inenumerate(data_loader): if i < num_batches: loss = calc_loss_batch(input_batch, target_batch, model, device) total_loss += loss.item() else: break return total_loss / num_batches
# Main training loop for epoch inrange(num_epochs): model.train() # Set model to training mode for input_batch, target_batch in train_loader: optimizer.zero_grad() # Reset loss gradients from previous batch iteration loss = calc_loss_batch(input_batch, target_batch, model, device) loss.backward() # Calculate loss gradients optimizer.step() # Update model weights using loss gradients tokens_seen += input_batch.numel() # Returns the total number of elements (or tokens) in the input_batch. global_step += 1
# Optional evaluation step if global_step % eval_freq == 0: train_loss, val_loss = evaluate_model( model, train_loader, val_loader, device, eval_iter) train_losses.append(train_loss) val_losses.append(val_loss) track_tokens_seen.append(tokens_seen) print(f"Ep {epoch+1} (Step {global_step:06d}): " f"Train loss {train_loss:.3f}, Val loss {val_loss:.3f}")
# Print a sample text after each epoch generate_and_print_sample( model, tokenizer, device, start_context )
# Plot training and validation loss against epochs ax1.plot(epochs_seen, train_losses, label="Training loss") ax1.plot(epochs_seen, val_losses, linestyle="-.", label="Validation loss") ax1.set_xlabel("Epochs") ax1.set_ylabel("Loss") ax1.legend(loc="upper right") ax1.xaxis.set_major_locator(MaxNLocator(integer=True)) # only show integer labels on x-axis
# Create a second x-axis for tokens seen ax2 = ax1.twiny() # Create a second x-axis that shares the same y-axis ax2.plot(tokens_seen, train_losses, alpha=0) # Invisible plot for aligning ticks ax2.set_xlabel("Tokens seen")
fig.tight_layout() # Adjust layout to make room plt.savefig("loss-plot.pdf") plt.show()
Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction: Rewrite the sentence using a simile.
### Input: The car is very fast.
Correct response: >> The car is as fast as lightning.
Model response: >> The car is as fast as a bullet. ------------------------------------- Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction: What type of cloud is typically associated with thunderstorms?
Correct response: >> The type of cloud typically associated with thunderstorms is cumulonimbus.
Model response: >> A thunderstorm is a type of cloud that typically forms in the atmosphere over a region of high pressure. It typically produces a strong wind that blows across the area, creating a dense, dense cloud. ------------------------------------- Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction: Name the author of 'Pride and Prejudice'.
Correct response: >> Jane Austen.
Model response: >> The author of 'Pride and Prejudice' is George Bernard Shaw. -------------------------------------