Files
Karpathy_ZtH_Learning/My_Experiments/pytorch3.ipynb
T

68 KiB

Manual approach

In [58]:
import torch

X = torch.tensor([[0.0],[10.0], [20.0], [30.0], [40.0]])
Y = torch.tensor([[32.0], [50.0], [68.0], [86.0], [104.0]])
In [59]:
W = torch.randn((1, 1), requires_grad=True)
B = torch.randn((1, 1), requires_grad=True)
In [60]:
learning_rate = 0.001

for epoch in range(20000):
    Y_pred = X.matmul(W) + B

    loss = ((Y_pred - Y)**2).mean()

    loss.backward()
    with torch.no_grad():
        W -= learning_rate * W.grad
        B -= learning_rate * B.grad

        W.grad.zero_()
        B.grad.zero_()

    if epoch % 400 == 0:
        print(f"Epoch {epoch}: Loss = {loss.item():.4f}")
Epoch 0: Loss = 2051.0815
Epoch 400: Loss = 181.1680
Epoch 800: Loss = 106.3257
Epoch 1200: Loss = 62.4014
Epoch 1600: Loss = 36.6228
Epoch 2000: Loss = 21.4935
Epoch 2400: Loss = 12.6143
Epoch 2800: Loss = 7.4032
Epoch 3200: Loss = 4.3449
Epoch 3600: Loss = 2.5500
Epoch 4000: Loss = 1.4965
Epoch 4400: Loss = 0.8783
Epoch 4800: Loss = 0.5155
Epoch 5200: Loss = 0.3025
Epoch 5600: Loss = 0.1775
Epoch 6000: Loss = 0.1042
Epoch 6400: Loss = 0.0612
Epoch 6800: Loss = 0.0359
Epoch 7200: Loss = 0.0211
Epoch 7600: Loss = 0.0124
Epoch 8000: Loss = 0.0073
Epoch 8400: Loss = 0.0043
Epoch 8800: Loss = 0.0025
Epoch 9200: Loss = 0.0015
Epoch 9600: Loss = 0.0009
Epoch 10000: Loss = 0.0005
Epoch 10400: Loss = 0.0003
Epoch 10800: Loss = 0.0002
Epoch 11200: Loss = 0.0001
Epoch 11600: Loss = 0.0001
Epoch 12000: Loss = 0.0000
Epoch 12400: Loss = 0.0000
Epoch 12800: Loss = 0.0000
Epoch 13200: Loss = 0.0000
Epoch 13600: Loss = 0.0000
Epoch 14000: Loss = 0.0000
Epoch 14400: Loss = 0.0000
Epoch 14800: Loss = 0.0000
Epoch 15200: Loss = 0.0000
Epoch 15600: Loss = 0.0000
Epoch 16000: Loss = 0.0000
Epoch 16400: Loss = 0.0000
Epoch 16800: Loss = 0.0000
Epoch 17200: Loss = 0.0000
Epoch 17600: Loss = 0.0000
Epoch 18000: Loss = 0.0000
Epoch 18400: Loss = 0.0000
Epoch 18800: Loss = 0.0000
Epoch 19200: Loss = 0.0000
Epoch 19600: Loss = 0.0000
In [61]:
with torch.no_grad():
    X_test = torch.tensor([[100.0]])
    Y_test_pred = X_test.matmul(W) + B
    print("\n--- Результаты обучения ---")
    print(f"Предсказание для 100°C: {Y_test_pred.item():.2f}°F (Ожидалось: 212.00)")
    print(f"Итоговый вес W: {W.item():.4f} (Ожидалось: 1.8)")
    print(f"Итоговое смещение B: {B.item():.4f} (Ожидалось: 32.0)")
--- Результаты обучения ---
Предсказание для 100°C: 212.00°F (Ожидалось: 212.00)
Итоговый вес W: 1.8000 (Ожидалось: 1.8)
Итоговое смещение B: 31.9986 (Ожидалось: 32.0)

Professional approach

In [62]:
import torch
import torch.nn as nn
import torch.optim as optim

