Repository navigation
Expand file tree
/
Copy pathdual_net.h
More file actions
87 lines (71 loc) · 3.09 KB
/
Copy pathdual_net.h
File metadata and controls
87 lines (71 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CC_DUAL_NET_DUAL_NET_H_
#define CC_DUAL_NET_DUAL_NET_H_
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/types/span.h"
#include "cc/constants.h"
#include "cc/position.h"
namespace minigo {
// The input features to the DualNet neural network have 17 binary feature
// planes. 8 feature planes X_t indicate the presence of the current player's
// stones at time t. A further 8 feature planes Y_t indicate the presence of
// the opposing player's stones at time t. The final feature plane C holds all
// 1s if black is to play, or 0s if white is to play. The planes are
// concatenated together to give input features:
// [X_t, Y_t, X_t-1, Y_t-1, ..., X_t-7, Y_t-7, C].
class DualNet {
public:
// Size of move history in the stone features.
static constexpr int kMoveHistory = 8;
// Number of features per stone.
static constexpr int kNumStoneFeatures = kMoveHistory * 2 + 1;
// Index of the per-stone feature that describes whether the black or white
// player is to play next.
static constexpr int kPlayerFeature = kMoveHistory * 2;
// Total number of features for the board.
static constexpr int kNumBoardFeatures = kN * kN * kNumStoneFeatures;
// TODO(tommadams): Change features type from float to uint8_t.
using StoneFeatures = std::array<float, kNumStoneFeatures>;
using BoardFeatures = std::array<float, kNumBoardFeatures>;
// Generates the board features from the history of recent moves, where
// history[0] is the current board position, and history[i] is the board
// position from i moves ago.
// history.size() must be <= kMoveHistory.
// TODO(tommadams): Move Position::Stones out of the Position class so we
// don't need to depend on position.h.
static void SetFeatures(absl::Span<const Position::Stones* const> history,
Color to_play, BoardFeatures* features);
struct Output {
std::array<float, kNumMoves> policy;
float value;
};
virtual ~DualNet();
// Runs inference on a batch of input features.
// If `model` is non-null, it will be set with the name of the model used for
// the inference.
virtual void RunMany(absl::Span<const BoardFeatures> features,
absl::Span<Output> outputs, std::string* model) = 0;
// Runs inference on features from a single position.
Output Run(const BoardFeatures features, std::string* model) {
Output output;
RunMany({&features, 1}, {&output, 1}, model);
return output;
}
};
} // namespace minigo
#endif // CC_DUAL_NET_DUAL_NET_H_