forked from maziarraissi/FBSNNs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFBSNNs.py
executable file
·226 lines (168 loc) · 7.41 KB
/
FBSNNs.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
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
"""
@author: Maziar Raissi
"""
import numpy as np
#import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import time
from abc import ABC, abstractmethod
class FBSNN(ABC): # Forward-Backward Stochastic Neural Network
def __init__(self, Xi, T,
M, N, D,
layers):
self.Xi = Xi # initial point
self.T = T # terminal time
self.M = M # number of trajectories
self.N = N # number of time snapshots
self.D = D # number of dimensions
# layers
self.layers = layers # (D+1) --> 1
# initialize NN
self.weights, self.biases = self.initialize_NN(layers)
# tf session
self.sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(allow_soft_placement=True,
log_device_placement=True))
# tf placeholders and graph (training)
self.learning_rate = tf.placeholder(tf.float32, shape=[])
self.t_tf = tf.placeholder(tf.float32, shape=[M, self.N+1, 1]) # M x (N+1) x 1
self.W_tf = tf.placeholder(tf.float32, shape=[M, self.N+1, self.D]) # M x (N+1) x D
self.Xi_tf = tf.placeholder(tf.float32, shape=[1, D]) # 1 x D
self.loss, self.X_pred, self.Y_pred, self.Z_pred, self.Y0_pred = self.loss_function(self.t_tf, self.W_tf, self.Xi_tf)
# optimizers
self.optimizer = tf.train.AdamOptimizer(learning_rate = self.learning_rate)
self.train_op = self.optimizer.minimize(self.loss)
# initialize session and variables
init = tf.global_variables_initializer()
self.sess.run(init)
def initialize_NN(self, layers):
weights = []
biases = []
num_layers = len(layers)
for l in range(0,num_layers-1):
W = self.xavier_init(size=[layers[l], layers[l+1]])
b = tf.Variable(tf.zeros([1,layers[l+1]], dtype=tf.float32), dtype=tf.float32)
weights.append(W)
biases.append(b)
return weights, biases
def xavier_init(self, size):
in_dim = size[0]
out_dim = size[1]
xavier_stddev = np.sqrt(2/(in_dim + out_dim))
return tf.Variable(tf.random.truncated_normal([in_dim, out_dim],
stddev=xavier_stddev), dtype=tf.float32)
def neural_net(self, X, weights, biases):
num_layers = len(weights) + 1
H = X
for l in range(0,num_layers-2):
W = weights[l]
b = biases[l]
H = tf.sin(tf.add(tf.matmul(H, W), b))
W = weights[-1]
b = biases[-1]
Y = tf.add(tf.matmul(H, W), b)
return Y
def net_u(self, t, X): # M x 1, M x D
u = self.neural_net(tf.concat([t,X], 1), self.weights, self.biases) # M x 1
Du = tf.gradients(u, X)[0] # M x D
return u, Du
def Dg_tf(self, X): # M x D
return tf.gradients(self.g_tf(X), X)[0] # M x D
def loss_function(self, t, W, Xi): # M x (N+1) x 1, M x (N+1) x D, 1 x D
loss = 0
X_list = []
Y_list = []
Z_list = []
t0 = t[:,0,:]
W0 = W[:,0,:]
X0 = tf.tile(Xi,[self.M,1]) # M x D
Y0, Z0 = self.net_u(t0,X0) # M x 1, M x D
X_list.append(X0)
Y_list.append(Y0)
Z_list.append(Z0)
for n in range(0,self.N):
t1 = t[:,n+1,:]
W1 = W[:,n+1,:]
X1 = X0 + self.mu_tf(t0,X0,Y0,Z0)*(t1-t0) + tf.squeeze(tf.matmul(self.sigma_tf(t0,X0,Y0),tf.expand_dims(W1-W0,-1)), axis=[-1])
Y1_tilde = Y0 + self.phi_tf(t0,X0,Y0,Z0)*(t1-t0) + tf.reduce_sum(Z0*tf.squeeze(tf.matmul(self.sigma_tf(t0,X0,Y0),tf.expand_dims(W1-W0,-1))), axis=1, keepdims = True)
Y1, Z1 = self.net_u(t1,X1)
loss += tf.reduce_sum(tf.square(Y1 - Y1_tilde))
t0 = t1
W0 = W1
X0 = X1
Y0 = Y1
Z0 = Z1
X_list.append(X0)
Y_list.append(Y0)
Z_list.append(Z0)
loss += tf.reduce_sum(tf.square(Y1 - self.g_tf(X1)))
loss += tf.reduce_sum(tf.square(Z1 - self.Dg_tf(X1)))
X = tf.stack(X_list,axis=1)
Y = tf.stack(Y_list,axis=1)
Z = tf.stack(Z_list,axis=1)
return loss, X, Y, Z, Y[0,0,0]
def fetch_minibatch(self):
T = self.T
M = self.M
N = self.N
D = self.D
Dt = np.zeros((M,N+1,1)) # M x (N+1) x 1
DW = np.zeros((M,N+1,D)) # M x (N+1) x D
dt = T/N
Dt[:,1:,:] = dt
DW[:,1:,:] = np.sqrt(dt)*np.random.normal(size=(M,N,D))
t = np.cumsum(Dt,axis=1) # M x (N+1) x 1
W = np.cumsum(DW,axis=1) # M x (N+1) x D
return t, W
def fetch_minibatch_no_noise(self):
T = self.T
M = self.M
N = self.N
D = self.D
Dt = np.zeros((M,N+1,1)) # M x (N+1) x 1
DW = np.zeros((M,N+1,D)) # M x (N+1) x D
dt = T/N
Dt[:,1:,:] = dt
DW[:,1:,:] = 0
t = np.cumsum(Dt,axis=1) # M x (N+1) x 1
W = np.cumsum(DW,axis=1) # M x (N+1) x D
return t, W
def train(self, N_Iter, learning_rate):
start_time = time.time()
for it in range(N_Iter):
t_batch, W_batch = self.fetch_minibatch() # M x (N+1) x 1, M x (N+1) x D
tf_dict = {self.Xi_tf: self.Xi, self.t_tf: t_batch, self.W_tf: W_batch, self.learning_rate: learning_rate}
self.sess.run(self.train_op, tf_dict)
# Print
if it % 100 == 0:
elapsed = time.time() - start_time
loss_value, Y0_value, learning_rate_value = self.sess.run([self.loss, self.Y0_pred, self.learning_rate], tf_dict)
print('It: %d, Loss: %.3e, Y0: %.3f, Time: %.2f, Learning Rate: %.3e' %
(it, loss_value, Y0_value, elapsed, learning_rate_value))
start_time = time.time()
def predict(self, Xi_star, t_star, W_star):
tf_dict = {self.Xi_tf: Xi_star, self.t_tf: t_star, self.W_tf: W_star}
X_star = self.sess.run(self.X_pred, tf_dict)
Y_star = self.sess.run(self.Y_pred, tf_dict)
Z_star = self.sess.run(self.Z_pred, tf_dict)
return X_star, Y_star, Z_star
###########################################################################
############################# Change Here! ################################
###########################################################################
@abstractmethod
def phi_tf(self, t, X, Y, Z): # M x 1, M x D, M x 1, M x D
pass # M x1
@abstractmethod
def g_tf(self, X): # M x D
pass # M x 1
@abstractmethod
def mu_tf(self, t, X, Y, Z): # M x 1, M x D, M x 1, M x D
M = self.M
D = self.D
return np.zeros([M,D]) # M x D
@abstractmethod
def sigma_tf(self, t, X, Y): # M x 1, M x D, M x 1
M = self.M
D = self.D
return tf.matrix_diag(tf.ones([M,D])) # M x D x D
###########################################################################