Skip to content

Commit 26aded5

Browse files
authored
Research/minigo unit test (tensorflow#4023)
* Add minigo unit test * Add minigo unit test * Fix gpylints and update readme
1 parent c86f791 commit 26aded5

20 files changed

+2646
-244
lines changed

research/minigo/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ parameterized layers respectively for the residual tower, plus an additional 2
5656
layers for the policy head and 3 layers for the value head.
5757

5858
## Getting Started
59-
Please follow the [instructions](https://github.com/tensorflow/minigo/blob/master/README.md#getting-started) in original Minigo repo to set up the environment.
59+
This project assumes you have virtualenv, TensorFlow (>= 1.5) and two other Go-related
60+
packages pygtp(>=0.4) and sgf (==0.5).
61+
6062

6163
## Training Model
6264
One iteration of reinforcement learning consists of the following steps:

research/minigo/coords.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
KGS 'A19' 'T19' 'pass'
4141
pygtp (1, 19) (19, 19) (0, 0)
4242
"""
43+
from __future__ import absolute_import
44+
from __future__ import division
45+
from __future__ import print_function
4346

4447
import gtp
4548

research/minigo/coords_test.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
"""Tests for coords."""
16+
from __future__ import absolute_import
17+
from __future__ import division
18+
from __future__ import print_function
19+
20+
import tensorflow as tf # pylint: disable=g-bad-import-order
21+
22+
import coords
23+
import numpy
24+
import utils_test
25+
26+
tf.logging.set_verbosity(tf.logging.ERROR)
27+
28+
29+
class TestCoords(utils_test.MiniGoUnitTest):
30+
31+
def test_upperleft(self):
32+
self.assertEqual(coords.from_sgf('aa'), (0, 0))
33+
self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 0), (0, 0))
34+
self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'A9'), (0, 0))
35+
self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (1, 9)), (0, 0))
36+
37+
self.assertEqual(coords.to_sgf((0, 0)), 'aa')
38+
self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 0)), 0)
39+
self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, (0, 0)), 'A9')
40+
self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (0, 0)), (1, 9))
41+
42+
def test_topleft(self):
43+
self.assertEqual(coords.from_sgf('ia'), (0, 8))
44+
self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 8), (0, 8))
45+
self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'J9'), (0, 8))
46+
self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (9, 9)), (0, 8))
47+
48+
self.assertEqual(coords.to_sgf((0, 8)), 'ia')
49+
self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 8)), 8)
50+
self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, (0, 8)), 'J9')
51+
self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (0, 8)), (9, 9))
52+
53+
def test_pass(self):
54+
self.assertEqual(coords.from_sgf(''), None)
55+
self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 81), None)
56+
self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'pass'), None)
57+
self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (0, 0)), None)
58+
59+
self.assertEqual(coords.to_sgf(None), '')
60+
self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, None), 81)
61+
self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, None), 'pass')
62+
self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, None), (0, 0))
63+
64+
def test_parsing_9x9(self):
65+
self.assertEqual(coords.from_sgf('aa'), (0, 0))
66+
self.assertEqual(coords.from_sgf('ac'), (2, 0))
67+
self.assertEqual(coords.from_sgf('ca'), (0, 2))
68+
self.assertEqual(coords.from_sgf(''), None)
69+
self.assertEqual(coords.to_sgf(None), '')
70+
self.assertEqual('aa', coords.to_sgf(coords.from_sgf('aa')))
71+
self.assertEqual('sa', coords.to_sgf(coords.from_sgf('sa')))
72+
self.assertEqual((1, 17), coords.from_sgf(coords.to_sgf((1, 17))))
73+
self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'A1'), (8, 0))
74+
self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'A9'), (0, 0))
75+
self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'C2'), (7, 2))
76+
self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'J2'), (7, 8))
77+
self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (1, 1)), (8, 0))
78+
self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (1, 9)), (0, 0))
79+
self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (3, 2)), (7, 2))
80+
self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (8, 0)), (1, 1))
81+
self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (0, 0)), (1, 9))
82+
self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (7, 2)), (3, 2))
83+
84+
self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, (0, 8)), 'J9')
85+
self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, (8, 0)), 'A1')
86+
87+
def test_flatten(self):
88+
self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 0)), 0)
89+
self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 3)), 3)
90+
self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (3, 0)), 27)
91+
self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 27), (3, 0))
92+
self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 10), (1, 1))
93+
self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 80), (8, 8))
94+
self.assertEqual(coords.to_flat(
95+
utils_test.BOARD_SIZE, coords.from_flat(utils_test.BOARD_SIZE, 10)), 10)
96+
self.assertEqual(coords.from_flat(
97+
utils_test.BOARD_SIZE, coords.to_flat(
98+
utils_test.BOARD_SIZE, (5, 4))), (5, 4))
99+
100+
def test_from_flat_ndindex_equivalence(self):
101+
ndindices = list(numpy.ndindex(
102+
utils_test.BOARD_SIZE, utils_test.BOARD_SIZE))
103+
flat_coords = list(range(
104+
utils_test.BOARD_SIZE * utils_test.BOARD_SIZE))
105+
def _from_flat(flat_coords):
106+
return coords.from_flat(utils_test.BOARD_SIZE, flat_coords)
107+
self.assertEqual(
108+
list(map(_from_flat, flat_coords)), ndindices)
109+
110+
111+
if __name__ == '__main__':
112+
tf.test.main()

research/minigo/dualnet_test.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
"""Tests for dualnet and dualnet_model."""
16+
from __future__ import absolute_import
17+
from __future__ import division
18+
from __future__ import print_function
19+
20+
import os
21+
import tempfile
22+
23+
import tensorflow as tf # pylint: disable=g-bad-import-order
24+
25+
import dualnet
26+
import go
27+
import model_params
28+
import preprocessing
29+
import utils_test
30+
31+
tf.logging.set_verbosity(tf.logging.ERROR)
32+
33+
34+
class TestDualNet(utils_test.MiniGoUnitTest):
35+
36+
def test_train(self):
37+
with tempfile.TemporaryDirectory() as working_dir, \
38+
tempfile.NamedTemporaryFile() as tf_record:
39+
preprocessing.make_dataset_from_sgf(
40+
utils_test.BOARD_SIZE, 'example_game.sgf', tf_record.name)
41+
dualnet.train(
42+
working_dir, [tf_record.name], 1, model_params.DummyMiniGoParams())
43+
44+
def test_inference(self):
45+
with tempfile.TemporaryDirectory() as working_dir, \
46+
tempfile.TemporaryDirectory() as export_dir:
47+
dualnet.bootstrap(working_dir, model_params.DummyMiniGoParams())
48+
exported_model = os.path.join(export_dir, 'bootstrap-model')
49+
dualnet.export_model(working_dir, exported_model)
50+
51+
n1 = dualnet.DualNetRunner(
52+
exported_model, model_params.DummyMiniGoParams())
53+
n1.run(go.Position(utils_test.BOARD_SIZE))
54+
55+
n2 = dualnet.DualNetRunner(
56+
exported_model, model_params.DummyMiniGoParams())
57+
n2.run(go.Position(utils_test.BOARD_SIZE))
58+
59+
60+
if __name__ == '__main__':
61+
tf.test.main()
62+

research/minigo/example_game.sgf

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
(;GM[1]FF[4]CA[UTF-8]AP[CGoban:3]ST[2]
2+
RU[Japanese]SZ[9]KM[0.00]
3+
PW[White]PB[Black]RE[B+4.00]
4+
;B[de]
5+
;W[fe]
6+
;B[ee]
7+
;W[fd]
8+
;B[ff]
9+
;W[gf]
10+
;B[gg]
11+
;W[fg]
12+
;B[ef]
13+
;W[gh]
14+
;B[hg]
15+
;W[hh]
16+
;B[eg]
17+
;W[fh]
18+
;B[ge]
19+
;W[hf]
20+
;B[he]
21+
;W[ig]
22+
;B[fc]
23+
;W[gd]
24+
;B[gc]
25+
;W[hd]
26+
;B[ed]
27+
;W[be]
28+
;B[hc]
29+
;W[ie]
30+
;B[bc]
31+
;W[cg]
32+
;B[cf]
33+
;W[bf]
34+
;B[ch]
35+
(;W[dg]
36+
;B[dh]
37+
;W[bh]
38+
;B[eh]
39+
;W[cc]
40+
;B[cb])
41+
(;W[cc]
42+
;B[cb]
43+
(;W[bh]
44+
;B[dh])
45+
(;W[dg]
46+
;B[dh]
47+
;W[bh]
48+
;B[eh]
49+
;W[dc]
50+
;B[bd]
51+
;W[ec]
52+
;B[cd]
53+
;W[fb]
54+
;B[gb]
55+
(;W[db])
56+
(;W[bb]
57+
;B[eb]
58+
;W[db]
59+
;B[fa]
60+
;W[ca]
61+
;B[ea]
62+
;W[da]
63+
;B[df]
64+
;W[bg]
65+
;B[bi]
66+
;W[ab]
67+
;B[ah]
68+
;W[ci]
69+
;B[di]
70+
;W[ag]
71+
;B[ae]
72+
;W[ac]
73+
;B[ad]
74+
;W[ha]
75+
;B[hb]
76+
;W[fi]
77+
;B[ce]
78+
;W[ai]
79+
;B[ci]
80+
;W[ei]
81+
;B[ah]
82+
;W[ic]
83+
;B[ib]
84+
;W[ai]
85+
;B[ba]
86+
;W[aa]
87+
;B[ah]
88+
;W[ga]
89+
;B[ia]
90+
;W[ai]
91+
;B[ga]
92+
;W[id]
93+
;B[ah]
94+
;W[dd]
95+
;B[af]TW[ba][cb][ge][he][if][gg][hg][ih][gi][hi][ii]TB[ha][fb][be][bf][ag][bg][cg][dg][bh][ai]))))

0 commit comments

Comments
 (0)