|
Chuqur o‘qitish algoritmlari asosida masalalar yechish
|
Sana | 22.12.2023 | Hajmi | 52,42 Kb. | | #127101 |
6-Topshiriq
Chuqur o‘qitish algoritmlari asosida masalalar yechish
Ishdan maqsad. Talabalar neyron tarmog‘i, ularning ishlash prinsiplari, ishlash texnologiyalarini o‘rganish, ular asosida chuqur o‘qitish algoritmlari asosida neyron tarmog‘ini ishlab chiqish.
Vazifalar:
Chuqur o‘qitish algoritmlarini o‘rganish
chuqur o‘qitish algoritmlari asosida neyron tarmog‘ini ishlab Topshiriqni baholash. MAX 5 ball.
Berilgan topshiriqqa optimal yondoshish – 2 ball;
Xulosa va foydalangan adabiyotlarni yoritilganlik – 1 ball;
Topshiriqni himoya qilish – 2 ball.
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
class UyHayvonlarDataset(Dataset):
def __init__(self, data_path):
self.data = torch.tensor(data_path, dtype=torch.float32)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
sample = self.data[idx, :-1]
label = int(self.data[idx, -1])
return sample, label
class UyHayvonlarModel(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(UyHayvonlarModel, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, output_size)
def forward(self, x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
def train(model, dataloader, criterion, optimizer, num_epochs=1000):
for epoch in range(num_epochs):
for inputs, labels in dataloader:
outputs = model(inputs)
loss = criterion(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1) % 100 == 0:
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
data_path = [
[200, 150, 350, 0],
[205, 165, 340, 0],
[210, 160, 330, 0],
[110, 100, 50, 1],
[100, 90, 40, 1],
[90, 90, 40, 1],
[40, 35, 10, 2],
[35, 35, 9, 2],
[30, 25, 7, 2],
]
dataset = UyHayvonlarDataset(data_path)
dataloader = DataLoader(dataset, batch_size=8, shuffle=True)
input_size = 3 # Uzunlik, balandlik, og'irlik
hidden_size = 16
output_size = 3
model = UyHayvonlarModel(input_size, hidden_size, output_size)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
train(model, dataloader, criterion, optimizer, num_epochs=1000)
with torch.no_grad():
test_data = torch.tensor([[210,170,395]], dtype=torch.float32)
predicted_class = torch.argmax(model(test_data))
print(f'Predicted Class: {predicted_class.item()}')
|
| |