first neuron network on pytorch

This commit is contained in:
2025-08-19 19:35:22 +07:00
parent c4559c0f65
commit 1196b8e767
4 changed files with 58 additions and 12 deletions

View File

@@ -2,24 +2,35 @@ import torch
import torch.nn as nn
import torch.optim as optim
torch.set_num_threads(torch.get_num_threads())
print("Потоков используется:", torch.get_num_threads())
# 1. Данные: y = 2x + 1 с шумом
torch.manual_seed(0)
x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1) # shape: [100,1]
y = 2 * x + 1 + 0.2 * torch.randn(x.size())
x1 = 2 * torch.rand(1000, 1) - 1
x2 = 2 * torch.rand(1000, 1) - 1
x = torch.cat([x1, x2], dim=1)
y = (((x1**2 + x2**2)**0.5)>=0.5).float()
# 2. Определяем модель (1 скрытый слой, 10 нейронов)
# 2. Определяем модель
model = nn.Sequential(
nn.Linear(1, 10), # вход -> скрытый слой
nn.ReLU(), # активация
nn.Linear(10, 1) # скрытый -> выход
nn.Linear(2, 100),
nn.Tanh(),
nn.Linear(100, 200),
nn.Tanh(),
nn.Linear(200, 100),
nn.Tanh(),
nn.Linear(100, 1),
nn.Sigmoid()
)
# 3. Функция потерь и оптимизатор
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
optimizer = optim.SGD(model.parameters(), lr=0.1)
# 4. Обучение
for epoch in range(200):
epochs = 10_000
for epoch in range(epochs):
# прямой проход
y_pred = model(x)
loss = criterion(y_pred, y)
@@ -30,9 +41,20 @@ for epoch in range(200):
optimizer.step()
if (epoch+1) % 40 == 0:
print(f'Epoch {epoch+1}/200 | Loss: {loss.item():.4f}')
print(f'Epoch {epoch+1}/{epochs} | Loss: {loss.item():.4f}')
torch.save(model.state_dict(), "model.pth")
# 5. Проверка результата
test_x = torch.tensor([[0.5]])
pred_y = model(test_x)
print(f"При x=0.5 сеть предсказывает: {pred_y.item():.3f}")
tests = 100
test_x1 = torch.unsqueeze(torch.linspace(-1, 1, tests), dim=1)
test_x2 = torch.unsqueeze(torch.linspace(-1, 1, tests), dim=1)
test_y = (((test_x1**2 + test_x2**2)**0.5) >= 0.5).float()
for i in range(tests):
# объединяем по dim=0, т.к. каждый x[i] имеет форму [1]
test_x = torch.cat([test_x1[i], test_x2[i]], dim=0).unsqueeze(0) # форма [1,2]
pred_y = model(test_x)
print(f"Нейросеть предсказала: {pred_y.item():.3f}; правильный ответ: {test_y[i].item():.0f}; точка: ({test_x})")