Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
TOPFARM
TopFarm2
Commits
9afbb711
Commit
9afbb711
authored
Nov 25, 2019
by
Riccardo Riva
Committed by
Mikkel Friis-Møller
Nov 25, 2019
Browse files
Added support for wind2load plugin
parent
5a5971a4
Pipeline
#11016
passed with stages
in 8 minutes and 17 seconds
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
topfarm/constraint_components/load.py
View file @
9afbb711
...
...
@@ -70,13 +70,14 @@ _switch_output[type(lambda: None)] = _predict_output_python_function
_switch_gradient
[
type
(
lambda
:
None
)]
=
_predict_gradient_python_function
# %%
Functions related to
scikit-learn neural networks.
# %%
Add
scikit-learn neural networks
to the switch dictionaries
.
def
_predict_output_scikit_MLPRegressor
(
model
,
input
):
"""
Predict output function for scikit-learn MLPRegressor objects.
"""
output
=
model
.
predict
(
input
)
# Ensure that output is 2D even when there is only one output channel.
if
output
.
ndim
==
1
:
output
=
output
.
reshape
(
-
1
,
1
)
return
output
...
...
@@ -85,7 +86,7 @@ def _predict_output_scikit_MLPRegressor(model, input):
_switch_output
[
MLPRegressor
]
=
_predict_output_scikit_MLPRegressor
# %%
Import functions related to OpenTURNS and tensorflow neural network
s.
# %%
Add plugins to the switch dictionarie
s.
if
'openturnsloads'
in
topfarm
.
plugins
:
...
...
@@ -102,6 +103,13 @@ if 'tensorflowloads' in topfarm.plugins:
_switch_output
.
update
(
_switch_output_update
)
_switch_gradient
.
update
(
_switch_gradient_update
)
if
'wind2loads'
in
topfarm
.
plugins
:
from
wind2loads.load
import
update_switch
_switch_output_update
,
_switch_gradient_update
=
update_switch
()
_switch_output
.
update
(
_switch_output_update
)
_switch_gradient
.
update
(
_switch_gradient_update
)
# %% Functions for evaluating surrogate models.
...
...
@@ -123,9 +131,10 @@ def predict_output(model,
----------
model : python function
scikit-learn MLPRegressor
The model to be evaluated, which
must
be a Multiple Input
Sing
le Output.
The model to be evaluated, which
can
be a Multiple Input
Multip
le Output.
Support for additional model types is provided through the Loads cutting
edge plugins.
edge plugins. model must return a 2D array, where each row is a
different sample, and each column a different output.
input : numpy.ndarray
dict
...
...
@@ -159,7 +168,8 @@ def predict_output(model,
-------
output : numpy.ndarray
Model output, optionally scaled through output_scaler.
1D array, where each element is associated to a different sample.
2D array, where each row is a different sample, and each column a
different output.
extrapolation_sample : list
Identifiers of the points outside of the boundary.
...
...
@@ -173,11 +183,6 @@ def predict_output(model,
"""
# This function should already work for the Multiple Output case,
# but we must be careful with the shape of the output:
# - always 2D?
# - 1D or 2D depending on the case?
# Form the input array.
if
type
(
input
)
is
dict
:
input_array
=
np
.
column_stack
(
...
...
topfarm/tests/test_constraint/test_loads.py
View file @
9afbb711
...
...
@@ -49,7 +49,7 @@ def test_predict_output_1():
input
=
prng
.
rand
(
5
,
2
)
def
model
(
input
):
return
input
[:,
0
]
*
input
[:,
1
]
return
(
input
[:,
0
]
*
input
[:,
1
]
).
reshape
(
-
1
,
1
)
output_ok
=
np
.
array
(
model
(
input
))
output_try
,
_
=
predict_output
(
model
,
input
)
...
...
@@ -66,7 +66,7 @@ def test_predict_output_2():
'x1'
:
[
4.0
,
5.0
,
6.0
]}
def
model
(
input
):
return
input
[:,
0
]
*
input
[:,
1
]
return
(
input
[:,
0
]
*
input
[:,
1
]
).
reshape
(
-
1
,
1
)
output_ok
=
np
.
array
(
model
(
np
.
array
([
input
[
'x0'
],
input
[
'x1'
]]).
transpose
()))
output_try
,
_
=
predict_output
(
model
,
input
,
model_in_keys
=
list
(
input
))
...
...
@@ -85,7 +85,7 @@ def test_predict_output_with_input_scaler():
input_scaled
=
input_scaler
.
fit_transform
(
input
)
def
model
(
input
):
return
input
[:,
0
]
*
input
[:,
1
]
return
(
input
[:,
0
]
*
input
[:,
1
]
).
reshape
(
-
1
,
1
)
output_ok
=
model
(
input_scaled
)
output_try
,
_
=
predict_output
(
model
,
input
,
input_scaler
=
input_scaler
)
...
...
@@ -102,28 +102,28 @@ def test_predict_output_with_output_scaler():
input
=
prng
.
rand
(
5
,
2
)
def
model
(
input
):
return
-
100.0
+
200.0
*
input
[:,
0
]
*
input
[:,
1
]
return
(
-
100.0
+
200.0
*
input
[:,
0
]
*
input
[:,
1
]
).
reshape
(
-
1
,
1
)
output_ok
=
model
(
input
)
output_scaler_mm
=
MinMaxScaler
()
output_scaler_std
=
StandardScaler
()
output_scaler_mm
.
fit
(
output_ok
.
reshape
(
-
1
,
1
)
)
output_scaler_std
.
fit
(
output_ok
.
reshape
(
-
1
,
1
)
)
output_scaler_mm
.
fit
(
output_ok
)
output_scaler_std
.
fit
(
output_ok
)
def
model_scaled_mm
(
input
):
output
=
model
(
input
)
return
output_scaler_mm
.
transform
(
output
.
reshape
(
-
1
,
1
)
)
return
output_scaler_mm
.
transform
(
output
)
def
model_scaled_std
(
input
):
output
=
model
(
input
)
return
output_scaler_std
.
transform
(
output
.
reshape
(
-
1
,
1
)
)
return
output_scaler_std
.
transform
(
output
)
output_try_mm
,
_
=
predict_output
(
model_scaled_mm
,
input
,
output_scaler
=
output_scaler_mm
)
output_try_std
,
_
=
predict_output
(
model_scaled_std
,
input
,
output_scaler
=
output_scaler_std
)
output_try_mm
=
output_try_mm
.
ravel
()
output_try_std
=
output_try_std
.
ravel
()
output_try_mm
=
output_try_mm
output_try_std
=
output_try_std
assert
np
.
allclose
(
output_ok
,
output_try_mm
)
assert
np
.
allclose
(
output_ok
,
output_try_std
)
...
...
@@ -167,7 +167,7 @@ def test_boundary():
return
2
*
input
def
bound_fun
(
x
):
return
np
.
abs
(
x
)
<
1
return
np
.
abs
(
x
)
<
=
1
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
_
,
extrapolation_sample
=
predict_output
(
...
...
@@ -303,13 +303,13 @@ def test_predict_gradient_with_output_scaler():
def
fun
(
input
):
x0
,
x1
=
input
.
T
return
x0
**
2
*
np
.
sin
(
x1
)
return
(
x0
**
2
*
np
.
sin
(
x1
)
).
reshape
(
-
1
,
1
)
output_ok
=
fun
(
input
)
output_scaler_mm
=
MinMaxScaler
()
output_scaler_std
=
StandardScaler
()
output_scaler_mm
.
fit
(
output_ok
.
reshape
(
-
1
,
1
)
)
output_scaler_std
.
fit
(
output_ok
.
reshape
(
-
1
,
1
)
)
output_scaler_mm
.
fit
(
output_ok
)
output_scaler_std
.
fit
(
output_ok
)
def
Dfun_Dx0
(
input
):
x0
,
x1
=
input
.
T
...
...
@@ -349,7 +349,7 @@ def test_MLPRegressor():
prng
=
RandomState
(
seed
=
0
)
input
=
prng
.
rand
(
100
,
1
)
output
=
2.0
*
input
.
ravel
()
output
=
2.0
*
input
model
=
MLPRegressor
(
hidden_layer_sizes
=
(
1
),
solver
=
'lbfgs'
,
...
...
@@ -357,10 +357,9 @@ def test_MLPRegressor():
max_iter
=
3000
,
activation
=
'identity'
,
learning_rate
=
'adaptive'
)
model
.
fit
(
input
,
output
)
model
.
fit
(
input
,
output
.
ravel
()
)
predicted_output
,
_
=
predict_output
(
model
,
np
.
array
(
input
))
predicted_output
=
predicted_output
.
ravel
()
assert
np
.
allclose
(
output
,
predicted_output
,
atol
=
1.e-3
)
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment