Implemented batching for TicTacToe AI

This commit is contained in:
Clemens Dautermann 2020-01-28 14:45:00 +01:00
parent 55cff9b18f
commit 56ee2635b5
96 changed files with 8426 additions and 7 deletions

View file

@ -0,0 +1,9 @@
wandb_version: 1
_wandb:
desc: null
value:
cli_version: 0.8.22
framework: torch
is_jupyter_run: false
python_version: 3.7.5

View file

@ -0,0 +1,131 @@
diff --git a/TicTacToe_AI/Net/pytorch_ai.py b/TicTacToe_AI/Net/pytorch_ai.py
index efea5ae..05da941 100644
--- a/TicTacToe_AI/Net/pytorch_ai.py
+++ b/TicTacToe_AI/Net/pytorch_ai.py
@@ -4,6 +4,11 @@ import torch.optim as optim
from torch import nn
import torch.nn.functional as F
from tqdm import tqdm
+import wandb
+
+wandb.init(project="tictactoe")
+
+BATCH_SIZE = 15
def to_set(raw_list):
@@ -35,6 +40,39 @@ def to_set(raw_list):
return out_set
+def to_batched_set(raw_list):
+ counter = 0
+ out_set = []
+ boardtensor = torch.zeros((BATCH_SIZE, 1, 9))
+ labeltensor = torch.zeros(BATCH_SIZE)
+ for line in tqdm(raw_list):
+ line = line.replace('\n', '')
+ raw_board, raw_label = line.split('|')[0], line.split('|')[1]
+
+ if not (int(raw_label) is -1):
+ labeltensor[counter] = int(raw_label)
+ else:
+ labeltensor[counter] = 9
+
+ for n, block in enumerate(raw_board):
+ if int(block) is -1:
+ boardtensor[counter][0][n] = 0
+ elif int(block) is 0:
+ boardtensor[counter][0][n] = 0.5
+ elif int(block) is 1:
+ boardtensor[counter][0][n] = 1
+
+ if counter == (BATCH_SIZE - 1):
+ out_set.append([boardtensor, labeltensor])
+ boardtensor = torch.zeros((BATCH_SIZE, 1, 9))
+ labeltensor = torch.zeros(BATCH_SIZE)
+ counter = 0
+ else:
+ counter += 1
+
+ return out_set
+
+
def buildsets():
with open('boards.bds', 'r') as infile:
print('Loading file...')
@@ -44,9 +82,11 @@ def buildsets():
print('Generating testset...')
testset = to_set(alllines[0:10000])
+ print(testset[0])
+ exit(0)
print('Generating trainset...')
- trainset = to_set(alllines[10001:200000])
+ trainset = to_set(alllines[10001:100000])
return trainset, testset
@@ -60,6 +100,7 @@ def testnet(net, testset):
if torch.argmax(output) == label[0]:
correct += 1
total += 1
+ wandb.log({'test_accuracy': correct / total})
print("Accuracy: ", round(correct / total, 3))
@@ -79,7 +120,15 @@ class Net(torch.nn.Module):
return F.log_softmax(x, dim=1)
-net = torch.load('./nets/net_3.pt')
+device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
+print('running on %s' % device)
+
+# net = torch.load('./nets/net_3.pt')
+
+net = Net()
+wandb.watch(net)
+
+net.to(device)
optimizer = optim.Adam(net.parameters(), lr=0.001)
@@ -87,13 +136,16 @@ trainset, testset = buildsets()
for epoch in range(100):
print('Epoch: ' + str(epoch))
+ wandb.log({'epoch': epoch})
for X, label in tqdm(trainset):
net.zero_grad()
+ X.to(device)
output = net(X)
+ output.cpu()
loss = F.nll_loss(output.view(1, 10), label[0])
loss.backward()
optimizer.step()
+ wandb.log({'loss': loss})
- print(loss)
- torch.save(net, './nets/net_' + str(epoch + 3) + '.pt')
+ torch.save(net, './nets/gpunets/net_' + str(epoch) + '.pt')
testnet(net, testset)
diff --git a/other_scripts/setcounter.py b/other_scripts/setcounter.py
index 9735f20..e9eb00c 100644
--- a/other_scripts/setcounter.py
+++ b/other_scripts/setcounter.py
@@ -7,9 +7,12 @@ data = datasets.MNIST('../datasets', train=True, download=True,
transforms.ToTensor()
]))
-loader = torch.utils.data.DataLoader(data, batch_size=1, shuffle=False)
+loader = torch.utils.data.DataLoader(data, batch_size=15, shuffle=False)
set = {'0': 0, '1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9': 0}
+for data in loader:
+ print(data[1].shape)
+
for _, label in tqdm(loader):
set[str(label[0].item())] += 1

