-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathsolver.py
140 lines (115 loc) · 5.39 KB
/
solver.py
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
import logging
import time
import numpy as np
import tensorflow as tf
DELTA_CLIP = 50.0
class BSDESolver(object):
def __init__(self, config, bsde):
self.eqn_config = config.eqn_config
self.net_config = config.net_config
self.bsde = bsde
self.model = NonsharedModel(config, bsde)
lr_schedule = tf.keras.optimizers.schedules.PiecewiseConstantDecay(
self.net_config.lr_boundaries, self.net_config.lr_values)
self.optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule, epsilon=1e-8)
def train(self):
start_time = time.time()
training_history = []
valid_data = self.bsde.sample(self.net_config.valid_size)
# begin sgd iteration
for step in range(self.net_config.num_iterations+1):
if step % self.net_config.logging_frequency == 0:
loss = self.loss_fn(valid_data, training=False).numpy()
y_init = self.model.y_init.numpy()[0]
elapsed_time = time.time() - start_time
training_history.append([step, loss, y_init, elapsed_time])
if self.net_config.verbose:
logging.info("step: %5u, loss: %.4e, Y0: %.4e, elapsed time: %3u" % (
step, loss, y_init, elapsed_time))
self.train_step(self.bsde.sample(self.net_config.batch_size))
return np.array(training_history)
def loss_fn(self, inputs, training):
dw, x = inputs
y_terminal = self.model(inputs, training=training)
delta = y_terminal - self.bsde.g_tf(self.bsde.total_time, x[:, :, -1])
# use linear approximation outside the clipped range
loss = tf.reduce_mean(tf.where(tf.abs(delta) < DELTA_CLIP, tf.square(delta),
2 * DELTA_CLIP * tf.abs(delta) - DELTA_CLIP ** 2))
return loss
def grad(self, inputs, training):
with tf.GradientTape(persistent=True) as tape:
loss = self.loss_fn(inputs, training)
grad = tape.gradient(loss, self.model.trainable_variables)
del tape
return grad
@tf.function
def train_step(self, train_data):
with tf.GradientTape() as tape:
loss = self.loss_fn(train_data, training=True)
gradients = tape.gradient(loss, self.model.trainable_variables)
self.optimizer.apply_gradients(zip(gradients, self.model.trainable_variables))
class NonsharedModel(tf.keras.Model):
def __init__(self, config, bsde):
super().__init__()
self.eqn_config = config.eqn_config
self.net_config = config.net_config
self.bsde = bsde
y_init_range = self.net_config.y_init_range
self.y_init = self.add_weight(
name='y_init',
shape=[1],
initializer=tf.random_uniform_initializer(
y_init_range[0],
y_init_range[1]
)
)
self.z_init = self.add_weight(
name='z_init',
shape=[1, self.eqn_config.dim],
initializer=tf.random_uniform_initializer(-0.1, 0.1)
)
self.subnet = [FeedForwardSubNet(config) for _ in range(self.bsde.num_time_interval-1)]
def call(self, inputs, training=None):
dw, x = inputs
time_stamp = np.arange(0, self.eqn_config.num_time_interval) * self.bsde.delta_t
all_one_vec = tf.ones(shape=tf.stack([tf.shape(dw)[0], 1]), dtype=self.net_config.dtype)
y = all_one_vec * self.y_init
z = tf.matmul(all_one_vec, self.z_init)
for t in range(0, self.bsde.num_time_interval-1):
y = y - self.bsde.delta_t * (
self.bsde.f_tf(time_stamp[t], x[:, :, t], y, z)
) + tf.reduce_sum(z * dw[:, :, t], 1, keepdims=True)
z = self.subnet[t](x[:, :, t + 1], training=training) / self.bsde.dim
# terminal time
y = y - self.bsde.delta_t * self.bsde.f_tf(time_stamp[-1], x[:, :, -2], y, z) + \
tf.reduce_sum(z * dw[:, :, -1], 1, keepdims=True)
return y
class FeedForwardSubNet(tf.keras.Model):
def __init__(self, config):
super(FeedForwardSubNet, self).__init__()
dim = config.eqn_config.dim
num_hiddens = config.net_config.num_hiddens
self.bn_layers = [
tf.keras.layers.BatchNormalization(
momentum=0.99,
epsilon=1e-6,
beta_initializer=tf.random_normal_initializer(0.0, stddev=0.1),
gamma_initializer=tf.random_uniform_initializer(0.1, 0.5)
)
for _ in range(len(num_hiddens) + 2)]
self.dense_layers = [tf.keras.layers.Dense(num_hiddens[i],
use_bias=False,
activation=None)
for i in range(len(num_hiddens))]
# final output should be gradient of size dim
self.dense_layers.append(tf.keras.layers.Dense(dim, activation=None))
def call(self, x, training=None):
"""structure: bn -> (dense -> bn -> relu) * len(num_hiddens) -> dense -> bn"""
x = self.bn_layers[0](x, training=training)
for i in range(len(self.dense_layers) - 1):
x = self.dense_layers[i](x)
x = self.bn_layers[i+1](x, training=training)
x = tf.nn.relu(x)
x = self.dense_layers[-1](x)
x = self.bn_layers[-1](x, training=training)
return x