-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathall_together_loss_time.py
519 lines (320 loc) · 13.6 KB
/
all_together_loss_time.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
from itertools import product
import deepxde as dde
import math
import pathlib
import os
import time
#import tf.keras.callbacks.EarlyStopping
import tensorflow as tf
from toolz import partition
OUTPUT_DIRECTORY = pathlib.Path.cwd() / "results" / "wave_equation"
if not OUTPUT_DIRECTORY.exists():
os.makedirs(OUTPUT_DIRECTORY, exist_ok=True)
def pde(x, y): # wave equation
dy_tt = dde.grad.hessian(y, x, i=1, j=1)
dy_xx = dde.grad.hessian(y, x, i=0, j=0)
return dy_tt - 4*dy_xx
def initial_pos(x): # initial position
return np.sin(np.pi * x[:, 0:1])
def initial_velo(x): # initial velocity
return 0.0
def boundary_left(x, on_boundary): # boundary x=0
is_on_boundary_left = on_boundary and np.isclose(x[0], 0)
return is_on_boundary_left
def boundary_right(x, on_boundary): # boundary x=1
is_on_boundary_right = on_boundary and np.isclose(x[0], 1)
return is_on_boundary_right
def boundary_bottom(x, on_boundary): # boundary t=0
is_on_boundary_bottom = (
on_boundary
and np.isclose(x[1], 0)
and not np.isclose(x[0], 0)
and not np.isclose(x[0], 1)
)
return is_on_boundary_bottom
def boundary_upper(x, on_boundary): # boundary t=2
is_on_boundary_upper = (
on_boundary
and np.isclose(x[1], 2)
and not np.isclose(x[0], 0)
and not np.isclose(x[0], 1)
)
return is_on_boundary_upper
def func(x):
return np.sin(np.pi * x[:, 0:1]) * np.cos(2*np.pi*x[:, 1:])
geom = dde.geometry.Rectangle(xmin=[0, 0], xmax=[1, 2])
bc1 = dde.DirichletBC(geom, lambda x: 0, boundary_left) #correct
bc2 = dde.DirichletBC(geom, lambda x: 0, boundary_right) #correct
bc3 = dde.DirichletBC(geom, initial_pos, boundary_bottom) #correct
bc4 = dde.NeumannBC(geom, initial_velo, boundary_bottom) #correct
data = dde.data.TimePDE(
geom,
pde,
[bc1, bc2, bc3, bc4],
num_domain=200,
num_boundary=100, solution=func, num_test=250
)
print("The training set is {}".format(data.train_x_all.T))
net = dde.maps.FNN([2] + [50] * 4 + [1], "tanh", "Glorot uniform") #the input layer has size 2, there are 4 hidden layers of size 50 and one output layer of size 1
#Activation function is tanh; the weights are initially chosen to be uniformly distributed according to Glorat distribution
#callback=dde.callbacks.EarlyStopping(min_delta=0.001, patience=1000, monitor='loss_test')
model = dde.Model(data, net)
model.compile("adam", lr=0.001)
class TimeHistory(dde.callbacks.Callback):
def on_train_begin(self):
self.times = []
def on_epoch_begin(self):
self.epoch_time_start = time.process_time()
def on_epoch_end(self):
self.times.append(time.process_time() - self.epoch_time_start)
time_callback = TimeHistory()
history=model.train(callbacks=[time_callback], epochs=10000)
def fun(x):
x=np.array(x)
return np.sum(x)
chunks_1 = list(partition(1000, time_callback.times))
time_epochs=list(map(fun, chunks_1))
model.compile("L-BFGS-B")
losshistory, train_state = model.train()
dde.saveplot(
losshistory, train_state, issave=True, isplot=True, output_dir=OUTPUT_DIRECTORY
)
print("Loss history gives {}".format(losshistory.loss_train))
print("Loss history gives {}".format(losshistory.metrics_test))
a=list(map(np.sum, losshistory.loss_test))[0:10]
print("Total loss is {}".format(a))
print("This are the times {}".format(time_epochs))
print("This are the test losses {}".format(losshistory.loss_test[0:10]))
time_taken=[]
b=0
for i in range(len(time_epochs)):
b=b+time_epochs[i]
time_taken.append(b)
#plt.plot(time_taken, a, label="[50] x 4")
#plt.xlabel("Computational time (s)")
#plt.ylabel("Test error")
#plt.title("Test error vs. Computational time")
#net_1 = dde.maps.FNN([2] + [100] * 2 + [1], "tanh", "Glorot uniform") #the input layer has size 2, there are 4 hidden layers of size 50 and one output layer of size 1
#net_2 = dde.maps.FNN([2] + [100] * 4 + [1], "tanh", "Glorot uniform") #the input layer has size 2, there are 4 hidden layers of size 50 and one output layer of size 1
#net_3 = dde.maps.FNN([2] + [20] * 10 + [1], "tanh", "Glorot uniform") #the input layer has size 2, there are 4 hidden layers of size 50 and one output layer of size 1
#4444444--------------------------------------------------------
net_4 = dde.maps.FNN([2] + [5] * 2 + [1], "tanh", "Glorot uniform")
net_5 = dde.maps.FNN([2] + [5] * 6 + [1], "tanh", "Glorot uniform")
net_6 = dde.maps.FNN([2] + [5] * 10 + [1], "tanh", "Glorot uniform")
net_7 = dde.maps.FNN([2] + [8] * 10 + [1], "tanh", "Glorot uniform")
net_8 = dde.maps.FNN([2] + [10] * 10 + [1], "tanh", "Glorot uniform")
net_9 = dde.maps.FNN([2] + [10] * 15 + [1], "tanh", "Glorot uniform")
#net_10 = dde.maps.FNN([2] + [20] * 10 + [1], "tanh", "Glorot uniform")
model_1 = dde.Model(data, net_4)
model_1.compile("adam", lr=0.001)
class TimeHistory_1(dde.callbacks.Callback):
def on_train_begin(self):
self.times = []
def on_epoch_begin(self):
self.epoch_time_start = time.process_time()
def on_epoch_end(self):
self.times.append(time.process_time() - self.epoch_time_start)
time_callback_1 = TimeHistory_1()
history=model_1.train(callbacks=[time_callback_1], epochs=10000)
def fun(x):
x=np.array(x)
return np.sum(x)
chunks_11 = list(partition(1000, time_callback_1.times))
time_epochs_1=list(map(fun, chunks_11))
model_1.compile("L-BFGS-B")
losshistory_1, train_state_1 = model_1.train()
dde.saveplot(
losshistory_1, train_state_1, issave=True, isplot=True, output_dir=OUTPUT_DIRECTORY
)
#print("Loss history gives {}".format(losshistory.loss_train))
#print("Loss history gives {}".format(losshistory.metrics_test))
a1=list(map(np.sum, losshistory_1.loss_test))[0:10]
#print("Total loss is {}".format(a))
#print("This are the times {}".format(time_epochs))
#print("This are the test losses {}".format(losshistory.loss_test[0:10]))
time_taken1=[]
b=0
for i in range(len(time_epochs_1)):
b=b+time_epochs_1[i]
time_taken1.append(b)
#plt.plot(time_taken1, a1, label="[100] x 2")
##555555555555------------------------------------------------------------------
model_2 = dde.Model(data, net_5)
model_2.compile("adam", lr=0.001)
class TimeHistory_2(dde.callbacks.Callback):
def on_train_begin(self):
self.times = []
def on_epoch_begin(self):
self.epoch_time_start = time.process_time()
def on_epoch_end(self):
self.times.append(time.process_time() - self.epoch_time_start)
time_callback_2 = TimeHistory_2()
history=model_2.train(callbacks=[time_callback_2], epochs=10000)
def fun(x):
x=np.array(x)
return np.sum(x)
chunks_12 = list(partition(1000, time_callback_2.times))
time_epochs_2=list(map(fun, chunks_12))
model_2.compile("L-BFGS-B")
losshistory_2, train_state_2 = model_2.train()
dde.saveplot(
losshistory_2, train_state_2, issave=True, isplot=True, output_dir=OUTPUT_DIRECTORY
)
#print("Loss history gives {}".format(losshistory.loss_train))
#print("Loss history gives {}".format(losshistory.metrics_test))
a2=list(map(np.sum, losshistory_2.loss_test))[0:10]
#print("Total loss is {}".format(a))
#print("This are the times {}".format(time_epochs))
#print("This are the test losses {}".format(losshistory.loss_test[0:10]))
time_taken2=[]
b=0
for i in range(len(time_epochs_2)):
b=b+time_epochs_2[i]
time_taken2.append(b)
#plt.plot(time_taken2, a2, label="[100] x 4")
#666666666666666666--------------------------------------------------------------
model_3 = dde.Model(data, net_6)
model_3.compile("adam", lr=0.001)
class TimeHistory_3(dde.callbacks.Callback):
def on_train_begin(self):
self.times = []
def on_epoch_begin(self):
self.epoch_time_start = time.process_time()
def on_epoch_end(self):
self.times.append(time.process_time() - self.epoch_time_start)
time_callback_3 = TimeHistory_3()
history=model_3.train(callbacks=[time_callback_3], epochs=10000)
def fun(x):
x=np.array(x)
return np.sum(x)
chunks_13 = list(partition(1000, time_callback_3.times))
time_epochs_3=list(map(fun, chunks_13))
model_3.compile("L-BFGS-B")
losshistory_3, train_state_3 = model_3.train()
dde.saveplot(
losshistory_3, train_state_3, issave=True, isplot=True, output_dir=OUTPUT_DIRECTORY
)
#print("Loss history gives {}".format(losshistory.loss_train))
#print("Loss history gives {}".format(losshistory.metrics_test))
a3=list(map(np.sum, losshistory_3.loss_test))[0:10]
#print("Total loss is {}".format(a))
#print("This are the times {}".format(time_epochs))
#print("This are the test losses {}".format(losshistory.loss_test[0:10]))
time_taken3=[]
b=0
for i in range(len(time_epochs_3)):
b=b+time_epochs_3[i]
time_taken3.append(b)
#777777777777777777-----------------------------------------
model_4 = dde.Model(data, net_7)
model_4.compile("adam", lr=0.001)
class TimeHistory_4(dde.callbacks.Callback):
def on_train_begin(self):
self.times = []
def on_epoch_begin(self):
self.epoch_time_start = time.process_time()
def on_epoch_end(self):
self.times.append(time.process_time() - self.epoch_time_start)
time_callback_4 = TimeHistory_4()
history=model_4.train(callbacks=[time_callback_4], epochs=10000)
def fun(x):
x=np.array(x)
return np.sum(x)
chunks_14 = list(partition(1000, time_callback_4.times))
time_epochs_4=list(map(fun, chunks_14))
model_4.compile("L-BFGS-B")
losshistory_4, train_state_4 = model_4.train()
dde.saveplot(
losshistory_4, train_state_4, issave=True, isplot=True, output_dir=OUTPUT_DIRECTORY
)
#print("Loss history gives {}".format(losshistory.loss_train))
#print("Loss history gives {}".format(losshistory.metrics_test))
a4=list(map(np.sum, losshistory_4.loss_test))[0:10]
#print("Total loss is {}".format(a))
#print("This are the times {}".format(time_epochs))
#print("This are the test losses {}".format(losshistory.loss_test[0:10]))
time_taken4=[]
b=0
for i in range(len(time_epochs_4)):
b=b+time_epochs_4[i]
time_taken4.append(b)
#888888888888888888888888------------------------------------------------------
model_5 = dde.Model(data, net_8)
model_5.compile("adam", lr=0.001)
class TimeHistory_5(dde.callbacks.Callback):
def on_train_begin(self):
self.times = []
def on_epoch_begin(self):
self.epoch_time_start = time.process_time()
def on_epoch_end(self):
self.times.append(time.process_time() - self.epoch_time_start)
time_callback_5 = TimeHistory_5()
history=model_5.train(callbacks=[time_callback_5], epochs=10000)
def fun(x):
x=np.array(x)
return np.sum(x)
chunks_15 = list(partition(1000, time_callback_5.times))
time_epochs_5=list(map(fun, chunks_15))
model_5.compile("L-BFGS-B")
losshistory_5, train_state_5 = model_5.train()
dde.saveplot(
losshistory_5, train_state_5, issave=True, isplot=True, output_dir=OUTPUT_DIRECTORY
)
#print("Loss history gives {}".format(losshistory.loss_train))
#print("Loss history gives {}".format(losshistory.metrics_test))
a5=list(map(np.sum, losshistory_5.loss_test))[0:10]
#print("Total loss is {}".format(a))
#print("This are the times {}".format(time_epochs))
#print("This are the test losses {}".format(losshistory.loss_test[0:10]))
time_taken5=[]
b=0
for i in range(len(time_epochs_5)):
b=b+time_epochs_5[i]
time_taken5.append(b)
#9999999999999999999999------------------------------------------------------------------
model_6 = dde.Model(data, net_9)
model_6.compile("adam", lr=0.001)
class TimeHistory_6(dde.callbacks.Callback):
def on_train_begin(self):
self.times = []
def on_epoch_begin(self):
self.epoch_time_start = time.process_time()
def on_epoch_end(self):
self.times.append(time.process_time() - self.epoch_time_start)
time_callback_6 = TimeHistory_6()
history=model_6.train(callbacks=[time_callback_6], epochs=10000)
def fun(x):
x=np.array(x)
return np.sum(x)
chunks_16 = list(partition(1000, time_callback_6.times))
time_epochs_6=list(map(fun, chunks_16))
model_6.compile("L-BFGS-B")
losshistory_6, train_state_6 = model_6.train()
dde.saveplot(
losshistory_6, train_state_6, issave=True, isplot=True, output_dir=OUTPUT_DIRECTORY
)
#print("Loss history gives {}".format(losshistory.loss_train))
#print("Loss history gives {}".format(losshistory.metrics_test))
a6=list(map(np.sum, losshistory_6.loss_test))[0:10]
#print("Total loss is {}".format(a))
#print("This are the times {}".format(time_epochs))
#print("This are the test losses {}".format(losshistory.loss_test[0:10]))
time_taken6=[]
b=0
for i in range(len(time_epochs_6)):
b=b+time_epochs_6[i]
time_taken6.append(b)
plt.plot(time_taken3, a3, label="50 nodes")
plt.plot(time_taken4, a4, label="80 nodes")
plt.plot(time_taken5, a5, label="100 nodes")
plt.plot(time_taken6, a6, label="150 nodes")
plt.plot(time_taken, a, label="200 nodes")
plt.plot(time_taken2, a2, label="30 nodes")
plt.plot(time_taken1, a1, label="10 nodes")
plt.xlabel("Computational time (s)")
plt.ylabel("Test error")
plt.title("Test error vs. Computational time")
plt.legend()