-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathDeepEddy.py
1050 lines (737 loc) · 37.2 KB
/
DeepEddy.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Tom Bolton
Atmospheric, Oceanic, and Planetary Physics, University of Oxford
23/09/2018
This script contains various functions for the preparation,
training, and testing of the convolutional neural networks (CNN)
used in the paper "Applications of Deep Learning to Ocean Data
Inference and Sub-Grid Parameterisation".
The functions are split into the following made catergories:
- Data preparation.
- Training and predict.
- Plotting/Misc.
"""
import time
import pickle
import numpy as np
import scipy.io as sio
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
from scipy.interpolate import griddata
from itertools import combinations
import random
from sklearn.neighbors import KNeighborsRegressor
from keras import backend as K
from keras.models import load_model
from keras.models import Model
from keras.layers import Input, Convolution2D, Dense, Flatten, MaxPooling2D, Lambda
from keras.optimizers import Adam
########################################################################
#
# Data Preparation Functions
#
########################################################################
def calcEddyForcing( psiEddy, psiBar, l ) :
"""
Given the filtered-streamfunction 'psiBar' and the sub-filter
streamfunction psiEddy = psi - psiBar, calculate the components
of the sub-filter eddy momentum forcing Sx and Sy
(Sx,Sy) = (U.grad)U - filter( (u.grad)u ),
where U is the velocity from the filtered-streamfunction, and
u is the full velocity (filtered + sub-filter). The calculation
requires more spatial-filtering, which is why the length-scale
of the filter 'l' (in km) is required as an input variable.
"""
# spatial-resolution of QG model (7.5km)
dx = 7.5e3
# streamfunction for calculating u and v
psi = psiBar + psiEddy
# calculate gradients
[ psi_t, psi_y, psi_x] = np.gradient( psi, dx )
[ psiBar_t, psiBar_y, psiBar_x] = np.gradient( psiBar, dx )
u, v = -psi_y, psi_x
U, V = -psiBar_y, psiBar_x
# Calcluate filtered-advection term
[ U_t, U_y, U_x ] = np.gradient( U, dx ); del U_t
[ V_t, V_y, V_x ] = np.gradient( V, dx ); del V_t
# ( Ud/dx + Vd/dy )U and ( Ud/dx + Vd/dy )V
adv1_x = U * U_x + V * U_y
adv1_y = U * V_x + V * V_y
del U_x, U_y, V_x, V_y
# Calculate sub-filter advection term
[u_t, u_y, u_x] = np.gradient(u, dx); del u_t
[v_t, v_y, v_x] = np.gradient(v, dx); del v_t
# ( ud/dx + vd/dy )u + ( ud/dx + vd/dy )v
adv2_x = u * u_x + v * u_y
adv2_y = u * v_x + v * v_y
del u_x, u_y, v_x, v_y
for t in range( adv2_x.shape[0] ) :
adv2_x[t,:,:] = gaussian_filter( adv2_x[t,:,:], (l*1e3)/dx )
adv2_y[t,:,:] = gaussian_filter( adv2_y[t,:,:], (l*1e3)/dx )
# Calculate the eddy momentum forcing components
Sx = adv1_x - adv2_x
Sy = adv1_y - adv2_y
return Sx, Sy
def loadAndNormDataS( l, axis, region, MOMENTUM_B=False ):
"""
This function loads the filtered-streamfunction and
streamfunction anomaly from a .mat file, and calculates
the corresponding sub-filter eddy momentum forcing. Both the eddy
momentum forcing and the filtered-streamfunctions are normalised
to zero mean and unit variance, and split into training and test data.
l = spatial-scale (in km) of low-pass Gaussian filter
axis = 'x' or 'y', specifying if Sx or Sy is calculated
region = 1, 2, or 3, the training regions being considered
For example, for l=30km, axis='y', and region=2, then this function
will form training and test datasets for the filtered-streamfunction
(input) and the meridional eddy momentum forcing Sy (output), using
data only from region=2, which corresponds to the eastern boundary.
"""
# construct file and variable names
fileName = 'data/Training/psiTrain' + str(region) + '_' + str(l) + 'km.mat'
varName1 = 'psi' + str(region) + 'Anom' # sub-filter streamfunction
varName2 = 'psi' + str(region) + '_' + str(l) + 'km' # filtered streamfunction
# load data
data = sio.loadmat( fileName )
# extract the filtered \bar(psi) and sub-filter psi' for this region
psiAnom = data[ varName1 ]
psiFilt = data[ varName2 ]
# move time dimension to the left
psiAnom = np.moveaxis( psiAnom, 2, 0 )
psiFilt = np.moveaxis( psiFilt, 2, 0 )
# calculate eddy (sub-filter) momentum forcing
Sx, Sy = calcEddyForcing( psiAnom, psiFilt, l )
# choose x or y axis depending on 'axis' variable
# Sx = zonal eddy momentum forcing
# Sy = meridional eddy momentum forcing
S = Sx if axis == 'x' else Sy
# standarize the variables to zero mean and unit variance,
# this is important in particular for neural networks
mu1, sigma1 = np.mean( psiFilt ), np.std( psiFilt )
mu2, sigma2 = np.mean( S ), np.std( S )
psiFilt = (psiFilt - mu1) / sigma1
S = (S - mu2) / sigma2
scalings = [mu1, sigma1, mu2, sigma2]
# split into training and test data (9 years for training,
# 1 year for testing)
xTrain, xTest = psiFilt[:3300, :, :], psiFilt[3300:, :, :]
yTrain, yTest = S[:3300, :, :], S[3300:, :, :]
# split the original 160x160 region into 16 sub-regions
# of size 40x40. Then combine into a single data set (which
# will have sixteen times as many training samples).
xTrain = np.reshape(np.array(np.split(np.array(np.split(xTrain, 4, axis=2)), 4, axis=2)), (4 * 4 * 3300, 40, 40))
yTrain = np.reshape(np.array(np.split(np.array(np.split(yTrain, 4, axis=2)), 4, axis=2)), (4 * 4 * 3300, 40, 40))
xTest = np.reshape(np.array(np.split(np.array(np.split(xTest, 4, axis=2)), 4, axis=2)), (4 * 4 * 350, 40, 40))
yTest = np.reshape(np.array(np.split(np.array(np.split(yTest, 4, axis=2)), 4, axis=2)), (4 * 4 * 350, 40, 40))
if MOMENTUM_B :
# remove the spatial-mean of S, at each time-slice, in order to make
# the net input of momentum zero in the training data (approach B to
# conserving momentum by pre-processing training data).
yTrain = yTrain - np.mean( yTrain, axis=(1,2), keepdims=True )
yTest = yTest - np.mean( yTest, axis=(1,2), keepdims=True )
# add singleton dimension for input variables (this is for Keras)
xTrain = np.reshape(xTrain, (-1, 40, 40, 1))
xTest = np.reshape(xTest, (-1, 40, 40, 1))
# reshape outputs from 2D (40x40) to 1D vector 1600
yTrain = np.reshape(yTrain, (-1, 40 * 40))
yTest = np.reshape(yTest, (-1, 40 * 40))
return xTrain, yTrain, xTest, yTest, scalings
def makeSparseDataset( l, axis, region, N ):
"""
This function loads the filtered-streamfunction and
streamfunction anomaly from a .mat file, and calculates
the corresponding sub-filter eddy momentum forcing. Both the eddy
momentum forcing and the filtered-streamfunctions are normalised
to zero mean and unit variance, and split into training and test data.
However, instead using all points in space, this function randomly
samples N unique (x,y) pairs and interpolates the corresponding values
of Sx (or Sy) to the 40x40 input size.
"""
# construct file and variable names
fileName = 'data/Training/psiTrain' + str(region) + '_' + str(l) + 'km.mat'
varName1 = 'psi' + str(region) + 'Anom' # sub-filter streamfunction
varName2 = 'psi' + str(region) + '_' + str(l) + 'km' # filtered streamfunction
# load data
data = sio.loadmat( fileName )
# extract the filtered \bar(psi) and sub-filter psi' for this region
psiAnom = data[ varName1 ]
psiFilt = data[ varName2 ]
# move time dimension to the left
psiAnom = np.moveaxis( psiAnom, 2, 0 )
psiFilt = np.moveaxis( psiFilt, 2, 0 )
# calculate eddy (sub-filter) momentum forcing
Sx, Sy = calcEddyForcing( psiAnom, psiFilt, l )
# choose x or y axis depending on 'axis' variable
# Sx = zonal eddy momentum forcing
# Sy = meridional eddy momentum forcing
S = Sx if axis == 'x' else Sy
# standarize the variables to zero mean and unit variance,
# this is important in particular for neural networks
mu1, sigma1 = np.mean( psiFilt ), np.std( psiFilt )
mu2, sigma2 = np.mean( S ), np.std( S )
psiFilt = (psiFilt - mu1) / sigma1
S = (S - mu2) / sigma2
scalings = [mu1, sigma1, mu2, sigma2]
# split into training and test data (9 years for training,
# 1 year for testing)
xTrain, xTest = psiFilt[:3300, :, :], psiFilt[3300:, :, :]
yTrain, yTest = S[:3300, :, :], S[3300:, :, :]
# split the original 160x160 region into 16 sub-regions
# of size 40x40. Then combine into a single data set (which
# will have sixteen times as many training samples).
xTrain = np.reshape(np.array(np.split(np.array(np.split(xTrain, 4, axis=2)), 4, axis=2)), (4 * 4 * 3300, 40, 40))
yTrain = np.reshape(np.array(np.split(np.array(np.split(yTrain, 4, axis=2)), 4, axis=2)), (4 * 4 * 3300, 40, 40))
xTest = np.reshape(np.array(np.split(np.array(np.split(xTest, 4, axis=2)), 4, axis=2)), (4 * 4 * 350, 40, 40))
yTest = np.reshape(np.array(np.split(np.array(np.split(yTest, 4, axis=2)), 4, axis=2)), (4 * 4 * 350, 40, 40))
# loop through each time-slice in the training data, sub-sample
# N points in space, and then reconstruct the input and output
# variables (psi and S) through a simple 2d interpolation
print "Sub-sampling training data to ", N, " points out of 1600."
for t in range(4*4*3300):
xy, f_psi, f_S = [], [], []
if t % 1000 == 0 : print t
coords = generateUniqueCoords( N )
for pair in coords :
xy.append( np.array( pair ) ) # x-y coordinates
f_psi.append( np.squeeze( xTrain[ t, pair[1], pair[0] ] ) )
f_S.append( np.squeeze( yTrain[ t, pair[1], pair[0] ] ) )
# nearest neighbour interp
grid_x, grid_y = np.mgrid[0:40:1, 0:40:1]
x_pred = grid_x.flatten()
y_pred = grid_y.flatten()
data = np.array( [ x_pred, y_pred ] ).T
KNR_psi = KNeighborsRegressor( n_neighbors=1 )
KNR_S = KNeighborsRegressor( n_neighbors=1 )
KNR_psi.fit( coords, f_psi )
KNR_S.fit( coords, f_S )
x_near = np.transpose( np.reshape( KNR_psi.predict( data ), (40,40) ) )
y_near = np.transpose( np.reshape( KNR_psi.predict( data ), (40,40) ) )
# cubic interp
x_cubic = np.transpose( griddata( xy, f_psi, (grid_x, grid_y), method='cubic') )
y_cubic = np.transpose( griddata( xy, f_S, (grid_x, grid_y), method='cubic') )
# sanity check
#fig, axArr = plt.subplots(2,2)
#axArr[0,0].imshow( np.squeeze( xTrain[t,:,:] ) )
#axArr[0,0].set_title( 'before' )
#axArr[0,1].imshow( x_near )
#axArr[0,1].set_title( 'nearest' )
#axArr[1,0].imshow( x_cubic )
#axArr[1,0].set_title( 'cubic' )
# remove NaNs using nearest neigbour
x_cubic[ np.isnan( x_cubic ) ] = x_near[ np.isnan( x_cubic ) ]
y_cubic[ np.isnan( y_cubic ) ] = y_near[ np.isnan( y_cubic ) ]
xTrain[t,:,:] = x_cubic
yTrain[t,:,:] = y_cubic
#axArr[1,1].imshow( x_cubic )
#axArr[1,1].set_title( 'after' )
#plt.show()
# add singleton dimension for input variables (this is for Keras)
xTrain = np.reshape(xTrain, (-1, 40, 40, 1))
xTest = np.reshape(xTest, (-1, 40, 40, 1))
# reshape outputs from 2D (40x40) to 1D vector 1600
yTrain = np.reshape(yTrain, (-1, 40 * 40))
yTest = np.reshape(yTest, (-1, 40 * 40))
return xTrain, yTrain, xTest, yTest, scalings
def loadAndNormDataMidPsi() :
"""
This function loads upper- and middle- layer streamfunctions.
The upper-layer streamfunction is spatially filtered as before
with a Gaussian low-pass filter of width 30km.
Data from region 1 is used, and the upper-layer streamfunction
is the input variable, while the middle-layer (sub-surface)
streamfunction is the output variable to be predicted by the
convolutional neural network.
"""
fileName = '../data/Training/psiTrain1_30km_mid.mat'
varName1 = 'psi1_top_30km'
varName2 = 'psi1_mid'
# load data
data = sio.loadmat( fileName )
# extract high resolution and interpolated streamfunctions
psiTop = data[ varName1 ]
psiMid = data[ varName2 ]
# move time dimension to the left
psiTop = np.moveaxis( psiTop, 2, 0 )
psiMid = np.moveaxis( psiMid, 2, 0 )
# standarize the input variables, i.e. the smoothed streamfunction,
# by removing the mean and dividing by the standard deviation
muTop, sigmaTop = np.mean( psiTop ), np.std( psiTop )
muMid, sigmaMid = np.mean( psiMid ), np.std( psiMid )
psiTop = ( psiTop - muTop ) / sigmaTop
psiMid = ( psiMid - muMid ) / sigmaMid
scalings = [ muTop, sigmaTop, muMid, sigmaMid ]
# split into training and test data
xTrain, xTest = psiTop[:3300, :, :], psiTop[3300:, :, :]
yTrain, yTest = psiMid[:3300, :, :], psiMid[3300:, :, :]
# split the original 160x160 region into 16 sub-regions
# of size 40x40. Then combine into a single data set (which
# will have sixteen times as many training samples).
xTrain = np.reshape( np.array( np.split( np.array( np.split( xTrain, 4, axis=2) ), 4, axis=2) ), (4 * 4 * 3300, 40, 40) )
yTrain = np.reshape( np.array( np.split( np.array( np.split( yTrain, 4, axis=2) ), 4, axis=2) ), (4 * 4 * 3300, 40, 40) )
xTest = np.reshape( np.array( np.split( np.array( np.split(xTest, 4, axis=2) ), 4, axis=2) ), (4 * 4 * 350, 40, 40) )
yTest = np.reshape( np.array( np.split( np.array( np.split( yTest, 4, axis=2) ), 4, axis=2) ), (4 * 4 * 350, 40, 40) )
# add singleton dimension for input variables (this is for Keras)
xTrain = np.reshape( xTrain, (-1, 40, 40, 1) )
xTest = np.reshape( xTest, (-1, 40, 40, 1) )
# reshape outputs from 2D (40x40) to 1D vector 1600
yTrain = np.reshape( yTrain, (-1, 40 * 40) )
yTest = np.reshape( yTest, (-1, 40 * 40) )
return xTrain, yTrain, xTest, yTest, scalings
########################################################################
#
# Training Functions
#
########################################################################
def trainCNN_S( l, axis, region, MOMENTUM_A=False, MOMENTUM_B=False ) :
"""
This function loads data from a particular region of the QG model,
and then trains a convolutional neural network (CNN) to predict
either the zonal (Sx) or meridional (Sy) component of the eddy momentum
forcing. The CNN is trained for 200 epochs, from which the model is
saved, including the validation loss during training as a function
of the number of epochs.
"""
# load data
xTrain, yTrain, xTest, yTest, scalings = loadAndNormDataS( l, axis, region, MOMENTUM_B )
# number of training and validation samples
nTrain = xTrain.shape[0]
nTest = xTest.shape[0]
print "Number of training samples: ", nTrain
print "Number of validation samples: ", nTest
########## Construct Layers ##########
input_layer = Input( shape=( 40, 40, 1 ) )
# Convolution layers
conv_1 = Convolution2D( 16, (8,8), strides=(2,2), padding='valid', activation='selu')( input_layer )
conv_2 = Convolution2D( 8, (4,4), padding='valid', activation='selu')( conv_1 )
conv_3 = Convolution2D( 8, (4,4), padding='valid', activation='selu')( conv_2 )
# Max Pooling
pool_1 = MaxPooling2D( pool_size=(2,2) )( conv_3 )
flat = Flatten()(pool_1)
if MOMENTUM_A :
# dense (fully-connected) layer
dense_1 = Dense( units=40*40, activation='linear' )( flat )
# Lambda layer to remove spatial-mean from time-slice. This is
# approach A to conserving momentum, i.e., altered-architecture.
output_layer = Lambda( lambda x: x - K.mean( x, axis=1, keepdims=True ) )( dense_1 )
else :
# dense (fully-connected) layer
output_layer = Dense( units=40*40, activation='linear' )( flat )
########## Train CNN ###########
myModel = Model( inputs=input_layer, outputs=output_layer )
myOpt = Adam( lr=0.001 )
myModel.compile( loss='mean_squared_error', optimizer=myOpt )
# show the architecture and the parameters
print myModel.summary()
# train the model
History = myModel.fit( xTrain, yTrain, batch_size=16, epochs=200, verbose=2, validation_data=( xTest, yTest ) )
# make file name
fileName = 'cnn30km_'+str(region)+'_S'+axis+'_200e'
if MOMENTUM_A :
fileName = fileName + '_MOM_A'
elif MOMENTUM_B :
fileName = fileName + '_MOM_B'
# save model
myModel.save( fileName + '.h5' )
# save training loss history
with open('history_' + fileName, 'wb') as file_pi :
pickle.dump( History.history, file_pi )
def trainSparseCNN_S( l, axis, region, N ) :
"""
This function loads data from a particular region of the QG model,
and then trains a convolutional neural network (CNN) to predict
either the zonal (Sx) or meridional (Sy) component of the eddy momentum
forcing. The CNN is trained for 200 epochs, from which the model is
saved, including the validation loss during training as a function
of the number of epochs.
Unique to this function is that the training data is sub-sampled to
N randomly chosen spatial locations and reconstructed with a simple
cubic interpolation.
"""
# load data
xTrain, yTrain, xTest, yTest, scalings = makeSparseDataset( l, axis, region, N )
# number of training and validation samples
nTrain = xTrain.shape[0]
nTest = xTest.shape[0]
print "Number of training samples: ", nTrain
print "Number of validation samples: ", nTest
########## Construct Layers ##########
input_layer = Input( shape=( 40, 40, 1 ) )
# Convolution layers
conv_1 = Convolution2D( 16, (8,8), strides=(2,2), padding='valid', activation='selu')( input_layer )
conv_2 = Convolution2D( 8, (4,4), padding='valid', activation='selu')( conv_1 )
conv_3 = Convolution2D( 8, (4,4), padding='valid', activation='selu')( conv_2 )
# Max Pooling
pool_1 = MaxPooling2D( pool_size=(2,2) )( conv_3 )
flat = Flatten()(pool_1)
# dense (fully-connected) layer
output_layer = Dense( units=40*40, activation='linear' )( flat )
########## Train CNN ###########
myModel = Model( inputs=input_layer, outputs=output_layer )
myOpt = Adam( lr=0.001 )
myModel.compile( loss='mean_squared_error', optimizer=myOpt )
# show the architecture and the parameters
print myModel.summary()
# train the model
History = myModel.fit( xTrain, yTrain, batch_size=16, epochs=200, verbose=2, validation_data=( xTest, yTest ) )
# make file name
fileName = 'cnn30km_'+str(region)+'_S'+axis+'_200e_'+str(N)+'ss'
# save model
myModel.save( fileName + '.h5' )
# save training loss history
with open('history_' + fileName, 'wb') as file_pi :
pickle.dump( History.history, file_pi )
def trainCNN_SubSurface() :
"""
This function loads the upper- and middle-layer streamfunction
from region 1, and trains a convolutional neural network to
predict the middle-layer (output) using the upper-layer (input).
The architecture of this neural network is identical to those
used to predict the eddy momentum forcing.
"""
xTrain, yTrain, xTest, yTest, scalings = loadAndNormDataMidPsi()
nTrain = xTrain.shape[0]
nTest = xTest.shape[0]
print nTrain, nTest
########## Construct Layers ##########
input_layer = Input( shape=( 40, 40, 1 ) )
# Convolution layers
conv_1 = Convolution2D( 16, (8,8), strides=(2,2), padding='valid', activation='selu')( input_layer )
conv_2 = Convolution2D( 8, (4,4), padding='valid', activation='selu')( conv_1 )
conv_3 = Convolution2D( 8, (4,4), padding='valid', activation='selu')( conv_2 )
# Max Pooling
pool_1 = MaxPooling2D( pool_size=(2,2) )( conv_3 )
flat = Flatten()(pool_1)
# Dense (fully-connected) layer
output_layer = Dense( units=40*40, activation='linear' )( flat )
########## Train CNN ###########
myModel = Model( inputs=input_layer, outputs=output_layer )
myOpt = Adam( lr=0.001 )
myModel.compile( loss='mean_squared_error', optimizer=myOpt )
# uncomment if you want to load a model instead
#myModel = load_model('cnn30km_1_Sy_200e_MOM_b.h5')
# show the architecture and the parameters
print myModel.summary()
# train the model
History = myModel.fit( xTrain, yTrain, batch_size=16, epochs=100, verbose=2, validation_data=( xTest, yTest ) )
# save the model
myModel.save('cnn30km_1_midPsi_100e.h5')
# save training history
with open('history_cnn30km_1_midPsi_100e', 'wb') as file_pi :
pickle.dump( History.history, file_pi )
########################################################################
#
# Predicting Functions
#
########################################################################
def makeOverlapPreds( l, axis, region, MOMENTUM_A=False, MOMENTUM_B=False, MOMENTUM_C=False ) :
"""
This function loads a trained neural network and makes predictions
for the final year of validation data, over the full region.
As each neural network makes predictions for a 40x40 grid point area,
multiple overlapping predictions have to be made over the full
domain (512x512) and then averaged at each grid point.
region = 1, 2, or 3, the region on which the models are trained
l = length-scale (in km) of the spatial-filtering
model = the CNN trained to predict Sx or Sy
The predictions (and truth) and then saved in zipped numpy files.
"""
modelName = 'cnn30km_'+str(region)+'_S'+axis+'_200e'
if MOMENTUM_A :
modelName = modelName + '_MOM_A'
elif MOMENTUM_B :
modelName = modelName + '_MOM_B'
model = load_model('models/'+modelName+'.h5')
# get scalings for psi, and either Sx or Sy
_, _, _, _, scalings = loadAndNormDataS(l, axis, region )
muPsi, sigmaPsi, muS, sigmaS = scalings[0], scalings[1], scalings[2], scalings[3]
# load filtered-streamfunction from full region (final year) to
# use as the input variable to make preditions with
data = sio.loadmat('data/Validation/psiPred_30km.mat')
# extract filtered streamfunction and anomaly
psiAnom = data['psiPredAnom']
psiFilt = data['psiPred_30km']
# move time dimension to the left
psiAnom = np.moveaxis(psiAnom, 2, 0)
psiFilt = np.moveaxis(psiFilt, 2, 0)
# calculate the TRUE eddy source term
SxTrue, SyTrue = calcEddyForcing(psiAnom, psiFilt, l)
# standarize the input variables, i.e. the smoothed streamfunction,
# by removing the mean and dividing by the standard deviation
psiFilt = (psiFilt - muPsi) / sigmaPsi
# We now want to make the predictions for the entire region, at every
# time step. We move the neural network one grid point at a time over
# the entire region, making predictions as it moves along. The predictions
# at each grid point are then averaged.
SPred = np.zeros( (350,512,512) )
mask = np.zeros( (512,512) )
stride = 2
for i in range( 0, 512-40+1, stride): # loop through points in x
print i # progress update
t0 = time.time()
for j in range( 0, 512-40+1, stride ): # loop through points in y
# make predictions at this point
SPred[:,j:j+40,i:i+40] += model.predict( np.reshape( psiFilt[:,j:j+40,i:i+40], (-1,40,40,1) ) ).reshape( (-1,40,40) )
# update number of predictions made at each grid point
mask[j:j+40,i:i+40] += 1
t1 = time.time()
print t1-t0
# average the predictions
SPred = np.divide( SPred, mask )
# rescale psi, either Sx or Sy
psiFilt = psiFilt * sigmaPsi + muPsi
SPred = SPred * sigmaS + muS
# save as zipped numpy files
fileName = 'S'+axis+'_Pred_R'+str(region)+'_str2'
if MOMENTUM_A :
fileName = fileName + '_MOM_A'
SPred -= muS
elif MOMENTUM_B :
fileName = fileName + '_MOM_B'
SPred -= muS
np.savez(fileName+'.npz', SP=SPred ) # predictions
np.savez('SxSy_True_str2.npz', SxT=SxTrue, SyT=SyTrue ) # truth
def predictNewModels() :
"""
This function loads a trained neural network and makes predictions
for the final year of validation data, over the full region - the
same as the function above 'makeOverlapPreds', but this time using
data from new QG models with different forcings.
Each new model has either a different wind forcing or viscosity.
For each model, predictions are made for one year of data, using the
filtered-streamfunction as the input as before. The various models
have the following parameters:
nu = 200 m2s-1
tau = 0.3 Nm-2
tau = 0.6 Nm-2
tau = 0.9 Nm-2
We use the neural network trained on region 1, namely
'cnn30km_1_Sx_200e.h5', and predict the zonal component Sx
in these new models. The original model that cnn30km_1_Sx_200e.h5
was trained on had a wind forcing of tau = 0.8 Nm-2 and 75 m2s-1.
"""
# load models
model = load_model('models/cnn30km_1_Sx_200e.h5')
# get scalings for psi and Sx
_, _, _, _, scalings = loadAndNormDataS( 30, 'x', 1 )
muPsi, sigmaPsi, muS, sigmaS = scalings[0], scalings[1], scalings[2], scalings[3]
# loop through each of the new models, and make overlapping
# predictions of Sx for one year of data
labels = [ '200', 'tau3', 'tau6', 'tau9' ]
for string in labels :
print "Currently making predictions for model: ", string
data = sio.loadmat('data/Validation/psiPred_30km_'+string+'.mat')
# extract filtered streamfunction and anomaly
psiAnom = data['psiPredAnom']
psiFilt = data['psiPred_30km']
# move time dimension to the left
psiAnom = np.moveaxis(psiAnom, 2, 0)
psiFilt = np.moveaxis(psiFilt, 2, 0)
# calculate the TRUE eddy source term
SxTrue, SyTrue = calcEddyForcing(psiAnom, psiFilt, 30)
# standarize the input variables, i.e. the smoothed streamfunction,
# by removing the mean and dividing by the standard deviation
psiFilt = (psiFilt - muPsi) / sigmaPsi
# We now want to make the predictions for the entire region, at every
# time step. We move the neural network one grid point at a time over
# the entire region, making predictions as it moves along. The predictions
# at each grid point are then averaged.
SPred = np.zeros( (350,512,512) )
mask = np.zeros( (512,512) )
stride = 2
for i in range( 0, 512-40+1, stride): # loop through points in x
if i % 100 == 0 : print i # progress update
t0 = time.time()
for j in range( 0, 512-40+1, stride ): # loop through points in y
# make predictions at this point
SPred[:,j:j+40,i:i+40] += model.predict( np.reshape( psiFilt[:,j:j+40,i:i+40], (-1,40,40,1) ) ).reshape( (-1,40,40) )
# update number of predictions made at each grid point
mask[j:j+40,i:i+40] += 1
t1 = time.time()
print t1-t0
# average the predictions
SPred = np.divide( SPred, mask )
# rescale psi, either Sx or Sy
psiFilt = psiFilt * sigmaPsi + muPsi
SPred = SPred * sigmaS + muS
# save as zipped numpy files
np.savez('Sx_Pred_R1_str2_'+string+'.npz', SP=SPred, ST=SxTrue ) # predictions
def makeSparsePreds() :
"""
Make predictions using the neural networks
trained on sparsely sub-sampled data.
"""
l = 30
axis = 'x'
region = 1
#for N in [ 1500, 1350, 1200, 1050, 900, 750, 600, 450, 300, 150, 75, 30 ] :
for N in [ 1200, 1050, 900, 750, 600, 450, 300, 150, 75, 30 ] :
# load data and get scalings
xTrain, yTrain, xTest, yTest, scalings = makeSparseDataset( l, axis, region, N )
muPsi, sigmaPsi, muS, sigmaS = scalings[0], scalings[1], scalings[2], scalings[3]
# load model
model = load_model('cnn30km_1_Sx_200e_'+str(N)+'ss.h5')
# load filtered-streamfunction from full region (final year) to
# use as the input variable to make preditions with
data = sio.loadmat('data/Validation/psiPred_30km.mat')
# extract filtered streamfunction and anomaly
psiAnom = data['psiPredAnom']
psiFilt = data['psiPred_30km']
# move time dimension to the left
psiAnom = np.moveaxis(psiAnom, 2, 0)
psiFilt = np.moveaxis(psiFilt, 2, 0)
# calculate the TRUE eddy source term
SxTrue, SyTrue = calcEddyForcing(psiAnom, psiFilt, l)
# standarize the input variables, i.e. the smoothed streamfunction,
# by removing the mean and dividing by the standard deviation
psiFilt = (psiFilt - muPsi) / sigmaPsi
# We now want to make the predictions for the entire region, at every
# time step. We move the neural network one grid point at a time over
# the entire region, making predictions as it moves along. The predictions
# at each grid point are then averaged.
SPred = np.zeros( (350,512,512) )
mask = np.zeros( (512,512) )
stride = 2
for i in range( 0, 512-40+1, stride): # loop through points in x
print i # progress update
t0 = time.time()
for j in range( 0, 512-40+1, stride ): # loop through points in y
# make predictions at this point
SPred[:,j:j+40,i:i+40] += model.predict( np.reshape( psiFilt[:,j:j+40,i:i+40], (-1,40,40,1) ) ).reshape( (-1,40,40) )
# update number of predictions made at each grid point
mask[j:j+40,i:i+40] += 1
t1 = time.time()
print t1-t0
# average the predictions
SPred = np.divide( SPred, mask )
# rescale psi, either Sx or Sy
psiFilt = psiFilt * sigmaPsi + muPsi
SPred = SPred * sigmaS + muS
# save as zipped numpy files
np.savez('Sx_Pred_R1_str2_'+str(N)+'ss.npz', SP=SPred, ST=SxTrue ) # predictions
########################################################################
#
# Plotting and Misc Functions
#
########################################################################
def getActivations( X, model ) :
# add a singelton dimension onto X
X = np.reshape( X, (1,40,40,1) )
# Get activations of layer 1
input_layer = Input(shape=(40, 40, 1))
conv_1 = Convolution2D( 16, (8, 8), strides=(2,2), padding='valid', activation='selu', weights=model.layers[1].get_weights() )(input_layer)
newModel1 = Model( inputs=input_layer, outputs=conv_1 )
act1 = np.squeeze( newModel1.predict(X) )
# Get activations of layer 2
conv_2 = Convolution2D( 8, (4, 4), padding='valid', activation='selu', weights=model.layers[2].get_weights() )(conv_1)
newModel2 = Model(inputs=input_layer, outputs=conv_2 )
act2 = np.squeeze( newModel2.predict(X) )
# Get activations of layer 3
conv_3 = Convolution2D( 8, (4, 4), padding='valid', activation='selu', weights=model.layers[3].get_weights() )(conv_2)
newModel3 = Model( inputs=input_layer, outputs=conv_3 )
act3 = np.squeeze( newModel3.predict(X) )
return act1, act2, act3
def plotSyntheticActivationMaps() :
"""
This function examines the activation maps at each
stage of a trained convolutional neural network, by
generating a 'fake' streamfunction as the input.
A radially symmetric Gaussian is used as the fake input
to represent an eddy. The resulting activations maps from
each convolution layer are then plotted.
The specific neural network loaded is the CNN trained on
region 1 to predict Sx.
"""
# load model
myModelSx = load_model('models/cnn30km_1_Sx_200e.h5')
print myModelSx.summary()
# make 'fake' streamfunction
x, y = np.meshgrid(np.linspace(-20,20,40), np.linspace(-20,20,40))
d = np.sqrt(x*x+y*y)
sigma = 8.0
psi = np.exp(-( d**2 / ( 2.0 * sigma**2 ) ) )
##### Calculate activations ######
# make prediction of Sx
Sx = myModelSx.predict( psi.reshape( (1,40,40,1) ) ).reshape( (40,40) )
# calculate the activation layers for two regions
act1, act2, act3 = getActivations( psi, myModelSx )
##### Create subplots #####
# create subplot for input streamfunction
col0Axis = plt.subplot2grid( (8,16), (2,0), colspan=4, rowspan=4 )
col0Axis.axis('off')
# create subplots for each layer
col1Axes = []; col2Axes = []
col3Axes = []; col4Axes = []
for row in range(8) :
# create axes
ax1 = plt.subplot2grid( (8,16), (row,5) ) # 1st layer
ax2 = plt.subplot2grid( (8,16), (row,6) ) # 1st layer
ax3 = plt.subplot2grid( (8,16), (row,8) ) # 2nd layer
ax4 = plt.subplot2grid( (8,16), (row,10) ) # 3rd layer
# add to lists
col1Axes.append( ax1 ); col2Axes.append( ax2 )
col3Axes.append( ax3 ); col4Axes.append( ax4 )
# create subplot for Sx prediction
col5Axis = plt.subplot2grid( (8,16), (2,12), colspan=4, rowspan=4 )
col5Axis.axis('off')
# set size of figure
fig = plt.gcf()
fig.set_size_inches( (16,9) )
##### Plot activations ######
# plot input streamfunction
col0Axis.imshow( psi, cmap='seismic', origin='lower' )
col0Axis.set_title(r'Synthetically-Generated Input ($\overline{\psi}$)')
#col0Axis.text( 1, 36, 'a.', color='white', fontsize=15, fontweight='bold' )
# plot activation functions of all three layers
for row in range(8) :
# turn of the axes of each subplot
col1Axes[row].axis('off'); col2Axes[row].axis('off')
col3Axes[row].axis('off'); col4Axes[row].axis('off')
# Activation Layer I
col1Axes[row].imshow( act1[:,:,row], cmap='seismic', origin='lower' )
col2Axes[row].imshow( act1[:,:,row+8], cmap='seismic', origin='lower' )
# Activation Layer II
col3Axes[row].imshow( act2[:,:,row], cmap='seismic', origin='lower' )
# Activation Layer III
col4Axes[row].imshow( act3[:,:,row], cmap='seismic', origin='lower' )
# plot predicted Sx
col5Axis.imshow( Sx, cmap='seismic', origin='lower' )
col5Axis.set_title(r'Output ($\tilde{S}_x$)')
col5Axis.yaxis.tick_right()
#col5Axis.text( 1, 36, 'e.', color='black', fontsize=15, fontweight='bold' )
# add horizontal arrows between each layer