## 1.Load Weight ```python import torch from llms_from_scratch.ch04 import GPTModel # For llms_from_scratch installation instructions, see: # https://github.com/rasbt/LLMs-from-scratch/tree/main/pkg BASE_CONFIG = { "vocab_size": 50257, # Vocabulary size "context_length": 1024, # Context length "drop_rate": 0.0, # Dropout rate "qkv_bias": True # Query-key-value bias } gpt = GPTModel(BASE_CONFIG) gpt.load_state_dict(torch.load(file_name, weights_only=True)) gpt.eval() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") gpt.to(device); ``` ## 2. Generate Text ```python import tiktoken from llms_from_scratch.ch05 import generate, text_to_token_ids, token_ids_to_text torch.manual_seed(123) tokenizer = tiktoken.get_encoding("gpt2") token_ids = generate( model=gpt.to(device), idx=text_to_token_ids("Every effort moves", tokenizer).to(device), max_new_tokens=30, context_size=BASE_CONFIG["context_length"], top_k=1, temperature=1.0 ) print("Output text:\n", token_ids_to_text(token_ids, tokenizer)) ```