View file

@ -0,0 +1,6 @@
running on cpu
Loading file...
986410
Generating testset...
0%| | 0/10000 [00:00<?, ?it/s] 3%|█▏ | 329/10000 [00:00<00:02, 3284.96it/s] 7%|██▌ | 702/10000 [00:00<00:02, 3406.48it/s] 11%|███▊ | 1071/10000 [00:00<00:02, 3484.39it/s] 14%|█████▏ | 1438/10000 [00:00<00:02, 3535.76it/s] 18%|██████▌ | 1806/10000 [00:00<00:02, 3576.26it/s] 21%|███████▌ | 2110/10000 [00:00<00:04, 1883.99it/s] 25%|████████▉ | 2483/10000 [00:00<00:03, 2211.89it/s] 29%|██████████▎ | 2855/10000 [00:01<00:02, 2517.61it/s] 32%|███████████▌ | 3225/10000 [00:01<00:02, 2783.04it/s] 36%|████████████▉ | 3592/10000 [00:01<00:02, 3000.32it/s] 40%|██████████████▎ | 3964/10000 [00:01<00:01, 3183.21it/s] 43%|███████████████▌ | 4339/10000 [00:01<00:01, 3333.87it/s] 47%|████████████████▉ | 4698/10000 [00:01<00:01, 3403.56it/s] 51%|██████████████████▎ | 5072/10000 [00:01<00:01, 3497.17it/s] 54%|███████████████████▌ | 5442/10000 [00:01<00:01, 3554.89it/s] 58%|████████████████████▉ | 5815/10000 [00:01<00:01, 3605.52it/s] 62%|██████████████████████▎ | 6191/10000 [00:01<00:01, 3648.30it/s] 66%|███████████████████████▋ | 6565/10000 [00:02<00:00, 3674.90it/s] 69%|████████████████████████▉ | 6936/10000 [00:02<00:00, 3649.80it/s] 73%|██████████████████████████▎ | 7307/10000 [00:02<00:00, 3667.44it/s] 77%|███████████████████████████▋ | 7676/10000 [00:02<00:00, 3637.19it/s] 80%|████████████████████████████▉ | 8041/10000 [00:02<00:00, 3634.91it/s] 84%|██████████████████████████████▎ | 8413/10000 [00:02<00:00, 3658.31it/s] 88%|███████████████████████████████▋ | 8785/10000 [00:02<00:00, 3676.50it/s] 92%|████████████████████████████████▉ | 9154/10000 [00:02<00:00, 3640.13it/s] 95%|██████████████████████████████████▎ | 9527/10000 [00:02<00:00, 3666.31it/s] 99%|███████████████████████████████████▋| 9900/10000 [00:02<00:00, 3683.42it/s] 100%|███████████████████████████████████| 10000/10000 [00:02<00:00, 3354.62it/s]
(tensor([[0., 0., 1., 0., 1., 1., 1., 1., 0.]]), tensor([[9]]))

View file

