|
Oʻzbekiston respublikasi axborot
|
Sana | 18.12.2023 | Hajmi | 217,68 Kb. | | #122702 |
Bog'liq 5-amaliy ML
OʻZBEKISTON RESPUBLIKASI AXBOROT
TEXNOLOGIYALARI VA KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI
MUHAMMAD AL-XORAZMIY NOMIDAGI
TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI
Kompyuter injiniringi fakulteti
Sun’iy intellekt kafedrasi
Mashinali o'qitishga kirishfanidan
5-TOPSHIRIQ
Bajardi: 221-21 guruh talabasi
Hakimov Alisher
Tekshirdi: Qobilov Sirojiddin
TOSHKENT 2023
Sun’iy neyron tarmoqlari. Sodda neyron tarmoqlarini qurish. Neyron tarmoqlarining to‘gri va teskari tarqalish algoritmlari.
Datasetni shakllantirish. Bunda o‘zgaruvchilar soni kamida 10 tani va qatorlar soni 20 tani tashkil etishi lozim.
Keras, TensorFlow kutubxonalaridan foydalanib sodda neyron tarmoq quring.
Neyron tarmoqning o’qitish natijalarini jadval va garfik tasvirini xosil qiling
Dastur kodi
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
class ShaharlarDataset(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 ShaharlarModel(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(ShaharlarModel, 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():.5f}')
data_path = [
[1000000, 1000, 90, 0],
[700000, 800, 85, 0],
[500000, 650, 66, 1],
[430000, 700, 78, 1],
[390000, 530, 56, 1],
[260000, 500, 60, 2],
[200000, 450, 50, 2],
[150000, 340, 48, 2],
]
dataset = ShaharlarDataset(data_path)
dataloader = DataLoader(dataset, batch_size=8, shuffle=True)
input_size = 3 # Aholi soni, O'rtacha oylik ish haqi, ta'lim darajasi,
hidden_size = 16
output_size = 3
model = ShaharlarModel(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([[400000,600,75]], dtype=torch.float32)
predicted_class = torch.argmax(model(test_data))
print(f'Predicted Class: {predicted_class.item()}')
O’qitishning jadval va visual ko’rinishi
|
| |