From a317c615eabd714d4a4dfdb4068b121f51d65866 Mon Sep 17 00:00:00 2001 From: Jakub Henyk Date: Sat, 6 May 2023 13:46:09 +0200 Subject: [PATCH] added train.py and test.py --- __pycache__/train.cpython-38.pyc | Bin 0 -> 2323 bytes __pycache__/zadanie1.cpython-38.pyc | Bin 0 -> 847 bytes cifar_net.pth | Bin 0 -> 1279 bytes test.py | 83 +++++ testresults.txt | 450 ++++++++++++++++++++++++++++ train.py | 81 +++++ zadanie1.py | 44 +-- 7 files changed, 638 insertions(+), 20 deletions(-) create mode 100644 __pycache__/train.cpython-38.pyc create mode 100644 __pycache__/zadanie1.cpython-38.pyc create mode 100644 cifar_net.pth create mode 100644 test.py create mode 100644 testresults.txt create mode 100644 train.py diff --git a/__pycache__/train.cpython-38.pyc b/__pycache__/train.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e6316ba970d7c2caad1bab6638d1760e85f2043e GIT binary patch literal 2323 zcmb_d-H#MS5by5!*xA{=5AKfR8xas#!9_qZAp}rP(B!y~0LDy?Oor{=y*cKKo?h@S zdx^=O;fX~5gWbpbU-ZQ%)33gGi7&ny6Rqmq!$sqpz3Hmz>YD26s$ca!nws(njGwN3 z!GG}x`570J!-d5ynB_G}2q&B-B%p?~z%twp9K+qfHM|m34ENG%P^F`9{InL-#&SKa zF+xc?6-@~CWd-NOyf`V& ziSvC6?lT`;5boeTuxSp7xG-d(qYp@9VHdNy(n2~ptns2e4T3Pa4cJT4rkrxN?Lm$$ zZbJ_2m&7K41D+ghtq9eEa=acW+c42tc@&5ueKMd!NV8%C3tZ75>ygmr1RSuHU92iN za>{NYr1ruv&SDjYnq`@Gm*Y%CQd{lTl?K&zDU$XM)zvU;B~e+1;STxzgt)ePe?#Tj z>gSK{t;XFn{NTgOS1*6Gs$>*r&0<43aKa3RgK-h+yPI{0y192!i=lcvP{z=2!z`zP z^vHnp=#XyF9_Eg&G^hk{Lg=yzOw+Gq9r%b$p{CN#<%>x2gRmGK>H2iDhDVNf8ZO>5 zi&6pi^usXCc{f419){1lQ8L;waZ3zbPmM3!5=jz<69;0$NIX?y;25tt(WCXM(X{qz z3Ol3*6SHmZ_B99!@X)7AeuJqWzHRY}J+&lK z$; zh}^b7-uYwaw;%64xqq|s!}mCTy$Kd9k%5=ysmN3}-JEG&Y{l(JhJc!8q1GA~wOd3P zk4nu7t{u2qu>lWcp}ozBM_DW`X}W2W2v|Z?K95TT(Bj0R38l|Uc!APV`xCNfY?@gn z@omuad&_3Ko3#{pi4yHk$n?(mjHW@Sc1Fy-XC5*0>KOD$Y+GF`q$naOmJqa#L7%p` zcn<9%N+qwLjr%-Gx&pAQa%@6fHD*Ti&}uAUFP6|oPFKZADbM1kKKflYjUy4#x`H2| zm2bWrwN%CYTZmE~2a{m-wuH|3G*dD!HkPqTPNCX*aCb@j5Gy>((0`>ojsEQWA3yvI z`Zank0>4zy_aTp4Y677KtLC}!u8$t_8~Es!w}5!iOru|&*6AFrTXPHEyw9eY4UGmI Vw2ZJnUF&bjW-jyS0;@Bd{skC(LB#+7 literal 0 HcmV?d00001 diff --git a/__pycache__/zadanie1.cpython-38.pyc b/__pycache__/zadanie1.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..453d6208c8ed65a4f962128055ef9f4dc7989426 GIT binary patch literal 847 zcmYjO%Wl*#6t$gq+8IiVL@T<2RW?Pfcu1&1MSajpbW{nIg*8%T<+>#_aU#c4+A6by zC4bPx%#uG~#fN0eYJPzYTn8%EmXFWXJvTY``ncDN39g?{KS1aa^2-Ky7sFr+PrZr& z1ZYk;?QX^$VBmZq+y(lcB<>6qWRP0CT_KV=JllBc9|(p_D9|Y#lZK3`tp^u8@F9Q@ zB8XuQ=5OLL>oUC?Hg+01VbE__W~*oy-D{lN=#v8rXB6&#>vGUvArlwa6m`(wB#qU3 zQ?iU~;!iDh(IYZ;8`Qz#SGGdN9`Z$4>bQ@52}s9-#%ug0ICZeQ2ZQV0_NX>xA$7f$ z))$&$JF^b#jAo7n*8+}_%zO)h1v`UTgpkCoGY|tF?@N)JkEzg-hr8-5Q&MW~zZ0LY z@b2v@BefBkDx@;4|3;gvHayzBlw+$;)<4biT#42xGKC;6lMpk)2<}}0rED;X-S!h#f5wsKzlP-GSJL5F|)8JN{f^90=(HdE*li8hB7iRfN%iLNRnbuMu{YSl;Ci4l1Ga!l(++NRtwtO zv;MQSKea&Le#P|?H^*|*%tvA^{u%Kq*fMf*dFzILX9&+K&6j_&JP zBER3|u=4)DS8nes{=&K6Ep@`a&z;luo$2P?e@Mb(|8zaueSZ`?_en~z?J>CUdSCA% zwfzjSCi~}Kez&i2=Dxl6lQ-?z^787w`Jbit+lZ;`UweRef9z+4eFvL=?E`t8fAxV0 z6M#Vm!uY(dfy3*d7;|${A>?&%Sj@V3!(Lqbt^Gz5|NT*HE&F3qBll0~%HI!iN9}dB zAPJxwKp3|>_!+d}?tn(Eo0B?GmIGa2%muRP;=J@wCZH`K9N^6eqTqQFIUxvuBv1f) z4n^0E9F|fjI+p?y7P4;Cq=Rk_a#*06vjFH??B;;t0o@#ApYo%)M+2C1Vdg-?F~FOR zO$Vw-j#(G399Uw30gUDbdIba~figc30QG^24~Dr=1t94FZ&o&t7z+@B)I-z)0QYY> ARsaA1 literal 0 HcmV?d00001 diff --git a/test.py b/test.py new file mode 100644 index 0000000..1fc4aa7 --- /dev/null +++ b/test.py @@ -0,0 +1,83 @@ +import pandas as pd +import numpy as np +import zadanie1 as z +import train as tr +import torch +import torchvision +import torchvision.transforms as transforms +import torch.nn as nn +import torch.nn.functional as F +import torch.optim as optim + +class Net(nn.Module): + def __init__(self): + super().__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(20, 6) + self.fc3 = nn.Linear(6, 6) + + def forward(self, x): + #x = self.pool(F.relu(self.conv1(x))) + #x = self.pool(F.relu(self.conv2(x))) + #x = torch.flatten(x, 1) + #x = F.relu(self.fc1(x)) + #x = F.relu(self.fc2(x)) + x = self.fc3(x) + return x + +testdata = [] + +def testNet(testloader): + + PATH = './cifar_net.pth' + net = Net() + net.load_state_dict(torch.load(PATH)) + + correct = 0 + total = 0 + with torch.no_grad(): + for data in testloader: + input, labels = data + + labelsX = torch.Tensor([x for x in labels]) + labels = labelsX.type(torch.LongTensor) + + outputs = net(input) + + _, predicted = torch.max(outputs.data, 1) + testdata.append([input, labels, predicted]) + total += labels.size(0) + correct += (predicted == labels).sum().item() + + print(f'Accuracy of the network: {100 * correct // total} %') + +if __name__ == '__main__': + + train, dev, test = z.prepareData() + + batch_size = 4 + + trainlist = train.values.tolist() + testlist = test.values.tolist() + + trainset = [[torch.Tensor(x[1:]), torch.Tensor([x[0]])] for x in trainlist] + trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, + shuffle=True, num_workers=2) + + testset = [[torch.Tensor(x[1:]), torch.Tensor([x[0]])] for x in testlist] + testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, + shuffle=False, num_workers=2) + + classes = ('male', 'female') + + testNet(testloader) + + with open('testresults.txt', 'w') as the_file: + for item in testdata: + for i in range(len(item)): + the_file.write(f'data: {item[0][i]} \n true value: {item[1][i]} \n prediction: {item[2][i]}\n') + + \ No newline at end of file diff --git a/testresults.txt b/testresults.txt new file mode 100644 index 0000000..4b4976d --- /dev/null +++ b/testresults.txt @@ -0,0 +1,450 @@ +data: tensor([0.8081, 0.4909, 0.5100, 0.5556, 0.2353, 0.5000]) + true value: 0 + prediction: 1 +data: tensor([0.2020, 0.9983, 0.6000, 0.3333, 0.4706, 0.3750]) + true value: 0 + prediction: 1 +data: tensor([0.8990, 0.6046, 0.5300, 0.5556, 0.5882, 0.0000]) + true value: 1 + prediction: 1 +data: tensor([0.7576, 0.7115, 0.1200, 0.2222, 0.4118, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.0505, 0.6646, 0.5500, 1.0000, 0.3529, 0.2500]) + true value: 1 + prediction: 1 +data: tensor([0.7980, 0.6478, 0.4100, 0.5556, 0.5882, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.9293, 0.9043, 0.6500, 0.5556, 0.3529, 0.5000]) + true value: 0 + prediction: 1 +data: tensor([0.1515, 0.8229, 0.2700, 0.8889, 0.3529, 0.1250]) + true value: 1 + prediction: 1 +data: tensor([0.9495, 0.3447, 0.8400, 1.0000, 0.5882, 0.7500]) + true value: 0 + prediction: 1 +data: tensor([0.6869, 0.4822, 0.4300, 0.4444, 0.4706, 0.0000]) + true value: 1 + prediction: 1 +data: tensor([0.2929, 0.2790, 0.7300, 0.3333, 0.2941, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.8788, 0.6090, 0.6100, 0.1111, 0.5294, 0.0000]) + true value: 0 + prediction: 1 +data: tensor([0.2929, 0.8699, 0.0700, 0.6667, 0.5294, 0.7500]) + true value: 1 + prediction: 1 +data: tensor([0.4343, 0.2654, 0.4900, 0.1111, 0.2941, 0.0000]) + true value: 0 + prediction: 1 +data: tensor([0.4343, 0.5450, 0.9000, 0.5556, 0.5882, 0.5000]) + true value: 0 + prediction: 1 +data: tensor([0.2323, 0.3998, 0.7000, 0.6667, 0.2941, 0.7500]) + true value: 0 + prediction: 1 +data: tensor([0.8788, 0.9168, 0.2800, 0.5556, 0.2353, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.8182, 0.5540, 0.5400, 0.6667, 0.4706, 0.2500]) + true value: 1 + prediction: 1 +data: tensor([0.3232, 0.9750, 0.8300, 0.1111, 0.2353, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.0707, 0.9986, 0.1800, 0.5556, 0.3529, 0.6250]) + true value: 0 + prediction: 1 +data: tensor([0.9192, 0.7089, 0.6600, 0.5556, 0.4118, 0.1250]) + true value: 1 + prediction: 1 +data: tensor([0.1010, 0.8139, 0.2100, 0.5556, 0.3529, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.2020, 0.5989, 0.8800, 0.5556, 0.3529, 0.1250]) + true value: 1 + prediction: 1 +data: tensor([0.0505, 0.8447, 0.6100, 0.3333, 0.4706, 0.0000]) + true value: 0 + prediction: 1 +data: tensor([0.5455, 0.2959, 0.3400, 0.1111, 0.5882, 0.3750]) + true value: 0 + prediction: 1 +data: tensor([0.1212, 0.2929, 0.6300, 0.1111, 0.4706, 0.0000]) + true value: 0 + prediction: 1 +data: tensor([0.9091, 0.3290, 0.9900, 0.5556, 0.5882, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.9192, 0.8242, 0.9600, 1.0000, 0.0000, 0.7500]) + true value: 1 + prediction: 1 +data: tensor([0.9091, 0.3211, 0.3500, 0.4444, 0.0588, 0.7500]) + true value: 1 + prediction: 1 +data: tensor([0.6061, 0.5712, 0.3600, 0.7778, 0.0000, 0.1250]) + true value: 1 + prediction: 1 +data: tensor([0.4343, 0.3891, 0.4700, 0.5556, 0.6471, 0.1250]) + true value: 1 + prediction: 1 +data: tensor([0.5253, 0.7838, 0.7700, 0.1111, 0.2353, 0.3750]) + true value: 0 + prediction: 1 +data: tensor([0.8182, 0.8033, 0.9100, 0.4444, 0.0000, 0.0000]) + true value: 0 + prediction: 1 +data: tensor([0.4343, 0.7040, 0.3700, 0.5556, 0.2941, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.2020, 0.5960, 0.4100, 0.3333, 0.5294, 0.0000]) + true value: 1 + prediction: 1 +data: tensor([0.7172, 0.3169, 0.4600, 0.5556, 0.0588, 0.2500]) + true value: 1 + prediction: 1 +data: tensor([0.7576, 0.3232, 0.6500, 0.7778, 0.4118, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.5253, 0.7014, 0.5500, 0.5556, 0.0588, 0.0000]) + true value: 1 + prediction: 1 +data: tensor([0.4040, 0.9856, 0.1400, 0.7778, 0.0000, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.4646, 0.8728, 0.3800, 0.1111, 0.1765, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.9899, 0.6589, 0.3000, 0.5556, 0.0000, 0.7500]) + true value: 0 + prediction: 1 +data: tensor([0.8485, 0.2990, 0.9600, 0.1111, 0.0588, 0.1250]) + true value: 1 + prediction: 1 +data: tensor([0.7879, 0.3721, 0.5400, 0.5556, 0.0588, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.6465, 0.2896, 0.5300, 0.5556, 0.0588, 0.7500]) + true value: 0 + prediction: 1 +data: tensor([0.9899, 0.4630, 0.7900, 0.2222, 0.0000, 0.1250]) + true value: 1 + prediction: 1 +data: tensor([0.3232, 0.5500, 0.2800, 0.1111, 0.3529, 0.3750]) + true value: 0 + prediction: 1 +data: tensor([0.2222, 0.9709, 0.4400, 0.6667, 0.1765, 0.7500]) + true value: 0 + prediction: 1 +data: tensor([0.1717, 0.7937, 0.3500, 0.4444, 0.0588, 0.7500]) + true value: 1 + prediction: 1 +data: tensor([0.0909, 0.7996, 0.1800, 0.5556, 0.0000, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.2424, 0.2716, 0.0400, 0.5556, 0.0000, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.0808, 0.8281, 0.7500, 0.5556, 0.5294, 0.2500]) + true value: 1 + prediction: 1 +data: tensor([0.6263, 0.9799, 0.5400, 0.1111, 0.0588, 0.0000]) + true value: 0 + prediction: 1 +data: tensor([0.3737, 0.8916, 0.4500, 0.4444, 0.0000, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.6869, 0.5801, 0.4700, 0.8889, 0.5294, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.1515, 0.3077, 0.6700, 0.6667, 0.0000, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.4444, 0.9142, 0.8900, 0.5556, 0.7059, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.1818, 0.8579, 0.1600, 0.4444, 0.0000, 0.6250]) + true value: 0 + prediction: 1 +data: tensor([0.6970, 0.8881, 0.8000, 0.7778, 0.0588, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.1616, 0.7906, 0.8300, 0.2222, 0.1176, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.7172, 0.4833, 0.7400, 0.5556, 0.0588, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.5758, 0.5406, 0.9400, 0.1111, 0.0000, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.9798, 0.8254, 0.7700, 0.1111, 0.0000, 0.6250]) + true value: 0 + prediction: 1 +data: tensor([0.6869, 0.2709, 0.4800, 0.5556, 0.0000, 0.2500]) + true value: 0 + prediction: 1 +data: tensor([0.6364, 0.8193, 0.8100, 0.0000, 0.0588, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.6869, 0.6842, 0.6100, 0.5556, 0.4706, 0.2500]) + true value: 0 + prediction: 1 +data: tensor([0.6970, 0.5483, 0.7700, 0.2222, 0.2353, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.7374, 0.9498, 0.5000, 0.4444, 0.0588, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.1919, 0.7087, 0.9600, 0.6667, 0.0588, 0.1250]) + true value: 1 + prediction: 1 +data: tensor([0.0101, 0.6848, 0.6700, 0.5556, 0.0000, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.0606, 0.6611, 0.7900, 0.1111, 0.4706, 0.6250]) + true value: 0 + prediction: 1 +data: tensor([0.6869, 0.9109, 0.1700, 0.1111, 0.0000, 0.2500]) + true value: 1 + prediction: 1 +data: tensor([0.3939, 0.3227, 0.9100, 0.5556, 0.0000, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.6566, 0.3806, 0.3600, 0.3333, 0.0588, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.3030, 0.4468, 0.0900, 0.4444, 0.3529, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.4646, 0.6644, 0.9500, 1.0000, 0.4706, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.4040, 0.2876, 0.7000, 0.5556, 0.0000, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.2020, 0.6938, 0.1600, 0.1111, 0.2941, 0.7500]) + true value: 1 + prediction: 1 +data: tensor([0.8889, 0.7290, 0.5900, 1.0000, 0.4706, 0.3750]) + true value: 0 + prediction: 1 +data: tensor([0.9495, 0.5918, 0.5400, 0.2222, 0.5294, 0.6250]) + true value: 0 + prediction: 1 +data: tensor([0.5253, 0.8635, 0.5300, 0.0000, 0.2941, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.6869, 0.2819, 0.9700, 1.0000, 0.4118, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.0404, 0.9492, 0.4100, 0.5556, 0.5294, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.5960, 0.7847, 0.7800, 0.7778, 0.5294, 0.7500]) + true value: 0 + prediction: 1 +data: tensor([0.3232, 0.9707, 0.3800, 0.4444, 0.3529, 0.7500]) + true value: 0 + prediction: 1 +data: tensor([0.9293, 0.9201, 0.9400, 0.6667, 0.2353, 0.2500]) + true value: 1 + prediction: 1 +data: tensor([0.0909, 0.3741, 0.0200, 0.5556, 0.4706, 0.0000]) + true value: 0 + prediction: 1 +data: tensor([0.4141, 0.2992, 0.0400, 0.1111, 0.4706, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.7879, 0.5942, 0.8000, 0.4444, 0.5294, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.0000, 0.3849, 0.7400, 0.5556, 0.4118, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.1111, 0.8041, 0.1700, 0.1111, 0.4706, 0.0000]) + true value: 1 + prediction: 1 +data: tensor([0.4646, 0.7263, 0.5000, 0.1111, 0.4118, 0.7500]) + true value: 0 + prediction: 1 +data: tensor([0.9596, 0.6674, 0.3400, 0.5556, 0.3529, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.9798, 0.3954, 0.8900, 0.6667, 0.4706, 0.2500]) + true value: 1 + prediction: 1 +data: tensor([0.9495, 0.3667, 0.3000, 0.8889, 0.2353, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.0000, 0.5576, 0.4600, 0.7778, 0.2353, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.0303, 0.3927, 0.6900, 0.1111, 0.3529, 0.0000]) + true value: 0 + prediction: 1 +data: tensor([0.7071, 0.4048, 0.6400, 0.6667, 0.2941, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.5859, 0.4611, 0.2500, 0.6667, 0.2941, 0.2500]) + true value: 0 + prediction: 1 +data: tensor([0.7273, 0.9597, 0.4600, 0.1111, 0.4706, 0.6250]) + true value: 0 + prediction: 1 +data: tensor([0.2323, 0.3153, 0.7900, 0.0000, 0.4118, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.8788, 0.6746, 0.4200, 0.5556, 0.4118, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.6465, 0.7507, 0.5800, 0.7778, 0.2353, 0.6250]) + true value: 0 + prediction: 1 +data: tensor([0.1414, 0.3580, 0.1700, 0.5556, 0.5294, 0.2500]) + true value: 1 + prediction: 1 +data: tensor([0.5455, 0.8748, 0.6500, 0.5556, 0.4118, 0.6250]) + true value: 0 + prediction: 1 +data: tensor([0.7677, 0.6977, 0.4400, 0.7778, 0.2941, 0.2500]) + true value: 0 + prediction: 1 +data: tensor([0.5253, 0.3340, 0.3800, 0.5556, 0.2941, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.9697, 0.5818, 0.0600, 0.5556, 0.5294, 0.3750]) + true value: 0 + prediction: 1 +data: tensor([0.5556, 0.3512, 0.5000, 0.1111, 0.5882, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.1919, 0.6824, 1.0000, 0.5556, 0.3529, 0.3750]) + true value: 0 + prediction: 1 +data: tensor([0.2929, 0.7032, 0.7400, 1.0000, 0.2941, 0.2500]) + true value: 0 + prediction: 1 +data: tensor([0.9697, 0.6644, 0.4700, 0.2222, 0.4118, 0.3750]) + true value: 0 + prediction: 1 +data: tensor([0.9192, 0.7506, 0.6400, 0.3333, 0.4118, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.3737, 0.5391, 0.3800, 0.1111, 0.2941, 0.5000]) + true value: 0 + prediction: 1 +data: tensor([0.4242, 0.3620, 0.6900, 0.4444, 0.2353, 0.0000]) + true value: 0 + prediction: 1 +data: tensor([0.5859, 0.7856, 0.8000, 0.4444, 0.4706, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.4242, 0.9730, 0.2400, 0.4444, 0.3529, 0.0000]) + true value: 1 + prediction: 1 +data: tensor([0.4848, 0.6930, 0.4500, 0.3333, 0.5294, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.5960, 0.5353, 0.8700, 0.5556, 0.5294, 0.2500]) + true value: 0 + prediction: 1 +data: tensor([0.4545, 0.8031, 0.3200, 0.4444, 0.5294, 0.1250]) + true value: 1 + prediction: 1 +data: tensor([0.7374, 0.7501, 0.9700, 0.7778, 0.2941, 0.0000]) + true value: 0 + prediction: 1 +data: tensor([0.7071, 0.5714, 0.4900, 0.4444, 0.2353, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.5253, 0.5608, 0.4600, 0.2222, 0.4118, 0.1250]) + true value: 1 + prediction: 1 +data: tensor([0.7071, 0.6546, 0.5100, 0.8889, 0.4118, 0.7500]) + true value: 0 + prediction: 1 +data: tensor([0.6162, 0.9590, 0.9600, 0.5556, 0.5294, 0.5000]) + true value: 0 + prediction: 1 +data: tensor([0.4646, 0.6024, 0.3400, 0.5556, 0.4706, 0.2500]) + true value: 1 + prediction: 1 +data: tensor([0.6566, 0.7065, 0.3200, 0.8889, 0.4706, 0.3750]) + true value: 0 + prediction: 1 +data: tensor([0.7677, 0.6529, 0.8800, 0.6667, 0.5882, 0.1250]) + true value: 1 + prediction: 1 +data: tensor([0.3131, 0.3574, 0.4200, 0.7778, 0.5294, 0.0000]) + true value: 0 + prediction: 1 +data: tensor([0.5556, 0.5638, 0.8900, 0.6667, 0.2353, 0.6250]) + true value: 0 + prediction: 1 +data: tensor([0.9899, 0.5488, 0.1200, 0.2222, 0.2941, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.4040, 0.8887, 0.3600, 0.6667, 0.2353, 0.5000]) + true value: 0 + prediction: 1 +data: tensor([0.1414, 0.8061, 0.5900, 0.1111, 0.4706, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.6061, 0.6708, 0.8200, 0.2222, 0.4118, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.6061, 0.6631, 1.0000, 0.3333, 0.4706, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.8485, 0.5505, 0.8500, 0.5556, 0.5882, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.1010, 0.4576, 0.7600, 0.5556, 0.4118, 0.1250]) + true value: 1 + prediction: 1 +data: tensor([0.6263, 0.7885, 0.1900, 0.5556, 0.2941, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.3333, 0.7216, 0.6800, 0.3333, 0.2353, 0.0000]) + true value: 0 + prediction: 1 +data: tensor([0.0202, 0.8086, 0.5100, 0.3333, 0.3529, 0.6250]) + true value: 1 + prediction: 1 +data: tensor([0.2727, 0.3898, 0.4400, 0.4444, 0.4706, 0.0000]) + true value: 0 + prediction: 1 +data: tensor([0.0404, 0.3584, 0.6100, 0.7778, 0.2353, 0.7500]) + true value: 1 + prediction: 1 +data: tensor([0.5455, 0.6261, 0.7700, 0.5556, 0.2353, 0.3750]) + true value: 1 + prediction: 1 +data: tensor([0.4747, 0.3963, 0.5500, 0.7778, 0.3529, 0.7500]) + true value: 1 + prediction: 1 +data: tensor([0.3030, 0.8790, 0.6900, 0.5556, 0.4118, 0.2500]) + true value: 1 + prediction: 1 +data: tensor([0.9495, 0.9537, 0.2400, 1.0000, 0.5294, 0.2500]) + true value: 0 + prediction: 1 +data: tensor([0.6465, 0.9225, 1.0000, 0.5556, 0.5294, 0.5000]) + true value: 1 + prediction: 1 +data: tensor([0.1919, 0.2849, 0.8900, 0.2222, 0.3529, 0.2500]) + true value: 1 + prediction: 1 +data: tensor([0.9192, 0.3851, 0.3200, 0.7778, 0.4118, 0.7500]) + true value: 1 + prediction: 1 +data: tensor([0.8788, 0.4788, 0.1400, 0.1111, 0.5294, 0.1250]) + true value: 0 + prediction: 1 +data: tensor([0.7778, 0.9586, 0.0400, 0.6667, 0.4118, 0.1250]) + true value: 0 + prediction: 1 diff --git a/train.py b/train.py new file mode 100644 index 0000000..67343f7 --- /dev/null +++ b/train.py @@ -0,0 +1,81 @@ +#!/usr/bin/python + +import pandas as pd +import numpy as np +import zadanie1 as z +import torch +import torchvision +import torchvision.transforms as transforms +import torch.nn as nn +import torch.nn.functional as F +import torch.optim as optim + + +class Net(nn.Module): + def __init__(self): + super().__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(20, 6) + self.fc3 = nn.Linear(6, 6) + + def forward(self, x): + #x = self.pool(F.relu(self.conv1(x))) + #x = self.pool(F.relu(self.conv2(x))) + #x = torch.flatten(x, 1) + #x = F.relu(self.fc1(x)) + #x = F.relu(self.fc2(x)) + x = self.fc3(x) + return x + + +def trainNet(trainloader, criterion, optimizer): + for epoch in range(20): + + for i, data in enumerate(trainloader, 0): + inputs, labels = data + + labelsX = torch.Tensor([x for x in labels]) + labels = labelsX.type(torch.LongTensor) + + optimizer.zero_grad() + + outputs = net(inputs) + + loss = criterion(outputs, labels) + loss.backward() + optimizer.step() + + print('Finished Training') + +if __name__ == '__main__': + + train, dev, test = z.prepareData() + + batch_size = 4 + + trainlist = train.values.tolist() + testlist = test.values.tolist() + + trainset = [[torch.Tensor(x[1:]), torch.Tensor([x[0]])] for x in trainlist] + trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, + shuffle=True, num_workers=2) + + testset = [[torch.Tensor(x[1:]), torch.Tensor([x[0]])] for x in testlist] + testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, + shuffle=False, num_workers=2) + + classes = ('male', 'female') + + net = Net() + + criterion = nn.CrossEntropyLoss() + optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) + + trainNet(trainloader, criterion, optimizer) + + PATH = './cifar_net.pth' + torch.save(net.state_dict(), PATH) + diff --git a/zadanie1.py b/zadanie1.py index 7a2dd49..d42dacb 100644 --- a/zadanie1.py +++ b/zadanie1.py @@ -3,34 +3,38 @@ import pandas as pd import numpy as np -data = pd.read_csv("Customers.csv") -print(data[:10]) -dataF = data +def prepareData(): + data = pd.read_csv("Customers.csv") + #print(data[:10]) -mapping = {'NaN' : 0, 'Healthcare' : 1, 'Engineer' : 2, 'Lawyer' : 3, 'Entertainment' : 4, 'Artist' : 5, 'Executive' : 6, - 'Doctor' : 7, 'Homemaker' : 8, 'Marketing' : 9} + dataF = data -mapping2 = {'Male' : 0, 'Female' : 1} + mapping = {'NaN' : 0, 'Healthcare' : 1, 'Engineer' : 2, 'Lawyer' : 3, 'Entertainment' : 4, 'Artist' : 5, 'Executive' : 6, + 'Doctor' : 7, 'Homemaker' : 8, 'Marketing' : 9} -dataF = dataF.replace({'Profession': mapping}) -dataF = dataF.replace({'Gender': mapping2}) + mapping2 = {'Male' : 0, 'Female' : 1} -dataF = dataF.drop(columns=['CustomerID']) + dataF = dataF.replace({'Profession': mapping}) + dataF = dataF.replace({'Gender': mapping2}) -dataF['Profession'] = dataF['Profession'].fillna(0) + dataF = dataF.drop(columns=['CustomerID']) -normalized_dataF = (dataF - dataF.min())/(dataF.max() - dataF.min()) + dataF['Profession'] = dataF['Profession'].fillna(0) -print(normalized_dataF[:10]) + normalized_dataF = (dataF - dataF.min())/(dataF.max() - dataF.min()) -train_data = normalized_dataF[0:1600] -dev_data = normalized_dataF[1600:1800] -test_data = normalized_dataF[1800:] + #print(normalized_dataF[:10]) -print(f"Wielkość zbioru Customers: {len(data)} elementów") -print(f"Wielkość zbioru trenującego: {len(train_data)} elementów") -print(f"Wielkość zbioru walidującego: {len(dev_data)} elementów") -print(f"Wielkość zbioru testującego: {len(test_data)} elementów") + train_data = normalized_dataF[0:1600] + dev_data = normalized_dataF[1600:1800] + test_data = normalized_dataF[1800:] -print(f" \nDane i wartości na temat zbioru: \n \n {normalized_dataF.describe()}") + #print(f"Wielkość zbioru Customers: {len(data)} elementów") + #print(f"Wielkość zbioru trenującego: {len(train_data)} elementów") + #print(f"Wielkość zbioru walidującego: {len(dev_data)} elementów") + #print(f"Wielkość zbioru testującego: {len(test_data)} elementów") + + #print(f" \nDane i wartości na temat zbioru: \n \n {normalized_dataF.describe()}") + + return train_data, dev_data, test_data