X = torch.tensor([[0.0],[10.0], [20.0], [30.0], [40.0]])
Y = torch.tensor([[32.0], [50.0], [68.0], [86.0], [104.0]])
In [63]:
model = nn.Linear(in_features=1, out_features=1)
In [64]:
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.001)
In [65]:
for epoch in range(2000):
    Y_pred = model(X)

    loss = criterion(Y_pred, Y)

    optimizer.zero_grad()

    loss.backward()

    optimizer.step()

    if epoch % 400 == 0:
        print(f"Epoch {epoch}: loss = {loss.item()}")
Epoch 0: loss = 5002.43408203125
Epoch 400: loss = 189.92440795898438
Epoch 800: loss = 111.46468353271484
Epoch 1200: loss = 65.41755676269531
Epoch 1600: loss = 38.39296340942383
In [66]:
with torch.no_grad():
    X_test = torch.tensor([[100.0]])
    Y_test_pred = model(X_test)
    print("\n--- Результаты обучения ---")
    print(f"Предсказание для 100°C: {Y_test_pred.item():.2f}°F")
    
    # Доступ к обученным весам внутри слоя
    print(f"Итоговый вес W: {model.weight.item():.4f}")
    print(f"Итоговое смещение B: {model.bias.item():.4f}")
--- Результаты обучения ---
Предсказание для 100°C: 231.20°F
Итоговый вес W: 2.0742
Итоговое смещение B: 23.7783
In [67]:
import torch
import torch.nn as nn
import torch.optim as optim

# 1. Define the MLP model
class SimpleMLP(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(SimpleMLP, self).__init__()
        # First linear layer: input to hidden layer
        self.fc1 = nn.Linear(input_size, hidden_size)
        # Second linear layer: hidden layer to output layer
        self.fc2 = nn.Linear(hidden_size, output_size)

    def forward(self, x):
        # Apply ReLU activation function
        x = torch.relu(self.fc1(x))
        # Output layer (no activation for regression or logits)
        x = self.fc2(x)
        return x

# 2. Setup parameters
input_size = 784  # Example: Flattened MNIST image (28*28)
hidden_size = 128
output_size = 10 # Example: Number of classes for classification

# Initialize the model
model = SimpleMLP(input_size, hidden_size, output_size)
print("--- MLP Model Architecture ---")
print(model)

# 3. Create dummy data for demonstration (In a real scenario, you would load a dataset like MNIST)
# Batch size and input features
batch_size = 64
dummy_input = torch.randn(batch_size, input_size)
# Dummy labels
dummy_target = torch.randint(0, output_size, (batch_size,))

# 4. Setup Loss Function and Optimizer
criterion = nn.CrossEntropyLoss()  # Suitable for multi-class classification
optimizer = optim.Adam(model.parameters(), lr=0.001)

print("\n--- Starting Training Simulation ---")

# 5. Training Loop (Simulation)
num_epochs = 5

for epoch in range(num_epochs):
    # Forward pass
    outputs = model(dummy_input)
    
    # Calculate loss
    loss = criterion(outputs, dummy_target)
    
    # Backward pass and optimization
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')

print("\nMLP Example successfully defined and simulated training steps.")
--- MLP Model Architecture ---
SimpleMLP(
  (fc1): Linear(in_features=784, out_features=128, bias=True)
  (fc2): Linear(in_features=128, out_features=10, bias=True)
)

--- Starting Training Simulation ---
Epoch [1/5], Loss: 2.3632
Epoch [2/5], Loss: 2.0220
Epoch [3/5], Loss: 1.7251
Epoch [4/5], Loss: 1.4678
Epoch [5/5], Loss: 1.2423

MLP Example successfully defined and simulated training steps.
In [68]:
import torch
from torch import nn
from matplotlib import pyplot as plt
In [69]:
w = 0.7
b = 0.3

X = torch.arange(10, 100, 5).unsqueeze(dim=1)
y = X * w + b

plt.plot(X, y)
Out [69]:
[<matplotlib.lines.Line2D at 0x704a3989d750>]
In [70]:
train_split = int(0.8 * len(X))
X_train, y_train = X[:train_split], y[:train_split]
X_test, y_test = X[train_split:], y[train_split:]
In [71]:


class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.w = nn.Parameter(torch.randn(1))
        self.b = nn.Parameter(torch.randn(1))
    def forward(self, x):
        return x * self.w + self.b
In [78]:
model = Model()
loss_fn = nn.L1Loss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.0001)
epochs_num = 100
epochs = []
losses = []
for epoch in range(epochs_num):
    model.train()
    y_pred = model(X_train)
    loss = loss_fn(y_pred, y_train)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    epochs.append(epoch)
    losses.append(loss.item())