@ -0,0 +1,109 @@
apturl==0.5.2
argh==0.26.2
asn1crypto==0.24.0
bcrypt==3.1.6
binwalk==2.1.2
blinker==1.4
brlapi==0.6.7
certifi==2018.8.24
chardet==3.0.4
click==7.0
command-not-found==0.3
configparser==4.0.2
cryptography==2.6.1
cupshelpers==1.0
cycler==0.10.0
dbus-python==1.2.12
decorator==4.3.0
defer==1.0.6
distro-info==0.21ubuntu4
distro==1.3.0
docker-pycreds==0.4.0
duplicity==0.8.4
entrypoints==0.3
fasteners==0.12.0
future==0.16.0
gitdb2==2.0.6
gitpython==3.0.5
gql==0.2.0
graphql-core==1.1
httplib2==0.11.3
idna==2.6
keyring==18.0.1
keyrings.alt==3.1.1
kiwisolver==1.0.1
language-selector==0.1
launchpadlib==1.10.7
lazr.restfulclient==0.14.2
lazr.uri==1.0.3
lockfile==0.12.2
louis==3.10.0
macaroonbakery==1.2.3
mako==1.0.7
markupsafe==1.1.0
matplotlib==3.0.2
monotonic==1.5
netifaces==0.10.4
numpy==1.16.2
nvidia-ml-py3==7.352.0
oauth==1.0.1
oauthlib==2.1.0
olefile==0.46
paramiko==2.6.0
pathtools==0.1.2
pexpect==4.6.0
pillow==6.1.0
pip==18.1
promise==2.3
protobuf==3.6.1
psutil==5.6.7
pycairo==1.16.2
pycrypto==2.6.1
pycups==1.9.73
pygments==2.3.1
pygobject==3.34.0
pyjwt==1.7.0
pymacaroons==0.13.0
pynacl==1.3.0
pyopengl==3.1.0
pyparsing==2.2.0
pyqt5==5.12.3
pyqtgraph==0.11.0.dev0
pyrfc3339==1.1
python-apt==1.9.0+ubuntu1.3
python-dateutil==2.7.3
python-debian==0.1.36
pytz==2019.2
pyxdg==0.25
pyyaml==5.1.2
reportlab==3.5.23
requests-unixsocket==0.1.5
requests==2.21.0
scipy==1.2.2
secretstorage==2.3.1
sentry-sdk==0.14.0
setuptools==41.1.0
shortuuid==0.5.0
simplejson==3.16.0
sip==4.19.18
six==1.12.0
smmap2==2.0.5
subprocess32==3.5.4
system-service==0.3
systemd-python==234
torch==1.3.1+cpu
torchvision==0.4.2+cpu
tqdm==4.41.0
ubuntu-advantage-tools==19.5
ubuntu-drivers-common==0.0.0
ufw==0.36
unattended-upgrades==0.1
urllib3==1.24.1
usb-creator==0.3.7
virtualenv==15.1.0
wadllib==1.3.3
wandb==0.8.22
watchdog==0.9.0
wheel==0.32.3
xkit==0.0.0
zope.interface==4.3.2

View file

@ -0,0 +1 @@
{"system.cpu": 51.8, "system.memory": 44.84, "system.disk": 8.1, "system.proc.memory.availableMB": 4248.14, "system.proc.memory.rssMB": 152.26, "system.proc.memory.percent": 1.98, "system.proc.cpu.threads": 1.2, "system.network.sent": 28306, "system.network.recv": 87546, "_wandb": true, "_timestamp": 1580205867, "_runtime": 7}

View file

@ -0,0 +1,23 @@
{
"root": "/home/clemens/repositorys/pytorch-ai",
"program": "pytorch_ai.py",
"git": {
"remote": "git@github.com:Clemens-Dautermann/pytorch-ai.git",
"commit": "55cff9b18f8558ae7a9170e56a3d5c6f6665d9ab"
},
"email": "clemens.dautermann@gmail.com",
"startedAt": "2020-01-28T10:04:19.609416",
"host": "ubuntu-laptop",
"username": "clemens",
"executable": "/usr/bin/python3",
"os": "Linux-5.3.0-26-generic-x86_64-with-Ubuntu-19.10-eoan",
"python": "3.7.5",
"cpu_count": 2,
"args": [],
"state": "finished",
"jobType": null,
"mode": "run",
"project": "tictactoe",
"heartbeatAt": "2020-01-28T10:04:28.075857",
"exitcode": 0
}