diff --git a/Tasks/daily tasks/Hashina/day_3.py b/Tasks/daily tasks/Hashina/day_3.py new file mode 100644 index 0000000..b4285fa --- /dev/null +++ b/Tasks/daily tasks/Hashina/day_3.py @@ -0,0 +1,18 @@ +from torch import nn + +class Network(nn.Module): + def __init__(self): + super().__init__() + + self.hidden = nn.Linear(1, 2) + self.output = nn.Linear(2, 1) + + self.sigmoid = nn.Sigmoid() + + def forward(self, x): + x = self.hidden(x) + x = self.sigmoid(x) + x = self.output(x) + + return x + diff --git a/Tasks/daily tasks/Hashina/day_4.py b/Tasks/daily tasks/Hashina/day_4.py index b4285fa..d8aa555 100644 --- a/Tasks/daily tasks/Hashina/day_4.py +++ b/Tasks/daily tasks/Hashina/day_4.py @@ -1,18 +1,98 @@ -from torch import nn +import torch +import torch.nn as nn +import torch.nn.functional as F +import torchvision +import torchvision.transforms as transforms +import torch.optim as optim -class Network(nn.Module): +transform = transforms.Compose( + [ + transforms.ToTensor(), + transforms.Normalize( + (0.5, 0.5, 0.5), + (0.5, 0.5, 0.5) + ) + ] +) + +trainset = torchvision.datasets.CIFAR10( + root='./data', + train=True, + download=False, + transform=transform +) + +testset = torchvision.datasets.CIFAR10( + root='./data', + train=False, + download=False, + transform=transform +) + +trainloader = torch.utils.data.DataLoader( + trainset, + batch_size=4, + shuffle=True, + num_workers=2 +) + +testloader = torch.utils.data.DataLoader( + testset, + batch_size=4, + shuffle=False, + num_workers=2 +) + +classes = ( + 'plane', 'car', 'bird', 'cat', + 'deer', 'dog', 'frog', 'horse', 'ship', 'truck' +) + +class Net(nn.Module): def __init__(self): - super().__init__() - - self.hidden = nn.Linear(1, 2) - self.output = nn.Linear(2, 1) - - self.sigmoid = nn.Sigmoid() - + super(Net, self).__init__() + self.conv1 = nn.Conv2d(3, 6, 5) + self.pool = nn.MaxPool2d(2, 2) + self.conv2 = nn.Conv2d(6, 16, 5) + self.fc1 = nn.Linear(16 * 5 * 5, 120) + self.fc2 = nn.Linear(120, 84) + self.fc3 = nn.Linear(84, 10) + def forward(self, x): - x = self.hidden(x) - x = self.sigmoid(x) - x = self.output(x) - + x = self.pool(F.relu(self.conv1(x))) + x = self.pool(F.relu(self.conv2(x))) + x = x.view(-1, 16 * 5 * 5) + x = F.relu(self.fc1(x)) + x = F.relu(self.fc2(x)) + x = self.fc3(x) return x - + + +net = Net() + +loss_function = nn.CrossEntropyLoss() +optimizer = optim.SGD( + net.parameters(), + lr=0.001 +) + +for epoch in range(2): + running_loss = 0.0 + for i, data in enumerate(trainloader, 0): + # data = (inputs, labels) + inputs, labels = data + optimizer.zero_grad() + + outputs = net(inputs) + loss = loss_function(outputs, labels) + loss.backward() + optimizer.step() + + running_loss = running_loss + loss.item() + if i % 2000 == 1999: + print( + '[%d, %5d] loss: %.3f' % + (epoch + 1, i+1, running_loss/2000) + ) + running_loss = 0.0 +print("vola")