plt.plot(X, y)
plt.plot(X_train, y_pred)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[78], line 17
     13     optimizer.step()
     14     epochs.append(epoch)
     15     losses.append(loss.item())
     16 plt.plot(X, y)
---> 17 plt.plot(X_train, y_pred)

File ~/Desktop/Karpathy_ZtH_Learning/.venv/lib/python3.11/site-packages/matplotlib/pyplot.py:4043, in plot(scalex, scaley, data, *args, **kwargs)
   4035 @_copy_docstring_and_deprecators(Axes.plot)
   4036 def plot(
   4037     *args: float | ArrayLike | str,
   (...)   4041     **kwargs,
   4042 ) -> list[Line2D]:
-> 4043     return gca().plot(
   4044         *args,
   4045         scalex=scalex,
   4046         scaley=scaley,
   4047         **({"data": data} if data is not None else {}),
   4048         **kwargs,
   4049     )

File ~/Desktop/Karpathy_ZtH_Learning/.venv/lib/python3.11/site-packages/matplotlib/axes/_axes.py:1792, in Axes.plot(self, scalex, scaley, data, *args, **kwargs)
   1549 """
   1550 Plot y versus x as lines and/or markers.
   1551 
   (...)   1789 (``'green'``) or hex strings (``'#008000'``).
   1790 """
   1791 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1792 lines = [*self._get_lines(self, *args, data=data, **kwargs)]
   1793 for line in lines:
   1794     self.add_line(line)

File ~/Desktop/Karpathy_ZtH_Learning/.venv/lib/python3.11/site-packages/matplotlib/axes/_base.py:331, in _process_plot_var_args.__call__(self, axes, data, return_kwargs, *args, **kwargs)
    329     this += args[0],
    330     args = args[1:]
--> 331 yield from self._plot_args(
    332     axes, this, kwargs, ambiguous_fmt_datakey=ambiguous_fmt_datakey,
    333     return_kwargs=return_kwargs
    334 )

File ~/Desktop/Karpathy_ZtH_Learning/.venv/lib/python3.11/site-packages/matplotlib/axes/_base.py:499, in _process_plot_var_args._plot_args(self, axes, tup, kwargs, return_kwargs, ambiguous_fmt_datakey)
    497 if len(xy) == 2:
    498     x = _check_1d(xy[0])
--> 499     y = _check_1d(xy[1])
    500 else:
    501     x, y = index_of(xy[-1])

File ~/Desktop/Karpathy_ZtH_Learning/.venv/lib/python3.11/site-packages/matplotlib/cbook.py:1413, in _check_1d(x)
   1411 """Convert scalars to 1D arrays; pass-through arrays as is."""
   1412 # Unpack in case of e.g. Pandas or xarray object
-> 1413 x = _unpack_to_numpy(x)
   1414 # plot requires `shape` and `ndim`.  If passed an
   1415 # object that doesn't provide them, then force to numpy array.
   1416 # Note this will strip unit information.
   1417 if (not hasattr(x, 'shape') or
   1418         not hasattr(x, 'ndim') or
   1419         len(x.shape) < 1):

File ~/Desktop/Karpathy_ZtH_Learning/.venv/lib/python3.11/site-packages/matplotlib/cbook.py:2517, in _unpack_to_numpy(x)
   2508         return xtmp
   2509 if _is_torch_array(x) \
   2510     or _is_jax_array(x) \
   2511     or _is_tensorflow_array(x) \
   (...)   2515     # https://numpy.org/devdocs/user/basics.interoperability.html#using-arbitrary-objects-in-numpy
   2516     # therefore, let arrays do better if they can
-> 2517     xtmp = np.asarray(x)
   2519     # In case np.asarray method does not return a numpy array in future
   2520     if isinstance(xtmp, np.ndarray):

File ~/Desktop/Karpathy_ZtH_Learning/.venv/lib/python3.11/site-packages/torch/_tensor.py:1253, in Tensor.__array__(self, dtype)
   1251     return handle_torch_function(Tensor.__array__, (self,), self, dtype=dtype)
   1252 if dtype is None:
-> 1253     return self.numpy()
   1254 else:
   1255     return self.numpy().astype(dtype, copy=False)

RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.