-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.fs
249 lines (205 loc) · 6.42 KB
/
Model.fs
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
namespace DeepKuhnPoker
open TorchSharp
open TorchSharp.Modules
open type torch
open type torch.nn
open type torch.optim
open FSharp.Core.Operators // reclaim "float32" and other F# operators
open MathNet.Numerics.LinearAlgebra
/// Neural network that maps an input tensor to an output
/// tensor.
type Network = Module<Tensor, Tensor>
/// Loss function.
type Loss = Loss<Tensor, Tensor, Tensor>
module Network =
/// Length of neural network input.
let inputSize = KuhnPoker.Encoding.encodedLength
/// Length of neural network output.
let outputSize = KuhnPoker.actions.Length
/// An observed advantage event.
type AdvantageSample =
{
/// Key of info set.
InfoSetKey : string
/// Observed regrets.
Regrets : Vector<float32>
/// 0-based iteration number.
Iteration : int
}
module AdvantageSample =
/// Creates an advantage sample.
let create infoSetKey regrets iteration =
{
InfoSetKey = infoSetKey
Regrets = regrets
Iteration = iteration
}
/// Model used for learning advantages.
type AdvantageModel =
{
/// Neural network.
Network : Network
/// Training optimizer.
Optimizer : Optimizer
/// Training loss function.
Loss : Loss
}
module AdvantageModel =
/// Creates an advantage model.
let create hiddenSize learningRate =
let network =
Sequential(
Linear(Network.inputSize, hiddenSize),
ReLU(),
Linear(hiddenSize, Network.outputSize))
{
Network = network
Optimizer =
Adam(
network.parameters(),
lr = learningRate)
Loss = MSELoss()
}
/// Gets the advantage for the given info set.
let getAdvantage infoSetKey model =
(infoSetKey
|> KuhnPoker.Encoding.encodeInput
|> tensor)
--> model.Network
/// Trains the given model using the given samples.
let train numSteps samples model =
// prepare training data
let inputs =
samples
|> Seq.map (fun sample ->
sample.InfoSetKey
|> KuhnPoker.Encoding.encodeInput)
|> array2D
|> tensor
let targets =
samples
|> Seq.map (fun sample ->
sample.Regrets)
|> array2D
|> tensor
let iters =
samples
|> Seq.map (fun sample ->
(sample.Iteration + 1) // make 1-based
|> float32
|> sqrt
|> Seq.singleton )
|> array2D
|> tensor
[|
for _ = 1 to numSteps do
// forward pass
let loss =
let outputs = inputs --> model.Network
model.Loss.forward(
iters * outputs, // favor later iterations
iters * targets)
// backward pass and optimize
model.Optimizer.zero_grad()
loss.backward()
model.Optimizer.step() |> ignore
loss.item<float32>()
|]
/// An observed strategy event.
type StrategySample =
{
/// Key of info set.
InfoSetKey : string
/// Observed strategy.
Strategy : Vector<float32>
/// O-based iteration number.
Iteration : int
}
module StrategySample =
/// Creates a strategy sample.
let create infoSetKey strategy iteration =
{
/// Key of info set.
InfoSetKey = infoSetKey
/// Observed strategy.
Strategy = strategy
/// 0-based iteration number.
Iteration = iteration
}
/// Model used for learning strategy.
type StrategyModel =
{
/// Neural network.
Network : Network
/// Training optimizer.
Optimizer : Optimizer
/// Training loss function.
Loss : Loss
/// Softmax layer.
Softmax : Softmax
}
module StrategyModel =
/// Creates a strategy model.
let create hiddenSize learningRate =
let network =
Sequential(
Linear(Network.inputSize, hiddenSize),
ReLU(),
Linear(hiddenSize, Network.outputSize))
{
Network = network
Optimizer =
Adam(
network.parameters(),
lr = learningRate)
Loss = MSELoss()
Softmax = Softmax(dim = -1)
}
/// Trains the given model using the given samples.
let train numSteps samples model =
// prepare training data
let inputs =
samples
|> Seq.map (fun sample ->
sample.InfoSetKey
|> KuhnPoker.Encoding.encodeInput)
|> array2D
|> tensor
let targets =
samples
|> Seq.map (fun sample ->
sample.Strategy)
|> array2D
|> tensor
let iters =
samples
|> Seq.map (fun sample ->
(sample.Iteration + 1) // make 1-based
|> float32
|> sqrt
|> Seq.singleton )
|> array2D
|> tensor
[|
for _ = 1 to numSteps do
// forward pass
let loss =
let outputs =
(inputs --> model.Network)
|> model.Softmax.forward
model.Loss.forward(
iters * outputs, // favor later iterations
iters * targets)
// backward pass and optimize
model.Optimizer.zero_grad()
loss.backward()
model.Optimizer.step() |> ignore
loss.item<float32>()
|]
/// Gets the strategy for the given info set.
let getStrategy infoSetKey model =
(infoSetKey
|> KuhnPoker.Encoding.encodeInput
|> tensor)
--> model.Network
|> model.Softmax.forward