Coverage for /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/memilio/surrogatemodel/ode_secir_simple/network_architectures.py: 100%
13 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-18 12:29 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-18 12:29 +0000
1#############################################################################
2# Copyright (C) 2020-2024 MEmilio
3#
4# Authors: Agatha Schmidt, Henrik Zunker
5#
6# Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7#
8# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#############################################################################
20import tensorflow as tf
23def mlp_multi_input_single_output(num_outputs=8):
24 """! Simple MLP Network which takes the compartments for multiple time steps as input and returns the 8 compartments for one single time step.
26 Reshaping adds an extra dimension to the output, so the shape of the output is 1x8. This makes the shape comparable to that of the multi-output models.
27 @param num_outputs [Default: 8] Number of compartments. Default value is reached when aggregating the confirmed compartments.
28 """
29 model = tf.keras.Sequential([
30 tf.keras.layers.Flatten(),
31 tf.keras.layers.Dense(units=32, activation='relu'),
32 tf.keras.layers.Dense(units=32, activation='relu'),
33 tf.keras.layers.Dense(units=num_outputs),
34 tf.keras.layers.Reshape([1, -1]), ])
35 return model
38def lstm_network_multi_input_single_output(num_outputs=8):
39 """! LSTM Network which uses multiple time steps as input and returns the 8 compartments for one single time step in the future.
41 Input and output have shape [number of expert model simulations, time points in simulation, number of individuals in infection states].
43 @param num_outputs [Default: 8] Number of compartments. Default value is reached when aggregating the confirmed compartments.
44 """
45 model = tf.keras.models.Sequential([
46 tf.keras.layers.LSTM(32, return_sequences=True),
47 tf.keras.layers.Dense(units=num_outputs)
48 ])
49 return model
52def cnn_multi_input_multi_output(label_width, conv_size=3, num_outputs=8):
53 """! CNN Network which uses multiple time steps as input and returns the 8 compartments for multiple time step in the future.
55 Input and output have shape [number of expert model simulations, time points in simulation, number of individuals in infection states].
56 The parameter conv_size describes the kernel_size of the 1d Conv layer.
57 We also use the parameter in combination with a lambda layer to transform the input to shape [batch, CONV_WIDTH, features].
60 @param label_width Number of time steps in the output.
61 @param conv_size [Default: 3] Convolution kernel width which is 3 per default.
62 @param num_outputs [Default: 8] Number of compartments. Default value is reached when aggregating the confirmed compartments.
63 """
65 model = tf.keras.Sequential([
66 tf.keras.layers.Lambda(lambda x: x[:, -conv_size:, :]),
67 tf.keras.layers.Conv1D(256, activation='relu',
68 kernel_size=(conv_size)),
69 tf.keras.layers.Dense(label_width*num_outputs,
70 kernel_initializer=tf.initializers.zeros()),
71 tf.keras.layers.Reshape([label_width, num_outputs])
72 ])
73 return model
76def lstm_multi_input_multi_output(label_width, num_outputs=8):
77 """! LSTM Network which uses multiple time steps as input and returns the 8 compartments for one single time step in the future.
79 Input and output have shape [number of expert model simulations, time points in simulation, number of individuals in infection states].
81 @param label_width Number of time steps in the output.
82 @param num_outputs [Default: 8] Number of compartments. Default value is reached when aggregating the confirmed compartments.
83 """
84 model = tf.keras.Sequential([
85 tf.keras.layers.LSTM(32, return_sequences=False),
86 tf.keras.layers.Dense(label_width*num_outputs,
87 kernel_initializer=tf.initializers.zeros()),
88 tf.keras.layers.Reshape([label_width, num_outputs])])
89 return model