/
ou_fokker_planck.sc
319 lines (253 loc) · 8.17 KB
/
ou_fokker_planck.sc
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import _root_.io.github.tailhq.dynaml.pipes.{DataPipe, MetaPipe}
import _root_.io.github.tailhq.dynaml.analysis
import _root_.io.github.tailhq.dynaml.probability._
import _root_.io.github.tailhq.dynaml.graphics.plot3d
import _root_.io.github.tailhq.dynaml.graphics.plot3d._
import _root_.io.github.tailhq.dynaml.utils
import _root_.io.github.tailhq.dynaml.analysis.implicits._
import _root_.io.github.tailhq.dynaml.tensorflow._
import _root_.io.github.tailhq.dynaml.tensorflow.layers.{
L1Regularization,
L2Regularization
}
import _root_.io.github.tailhq.dynaml.tensorflow.pde.{source => q, _}
import _root_.org.platanios.tensorflow.api.learn.Mode
import _root_.org.platanios.tensorflow.api.learn.layers.Layer
import _root_.io.github.tailhq.dynaml.repl.Router.main
import ammonite.ops.home
import org.joda.time.DateTime
import _root_.org.platanios.tensorflow.api._
import org.platanios.tensorflow.api.ops.training.optimizers.Optimizer
import spire.algebra.InnerProductSpace
import scala.util.Random
val random = new Random()
def batch[T: TF: IsFloatOrDouble](
dim: Int,
min: Seq[T],
max: Seq[T],
gridSize: Int,
func: Seq[T] => T
)(
implicit f: InnerProductSpace[T, Double]
): (Tensor[T], Tensor[T]) = {
val points = utils.combine(
Seq.tabulate(dim)(i => utils.range(min(i), max(i), gridSize) :+ max(i))
)
val targets = points.map(func)
(
dtf
.tensor_from[T](Seq.fill(dim)(gridSize + 1).product, dim)(points.flatten),
dtf.tensor_from[T](Seq.fill(dim)(gridSize + 1).product, 1)(targets)
)
}
val layer = new Layer[Output[Float], Output[Float]]("Sin") {
override val layerType = "Sin"
override def forwardWithoutContext(
input: Output[Float]
)(
implicit mode: Mode
): Output[Float] =
tf.sin(input)
}
def plot_field(x: Tensor[Float], t: Tensor[Float]): DelauneySurface = {
val size = x.shape(0)
val data = (0 until size).map(row => {
val inputs = (
x(row, 0).scalar.asInstanceOf[Double],
x(row, 1).scalar.asInstanceOf[Double]
)
val output = t(row).scalar.asInstanceOf[Double]
(inputs, output)
})
plot3d.draw(data)
}
@main
def apply(
num_data: Int = 100,
num_neurons: Seq[Int] = Seq(5, 5),
optimizer: Optimizer = tf.train.Adam(0.01f),
iterations: Int = 50000,
reg: Double = 0.001,
reg_sources: Double = 0.001,
pde_wt: Double = 1.5
) = {
val session = Session()
val tempdir = home / "tmp"
val summary_dir = tempdir / s"dtf_fokker_planck_test-${DateTime.now().toString("YYYY-MM-dd-HH-mm-ss")}"
val domain = (-5.0, 5.0)
val time_domain = (0d, 10d)
val input_dim: Int = 2
val output_dim: Int = 1
val diff = 1.5
val x0 = domain._2 - 1.5d
val th = 0.25
val ground_truth = (tl: Seq[Float]) => {
val (t, x) = (tl.head, tl.last)
(math.sqrt(th / (2 * math.Pi * diff * (1 - math.exp(-2 * th * t)))) *
math.exp(
-1 * th * math
.pow(x - x0 * math.exp(-th * t), 2) / (2 * diff * (1 - math
.exp(-2 * th * t)))
)).toFloat
}
val f1 = (l: Double) => if (l == x0) 1d else 0d
val (test_data, test_targets) = batch[Float](
input_dim,
Seq(time_domain._1.toFloat, domain._1.toFloat),
Seq(time_domain._2.toFloat, domain._2.toFloat),
gridSize = 20,
ground_truth
)
val xs = utils.range(domain._1, domain._2, num_data) ++ Seq(domain._2)
val rv = UniformRV(time_domain._1, time_domain._2)
val training_data =
dtfdata.supervised_dataset[Tensor[Float], Tensor[Float]](
data = xs.flatMap(x => {
val rand_time = rv.draw.toFloat
Seq(
(
dtf.tensor_f32(input_dim)(0f, x.toFloat),
dtf.tensor_f32(output_dim)(f1(x).toFloat)
),
(
dtf.tensor_f32(input_dim)(rand_time, x.toFloat),
dtf.tensor_f32(output_dim)(
ground_truth(Seq(rand_time.toFloat, x.toFloat))
)
)
)
})
)
val input = Shape(input_dim)
val output = Shape(output_dim)
val architecture =
dtflearn.feedforward_stack[Float](
(i: Int) =>
if (i == 1) tf.learn.ReLU(s"Act_$i") else tf.learn.Sigmoid(s"Act_$i")
)(num_neurons ++ Seq(1)) >>
tf.learn.Sigmoid("Act_Output")
val x_layer = dtflearn.layer(
name = "X",
MetaPipe[Mode, Output[Float], Output[Float]](_ => xt => xt(---, 1 ::))
)
val theta_layer = tf.learn.Linear[Float]("theta", 1) >> dtflearn.layer(
name = "Act_theta",
MetaPipe[Mode, Output[Float], Output[Float]](_ => xt => xt.pow(2))
)
val diff_layer = tf.learn.Linear[Float]("D", 1) >> dtflearn.layer(
name = "Act_D",
MetaPipe[Mode, Output[Float], Output[Float]](_ => xt => xt.pow(2))
)
val (_, layer_shapes, layer_parameter_names, layer_datatypes) =
dtfutils.get_ffstack_properties(
d = input_dim,
num_pred_dims = 1,
num_neurons
)
val (th_parameters, th_shapes, th_dt) =
(Seq("theta/Weights"), Seq(Shape(input_dim, 1)), Seq("FLOAT32"))
val (d_parameters, d_shapes, d_dt) =
(Seq("D/Weights"), Seq(Shape(input_dim, 1)), Seq("FLOAT32"))
val th_scopes =
th_parameters.map(_ => "" /*dtfutils.get_scope(theta_layer)*/ )
val d_scopes = d_parameters.map(_ => "" /*dtfutils.get_scope(diff_layer)*/ )
val scope = dtfutils.get_scope(architecture) _
val layer_scopes =
layer_parameter_names.map(n => "" /*scope(n.split("/").last)*/ )
val theta = q[Output[Float], Float](
name = "theta",
theta_layer,
isSystemVariable = true
)
val D =
q[Output[Float], Float](name = "D", diff_layer, isSystemVariable = true)
val x = q[Output[Float], Float](name = "X", x_layer, isSystemVariable = false)
val ornstein_ulhenbeck: Operator[Output[Float], Output[Float]] =
d_t - theta * d_s(x * I[Float, Float]()) - d_s(D * d_s)
val analysis.GaussianQuadrature(nodes, weights) =
analysis.eightPointGaussLegendre.scale(domain._1, domain._2)
val analysis.GaussianQuadrature(nodes_t, weights_t) =
analysis.eightPointGaussLegendre.scale(time_domain._1, time_domain._2)
val nodes_tensor: Tensor[Float] = dtf.tensor_f32(
nodes.length * nodes_t.length,
2
)(
utils.combine(Seq(nodes_t.map(_.toFloat), nodes.map(_.toFloat))).flatten: _*
)
val weights_tensor: Tensor[Float] =
dtf.tensor_f32(nodes.length * nodes_t.length)(
utils
.combine(Seq(weights_t.map(_.toFloat), weights.map(_.toFloat)))
.map(_.product): _*
)
val loss_func =
tf.learn.L2Loss[Float, Float]("Loss/L2") >>
tf.learn.Mean[Float]("L2/Mean")
val reg_y = L2Regularization[Float](
layer_scopes,
layer_parameter_names,
layer_datatypes,
layer_shapes,
reg
)
val reg_v = L1Regularization[Float](
th_scopes ++ d_scopes,
th_parameters ++ d_parameters,
th_dt ++ d_dt,
th_shapes ++ d_shapes,
reg_sources
)
val wave_system1d = dtflearn.pde_system[Float, Float, Float](
architecture,
ornstein_ulhenbeck,
input,
output,
loss_func,
nodes_tensor,
weights_tensor,
Tensor(pde_wt.toFloat).reshape(Shape()),
reg_f = Some(reg_y),
reg_v = Some(reg_v)
)
val wave_model1d = wave_system1d.solve(
training_data,
dtflearn.model.trainConfig(
summary_dir,
dtflearn.model.data_ops(
training_data.size / 10,
training_data.size / 4,
10
),
optimizer,
dtflearn.abs_loss_change_stop(0.001, iterations),
Some(dtflearn.model._train_hooks(summary_dir))
),
dtflearn.model.tf_data_handle_ops(
bufferSize = training_data.size / 10,
patternToTensor = Some(wave_system1d.pattern_to_tensor)
)
)
print("Test Data Shapes: ")
pprint.pprintln(test_data.shape)
pprint.pprintln(test_targets.shape)
val predictions = wave_model1d.predict("Output", "theta", "D")(test_data)
val plot = plot_field(test_data, predictions.head)
val plot_th = plot_field(test_data, predictions(1))
val plot_d = plot_field(test_data, predictions(2))
val error_tensor = tfi.subtract(predictions.head, test_targets)
val mae = tfi.mean(tfi.abs(error_tensor)).scalar
session.close()
print("Test Error is = ")
pprint.pprintln(mae)
val error_plot = plot_field(test_data, error_tensor)
(
wave_system1d,
wave_model1d,
training_data,
plot,
error_plot,
plot_th,
plot_d,
mae
)
}