Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MLAir
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
esde
machine-learning
MLAir
Commits
daf4b146
Commit
daf4b146
authored
5 years ago
by
lukas leufen
Browse files
Options
Downloads
Patches
Plain Diff
moved tower model to model class
parent
aee2fe01
No related branches found
No related tags found
1 merge request
!50
release for v0.7.0
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/model_modules/model_class.py
+111
-0
111 additions, 0 deletions
src/model_modules/model_class.py
src/run_modules/model_setup.py
+0
-87
0 additions, 87 deletions
src/run_modules/model_setup.py
with
111 additions
and
87 deletions
src/model_modules/model_class.py
+
111
−
0
View file @
daf4b146
...
...
@@ -8,6 +8,8 @@ from abc import ABC
from
typing
import
Any
,
Callable
import
keras
from
src.model_modules.inception_model
import
InceptionModelBase
from
src.model_modules.flatten
import
flatten_tail
class
AbstractModelClass
(
ABC
):
...
...
@@ -240,3 +242,112 @@ class MyBranchedModel(AbstractModelClass):
self
.
loss
=
[
keras
.
losses
.
mean_absolute_error
]
+
[
keras
.
losses
.
mean_squared_error
]
+
\
[
keras
.
losses
.
mean_squared_error
]
class
MyTowerModel
(
AbstractModelClass
):
def
__init__
(
self
,
window_history_size
,
window_lead_time
,
channels
):
"""
Sets model and loss depending on the given arguments.
:param activation: activation function
:param window_history_size: number of historical time steps included in the input data
:param channels: number of variables used in input data
:param regularizer: <not used here>
:param dropout_rate: dropout rate used in the model [0, 1)
:param window_lead_time: number of time steps to forecast in the output layer
"""
super
().
__init__
()
# settings
self
.
window_history_size
=
window_history_size
self
.
window_lead_time
=
window_lead_time
self
.
channels
=
channels
self
.
dropout_rate
=
1e-2
self
.
regularizer
=
keras
.
regularizers
.
l2
(
0.1
)
self
.
initial_lr
=
1e-2
self
.
optimizer
=
keras
.
optimizers
.
adam
(
lr
=
self
.
initial_lr
)
self
.
lr_decay
=
src
.
model_modules
.
keras_extensions
.
LearningRateDecay
(
base_lr
=
self
.
initial_lr
,
drop
=
.
94
,
epochs_drop
=
10
)
self
.
epochs
=
20
self
.
batch_size
=
int
(
256
*
4
)
self
.
activation
=
keras
.
layers
.
PReLU
# apply to model
self
.
set_model
()
self
.
set_loss
()
def
set_model
(
self
):
"""
Build the model.
:param activation: activation function
:param window_history_size: number of historical time steps included in the input data
:param channels: number of variables used in input data
:param dropout_rate: dropout rate used in the model [0, 1)
:param window_lead_time: number of time steps to forecast in the output layer
:return: built keras model
"""
activation
=
self
.
activation
conv_settings_dict1
=
{
'
tower_1
'
:
{
'
reduction_filter
'
:
8
,
'
tower_filter
'
:
8
*
2
,
'
tower_kernel
'
:
(
3
,
1
),
'
activation
'
:
activation
},
'
tower_2
'
:
{
'
reduction_filter
'
:
8
,
'
tower_filter
'
:
8
*
2
,
'
tower_kernel
'
:
(
5
,
1
),
'
activation
'
:
activation
},
'
tower_3
'
:
{
'
reduction_filter
'
:
8
,
'
tower_filter
'
:
8
*
2
,
'
tower_kernel
'
:
(
1
,
1
),
'
activation
'
:
activation
},
}
pool_settings_dict1
=
{
'
pool_kernel
'
:
(
3
,
1
),
'
tower_filter
'
:
8
*
2
,
'
activation
'
:
activation
}
conv_settings_dict2
=
{
'
tower_1
'
:
{
'
reduction_filter
'
:
8
*
2
,
'
tower_filter
'
:
16
*
2
*
2
,
'
tower_kernel
'
:
(
3
,
1
),
'
activation
'
:
activation
},
'
tower_2
'
:
{
'
reduction_filter
'
:
8
*
2
,
'
tower_filter
'
:
16
*
2
*
2
,
'
tower_kernel
'
:
(
5
,
1
),
'
activation
'
:
activation
},
'
tower_3
'
:
{
'
reduction_filter
'
:
8
*
2
,
'
tower_filter
'
:
16
*
2
*
2
,
'
tower_kernel
'
:
(
1
,
1
),
'
activation
'
:
activation
},
}
pool_settings_dict2
=
{
'
pool_kernel
'
:
(
3
,
1
),
'
tower_filter
'
:
16
,
'
activation
'
:
activation
}
conv_settings_dict3
=
{
'
tower_1
'
:
{
'
reduction_filter
'
:
16
*
4
,
'
tower_filter
'
:
32
*
2
,
'
tower_kernel
'
:
(
3
,
1
),
'
activation
'
:
activation
},
'
tower_2
'
:
{
'
reduction_filter
'
:
16
*
4
,
'
tower_filter
'
:
32
*
2
,
'
tower_kernel
'
:
(
5
,
1
),
'
activation
'
:
activation
},
'
tower_3
'
:
{
'
reduction_filter
'
:
16
*
4
,
'
tower_filter
'
:
32
*
2
,
'
tower_kernel
'
:
(
1
,
1
),
'
activation
'
:
activation
},
}
pool_settings_dict3
=
{
'
pool_kernel
'
:
(
3
,
1
),
'
tower_filter
'
:
32
,
'
activation
'
:
activation
}
##########################################
inception_model
=
InceptionModelBase
()
X_input
=
keras
.
layers
.
Input
(
shape
=
(
self
.
window_history_size
+
1
,
1
,
self
.
channels
))
# add 1 to window_size to include current time step t0
X_in
=
inception_model
.
inception_block
(
X_input
,
conv_settings_dict1
,
pool_settings_dict1
,
regularizer
=
self
.
regularizer
,
batch_normalisation
=
True
)
X_in
=
keras
.
layers
.
Dropout
(
self
.
dropout_rate
)(
X_in
)
X_in
=
inception_model
.
inception_block
(
X_in
,
conv_settings_dict2
,
pool_settings_dict2
,
regularizer
=
self
.
regularizer
,
batch_normalisation
=
True
)
X_in
=
keras
.
layers
.
Dropout
(
self
.
dropout_rate
)(
X_in
)
X_in
=
inception_model
.
inception_block
(
X_in
,
conv_settings_dict3
,
pool_settings_dict3
,
regularizer
=
self
.
regularizer
,
batch_normalisation
=
True
)
#############################################
out_main
=
flatten_tail
(
X_in
,
'
Main
'
,
activation
=
activation
,
bound_weight
=
True
,
dropout_rate
=
self
.
dropout_rate
,
reduction_filter
=
64
,
first_dense
=
64
,
window_lead_time
=
self
.
window_lead_time
)
self
.
model
=
keras
.
Model
(
inputs
=
X_input
,
outputs
=
[
out_main
])
def
set_loss
(
self
):
"""
Set the loss
:return: loss function
"""
self
.
loss
=
[
keras
.
losses
.
mean_squared_error
]
This diff is collapsed.
Click to expand it.
src/run_modules/model_setup.py
+
0
−
87
View file @
daf4b146
...
...
@@ -104,90 +104,3 @@ class ModelSetup(RunEnvironment):
with
tf
.
device
(
"
/cpu:0
"
):
file_name
=
f
"
{
self
.
model_name
.
split
(
sep
=
'
.
'
)[
0
]
}
.pdf
"
keras
.
utils
.
plot_model
(
self
.
model
,
to_file
=
file_name
,
show_shapes
=
True
,
show_layer_names
=
True
)
def
my_loss
():
loss
=
l_p_loss
(
4
)
keras_loss
=
losses
.
mean_squared_error
loss_all
=
[
loss
]
+
[
keras_loss
]
return
loss_all
def
my_little_loss
():
return
losses
.
mean_squared_error
def
my_little_model
(
activation
,
window_history_size
,
channels
,
regularizer
,
dropout_rate
,
window_lead_time
):
X_input
=
keras
.
layers
.
Input
(
shape
=
(
window_history_size
+
1
,
1
,
channels
))
# add 1 to window_size to include current time step t0
X_in
=
keras
.
layers
.
Conv2D
(
32
,
(
1
,
1
),
padding
=
'
same
'
,
name
=
'
{}_Conv_1x1
'
.
format
(
"
major
"
))(
X_input
)
X_in
=
activation
(
name
=
'
{}_conv_act
'
.
format
(
"
major
"
))(
X_in
)
X_in
=
keras
.
layers
.
Flatten
(
name
=
'
{}
'
.
format
(
"
major
"
))(
X_in
)
X_in
=
keras
.
layers
.
Dropout
(
dropout_rate
,
name
=
'
{}_Dropout_1
'
.
format
(
"
major
"
))(
X_in
)
X_in
=
keras
.
layers
.
Dense
(
64
,
name
=
'
{}_Dense_64
'
.
format
(
"
major
"
))(
X_in
)
X_in
=
activation
()(
X_in
)
X_in
=
keras
.
layers
.
Dense
(
32
,
name
=
'
{}_Dense_32
'
.
format
(
"
major
"
))(
X_in
)
X_in
=
activation
()(
X_in
)
X_in
=
keras
.
layers
.
Dense
(
16
,
name
=
'
{}_Dense_16
'
.
format
(
"
major
"
))(
X_in
)
X_in
=
activation
()(
X_in
)
X_in
=
keras
.
layers
.
Dense
(
window_lead_time
,
name
=
'
{}_Dense
'
.
format
(
"
major
"
))(
X_in
)
out_main
=
activation
()(
X_in
)
return
keras
.
Model
(
inputs
=
X_input
,
outputs
=
[
out_main
])
def
my_model
(
activation
,
window_history_size
,
channels
,
regularizer
,
dropout_rate
,
window_lead_time
):
conv_settings_dict1
=
{
'
tower_1
'
:
{
'
reduction_filter
'
:
8
,
'
tower_filter
'
:
8
*
2
,
'
tower_kernel
'
:
(
3
,
1
),
'
activation
'
:
activation
},
'
tower_2
'
:
{
'
reduction_filter
'
:
8
,
'
tower_filter
'
:
8
*
2
,
'
tower_kernel
'
:
(
5
,
1
),
'
activation
'
:
activation
},
'
tower_3
'
:
{
'
reduction_filter
'
:
8
,
'
tower_filter
'
:
8
*
2
,
'
tower_kernel
'
:
(
1
,
1
),
'
activation
'
:
activation
},
}
pool_settings_dict1
=
{
'
pool_kernel
'
:
(
3
,
1
),
'
tower_filter
'
:
8
*
2
,
'
activation
'
:
activation
}
conv_settings_dict2
=
{
'
tower_1
'
:
{
'
reduction_filter
'
:
8
*
2
,
'
tower_filter
'
:
16
*
2
*
2
,
'
tower_kernel
'
:
(
3
,
1
),
'
activation
'
:
activation
},
'
tower_2
'
:
{
'
reduction_filter
'
:
8
*
2
,
'
tower_filter
'
:
16
*
2
*
2
,
'
tower_kernel
'
:
(
5
,
1
),
'
activation
'
:
activation
},
'
tower_3
'
:
{
'
reduction_filter
'
:
8
*
2
,
'
tower_filter
'
:
16
*
2
*
2
,
'
tower_kernel
'
:
(
1
,
1
),
'
activation
'
:
activation
},
}
pool_settings_dict2
=
{
'
pool_kernel
'
:
(
3
,
1
),
'
tower_filter
'
:
16
,
'
activation
'
:
activation
}
conv_settings_dict3
=
{
'
tower_1
'
:
{
'
reduction_filter
'
:
16
*
4
,
'
tower_filter
'
:
32
*
2
,
'
tower_kernel
'
:
(
3
,
1
),
'
activation
'
:
activation
},
'
tower_2
'
:
{
'
reduction_filter
'
:
16
*
4
,
'
tower_filter
'
:
32
*
2
,
'
tower_kernel
'
:
(
5
,
1
),
'
activation
'
:
activation
},
'
tower_3
'
:
{
'
reduction_filter
'
:
16
*
4
,
'
tower_filter
'
:
32
*
2
,
'
tower_kernel
'
:
(
1
,
1
),
'
activation
'
:
activation
},
}
pool_settings_dict3
=
{
'
pool_kernel
'
:
(
3
,
1
),
'
tower_filter
'
:
32
,
'
activation
'
:
activation
}
##########################################
inception_model
=
InceptionModelBase
()
X_input
=
keras
.
layers
.
Input
(
shape
=
(
window_history_size
+
1
,
1
,
channels
))
# add 1 to window_size to include current time step t0
X_in
=
inception_model
.
inception_block
(
X_input
,
conv_settings_dict1
,
pool_settings_dict1
,
regularizer
=
regularizer
,
batch_normalisation
=
True
)
out_minor
=
flatten_tail
(
X_in
,
'
Minor_1
'
,
bound_weight
=
True
,
activation
=
activation
,
dropout_rate
=
dropout_rate
,
reduction_filter
=
4
,
first_dense
=
32
,
window_lead_time
=
window_lead_time
)
X_in
=
keras
.
layers
.
Dropout
(
dropout_rate
)(
X_in
)
X_in
=
inception_model
.
inception_block
(
X_in
,
conv_settings_dict2
,
pool_settings_dict2
,
regularizer
=
regularizer
,
batch_normalisation
=
True
)
X_in
=
keras
.
layers
.
Dropout
(
dropout_rate
)(
X_in
)
X_in
=
inception_model
.
inception_block
(
X_in
,
conv_settings_dict3
,
pool_settings_dict3
,
regularizer
=
regularizer
,
batch_normalisation
=
True
)
#############################################
out_main
=
flatten_tail
(
X_in
,
'
Main
'
,
activation
=
activation
,
bound_weight
=
True
,
dropout_rate
=
dropout_rate
,
reduction_filter
=
64
,
first_dense
=
64
,
window_lead_time
=
window_lead_time
)
return
keras
.
Model
(
inputs
=
X_input
,
outputs
=
[
out_minor
,
out_main
])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment