Coverage for /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/memilio/surrogatemodel/ode_secir_groups/network_architectures.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-11-18 12:29 +0000

1############################################################################# 

2# Copyright (C) 2020-2023 German Aerospace Center (DLR-SC) 

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 

21 

22 

23def mlp_multi_input_single_output(num_age_groups=6): 

24 """! Simple MLP Network which takes the compartments for multiple time steps as input and 

25 returns the 8 compartments for all six age groups for one single time step. 

26 

27 Reshaping adds an extra dimension to the output, so the shape of the output is 1x48. 

28 This makes the shape comparable to that of the multi-output models. 

29 

30 @param num_age_groups Number of age groups in population. 

31 """ 

32 model = tf.keras.Sequential([ 

33 tf.keras.layers.Flatten(), 

34 tf.keras.layers.Dense(units=32, activation='relu'), 

35 tf.keras.layers.Dense(units=32, activation='relu'), 

36 tf.keras.layers.Dense(units=8*num_age_groups), 

37 tf.keras.layers.Reshape([1, -1]), ]) 

38 return model 

39 

40 

41def mlp_multi_input_multi_output(label_width, num_age_groups=6): 

42 """! Simple MLP Network which takes the compartments for multiple time steps as input and 

43 returns the 8 compartments for all age groups for multiple time steps in the future.  

44 

45 Reshaping adds an extra dimension to the output, so the shape of the output is (label_width)x48. 

46 This makes the shape comparable to that of the multi-output models. 

47 

48 @param label_width Number of time steps in the output. 

49 @param num_age_groups Number of age groups in population. 

50 """ 

51 model = tf.keras.Sequential([ 

52 tf.keras.layers.Flatten(), 

53 tf.keras.layers.Dense(units=32, activation='relu'), 

54 tf.keras.layers.Dense(units=32, activation='relu'), 

55 tf.keras.layers.Dense(units=label_width*num_age_groups*8), 

56 tf.keras.layers.Reshape([label_width, num_age_groups*8]) 

57 ]) 

58 return model 

59 

60 

61def cnn_multi_input_multi_output(label_width, num_age_groups=6): 

62 """! CNN Network which uses multiple time steps as input and returns the 8 compartments for 

63 each age group for multiple time steps in the future. 

64 

65 Input and output have shape [number of expert model simulations, time points in simulation, 

66 number of individuals in infection states]. 

67 

68 @param label_width Number of time steps in the output. 

69 @param num_age_groups Number of age groups in population. 

70 

71 """ 

72 

73 model = tf.keras.Sequential([ 

74 tf.keras.layers.Conv1D(filters=64, kernel_size=3, activation='relu'), 

75 tf.keras.layers.Conv1D(filters=64, kernel_size=3, activation='relu'), 

76 # tf.keras.layers.MaxPooling1D(pool_size=2), 

77 tf.keras.layers.Flatten(), 

78 tf.keras.layers.GaussianNoise(0.35), 

79 tf.keras.layers.Dense(512, activation='relu'), 

80 # tf.keras.layers.Dense(512, activation='relu'), 

81 tf.keras.layers.Dense( 

82 label_width * num_age_groups * 8, activation='linear'), 

83 tf.keras.layers.Reshape([label_width, 8 * num_age_groups]) 

84 ]) 

85 return model 

86 

87 

88def lstm_multi_input_multi_output(label_width, num_age_groups=6): 

89 """! LSTM Network which uses multiple time steps as input and returns the 8 compartments for 

90 multiple time steps in the future. 

91 

92 Input and output have shape [number of expert model simulations, time points in simulation, 

93 number of individuals in infection states]. 

94 

95 @param label_width Number of time steps in the output. 

96 """ 

97 model = tf.keras.Sequential([ 

98 tf.keras.layers.LSTM(32, return_sequences=False), 

99 tf.keras.layers.Dense(label_width * 8 * num_age_groups, 

100 kernel_initializer=tf.initializers.zeros()), 

101 tf.keras.layers.Reshape([label_width, 8 * num_age_groups])]) 

102 return model