|
a |
|
b/ROMNet.py |
|
|
1 |
""" |
|
|
2 |
|
|
|
3 |
Stefania Fresca, MOX Laboratory, Politecnico di Milano |
|
|
4 |
April 2019 |
|
|
5 |
|
|
|
6 |
""" |
|
|
7 |
|
|
|
8 |
import tensorflow as tf |
|
|
9 |
import numpy as np |
|
|
10 |
|
|
|
11 |
from Net import Net |
|
|
12 |
|
|
|
13 |
class ROMNet(Net): |
|
|
14 |
def __init__(self, config): |
|
|
15 |
Net.__init__(self, config) |
|
|
16 |
|
|
|
17 |
self.n = config['n'] |
|
|
18 |
self.n_params = config['n_params'] |
|
|
19 |
|
|
|
20 |
self.size = 5 |
|
|
21 |
self.n_layers = 10 |
|
|
22 |
self.n_neurons = 50 |
|
|
23 |
self.n_h = config['n_h'] |
|
|
24 |
|
|
|
25 |
def inference(self): |
|
|
26 |
# encoder function providing the low-dimensional representation of the FOM solution |
|
|
27 |
conv1 = tf.layers.conv2d(inputs = self.input, |
|
|
28 |
filters = 8, |
|
|
29 |
kernel_size = [self.size, self.size], |
|
|
30 |
padding = 'SAME', |
|
|
31 |
strides = 1, |
|
|
32 |
kernel_initializer = tf.keras.initializers.he_uniform(), |
|
|
33 |
activation = tf.nn.elu, |
|
|
34 |
name = 'conv1') |
|
|
35 |
conv2 = tf.layers.conv2d(inputs = conv1, |
|
|
36 |
filters = 16, |
|
|
37 |
kernel_size = [self.size, self.size], |
|
|
38 |
padding = 'SAME', |
|
|
39 |
strides = 2, |
|
|
40 |
kernel_initializer = tf.keras.initializers.he_uniform(), |
|
|
41 |
activation = tf.nn.elu, |
|
|
42 |
name = 'conv2') |
|
|
43 |
conv3 = tf.layers.conv2d(inputs = conv2, |
|
|
44 |
filters = 32, |
|
|
45 |
kernel_size = [self.size, self.size], |
|
|
46 |
padding = 'SAME', |
|
|
47 |
strides = 2, |
|
|
48 |
kernel_initializer = tf.keras.initializers.he_uniform(), |
|
|
49 |
activation = tf.nn.elu, |
|
|
50 |
name = 'conv3') |
|
|
51 |
conv4 = tf.layers.conv2d(inputs = conv3, |
|
|
52 |
filters = 64, |
|
|
53 |
kernel_size = [self.size, self.size], |
|
|
54 |
padding = 'SAME', |
|
|
55 |
strides = 2, |
|
|
56 |
kernel_initializer = tf.keras.initializers.he_uniform(), |
|
|
57 |
activation = tf.nn.elu, |
|
|
58 |
name = 'conv4') |
|
|
59 |
feature_dim_enc = conv4.shape[1] * conv4.shape[2] * conv4.shape[3] |
|
|
60 |
conv4 = tf.reshape(conv4, [-1, feature_dim_enc]) |
|
|
61 |
fc1 = tf.layers.dense(conv4, 256, activation = tf.nn.elu, kernel_initializer = tf.keras.initializers.he_uniform(), name = 'fc1') |
|
|
62 |
self.enc = tf.layers.dense(fc1, self.n, activation = tf.nn.elu, kernel_initializer = tf.keras.initializers.he_uniform(), name = 'fc2') |
|
|
63 |
# feed-forward neural network for reduced dynamics learning |
|
|
64 |
fc_n = tf.layers.dense(self.params, |
|
|
65 |
self.n_neurons, |
|
|
66 |
activation = tf.nn.elu, |
|
|
67 |
kernel_initializer = tf.keras.initializers.he_uniform()) |
|
|
68 |
for i in range(self.n_layers): |
|
|
69 |
fc_n = tf.layers.dense(fc_n, |
|
|
70 |
self.n_neurons, |
|
|
71 |
activation = tf.nn.elu, |
|
|
72 |
kernel_initializer = tf.keras.initializers.he_uniform()) |
|
|
73 |
self.u_n = tf.layers.dense(fc_n, |
|
|
74 |
self.n, |
|
|
75 |
activation = tf.nn.elu, |
|
|
76 |
kernel_initializer = tf.keras.initializers.he_uniform()) |
|
|
77 |
# decoder function for reduced nonlinear trial manifold learning |
|
|
78 |
fc1_t = tf.layers.dense(self.u_n, 256, activation = tf.nn.elu, kernel_initializer = tf.keras.initializers.he_uniform(), name = 'fc1_t') |
|
|
79 |
fc2_t = tf.layers.dense(fc1_t, self.N_h, activation = tf.nn.elu, kernel_initializer = tf.keras.initializers.he_uniform(), name = 'fc2_t') |
|
|
80 |
fc2_t = tf.reshape(fc2_t, [-1, self.n_h, self.n_h, 64]) |
|
|
81 |
conv1_t = tf.layers.conv2d_transpose(inputs = fc2_t, |
|
|
82 |
filters = 64, |
|
|
83 |
kernel_size = [self.size, self.size], |
|
|
84 |
padding = 'SAME', |
|
|
85 |
strides = 2, |
|
|
86 |
kernel_initializer = tf.keras.initializers.he_uniform(), |
|
|
87 |
activation = tf.nn.elu, |
|
|
88 |
name = 'conv1_t') |
|
|
89 |
conv2_t = tf.layers.conv2d_transpose(inputs = conv1_t, |
|
|
90 |
filters = 32, |
|
|
91 |
kernel_size = [self.size, self.size], |
|
|
92 |
padding = 'SAME', |
|
|
93 |
strides = 2, |
|
|
94 |
kernel_initializer = tf.keras.initializers.he_uniform(), |
|
|
95 |
activation = tf.nn.elu, |
|
|
96 |
name = 'conv2_t') |
|
|
97 |
conv3_t = tf.layers.conv2d_transpose(inputs = conv2_t, |
|
|
98 |
filters = 16, |
|
|
99 |
kernel_size = [self.size, self.size], |
|
|
100 |
padding = 'SAME', |
|
|
101 |
strides = 2, |
|
|
102 |
kernel_initializer = tf.keras.initializers.he_uniform(), |
|
|
103 |
activation = tf.nn.elu, |
|
|
104 |
name = 'conv3_t') |
|
|
105 |
conv4_t = tf.layers.conv2d_transpose(inputs = conv3_t, |
|
|
106 |
filters = 1, |
|
|
107 |
kernel_size = [self.size, self.size], |
|
|
108 |
padding = 'SAME', |
|
|
109 |
strides = 1, |
|
|
110 |
kernel_initializer = tf.keras.initializers.he_uniform(), |
|
|
111 |
name = 'conv4_t') |
|
|
112 |
feature_dim_dec = conv4_t.shape[1] * conv4_t.shape[2] * conv4_t.shape[3] |
|
|
113 |
self.u_h = tf.reshape(conv4_t, [-1, feature_dim_dec]) |