Skip to content
Learn Netverks
-2

Steps to install Pytorch

asked 8 hours ago by @qa-irvbzltyfruxws9sutzo 0 rep · 54 views

large language model torch google publisher tag

I am new to Python programming and trying to learn PyTorch with the Anaconda distribution. However, I am not sure how to correctly install PyTorch on my Windows machine. Somehow, I have managed to install the library, but it looks like there is an issue, and I am getting an exception while running the following program:

import torch
import torch.nn as nn
import torch.nn.functional as F
import math
text = "hello world! transformers are fun."  # Training text
chars = sorted(list(set(text)))
vocab_size = len(chars)

char_to_idx = {ch: i for i, ch in enumerate(chars)}
idx_to_char = {i: ch for i, ch in enumerate(chars)}

data = [char_to_idx[ch] for ch in text]
X = torch.tensor(data[:-1])
Y = torch.tensor(data[1:])

class PositionalEncoding(nn.Module):
    def __init__(self, d_model, max_len=100):
        super().__init__()
        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0, max_len).unsqueeze(1).float()
        div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0)/d_model))
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        self.pe = pe.unsqueeze(0)  # Shape: (1, max_len, d_model)

    def forward(self, x):
        x = x + self.pe[:, :x.size(1)]
        return x
class TinyGPT(nn.Module):
    def __init__(self, vocab_size, d_model=32, nhead=2):
        super().__init__()
        self.embed = nn.Embedding(vocab_size, d_model)
        self.pos_enc = PositionalEncoding(d_model)
        self.transformer_layer = nn.TransformerEncoderLayer(d_model=d_model, nhead=nhead)
        self.transformer = nn.TransformerEncoder(self.transformer_layer, num_layers=1)
        self.fc = nn.Linear(d_model, vocab_size)

    def forward(self, x):
        x = self.embed(x).unsqueeze(1)  # (seq_len, batch=1, d_model)
        x = self.pos_enc(x)
        x = self.transformer(x)
        x = self.fc(x.squeeze(1))
        return x
model = TinyGPT(vocab_size)
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
for epoch in range(200):
    optimizer.zero_grad()
    output = model(X)
    loss = loss_fn(output, Y)
    loss.backward()
    optimizer.step()

    if epoch % 20 == 0:
        print(f"Epoch {epoch}, Loss: {loss.item():.4f}")
    def generate(model, start_char, length=20):
       model.eval()
       idx = char_to_idx[start_char]
       result = start_char
       input_seq = torch.tensor([idx])

       for _ in range(length):
         output = model(input_seq)
         probs = F.softmax(output[-1], dim=0).detach()
         idx = torch.multinomial(probs, 1).item()
         result += idx_to_char[idx]
         input_seq = torch.cat([input_seq, torch.tensor([idx])])  # Append new char

         return result

print(generate(model, start_char="h", length=30))

Exception details:

--------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In[10], line 1
----> 1 import torch
      2 import torch.nn as nn
      3 import torch.nn.functional as F

File ~\anaconda3\Lib\site-packages\torch\__init__.py:287
    283                     raise err
    285         kernel32.SetErrorMode(prev_error_mode)
--> 287     _load_dll_libraries()
    288     del _load_dll_libraries
    291 def _get_cuda_dep_paths(path: str, lib_folder: str, lib_name: str) -> list[str]:
    292     # Libraries can either be in
    293     # path/nvidia/lib_folder/lib or
    294     # path/nvidia/cuXX/lib (since CUDA 13.0) or
    295     # path/lib_folder/lib

File ~\anaconda3\Lib\site-packages\torch\__init__.py:270, in _load_dll_libraries()
    266     err = ctypes.WinError(last_error)
    267     err.strerror += (
    268         f' Error loading "{dll}" or one of its dependencies.'
    269     )
--> 270     raise err
    271 elif res is not None:
    272     is_loaded = True

OSError: [WinError 1114] A dynamic link library (DLL) initialization routine failed. Error loading "C:\Users\ME\anaconda3\Lib\site-packages\torch\lib\c10.dll" or one of its dependencies.

Comments on this question (0)

Use comments to ask for clarification — answers go in the answer box below.

Log in to comment on this question.

0 answers

Your answer