Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
E
EMADL2CPP
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
10
Issues
10
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Test Cases
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
monticore
EmbeddedMontiArc
generators
EMADL2CPP
Commits
90023d67
Commit
90023d67
authored
Mar 06, 2020
by
Julian Dierkes
Browse files
Options
Browse Files
Download
Plain Diff
fixed merge request
parents
18fe21dd
d763cb6c
Changes
13
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
495 additions
and
385 deletions
+495
-385
.gitlab-ci.yml
.gitlab-ci.yml
+7
-7
pom.xml
pom.xml
+1
-1
src/test/java/de/monticore/lang/monticar/emadl/IntegrationPythonWrapperTest.java
...ore/lang/monticar/emadl/IntegrationPythonWrapperTest.java
+0
-2
src/test/resources/target_code/CNNCreator_cifar10_cifar10Classifier_net.py
...s/target_code/CNNCreator_cifar10_cifar10Classifier_net.py
+294
-294
src/test/resources/target_code/gluon/CNNDataLoader_mnist_mnistClassifier_net.py
...get_code/gluon/CNNDataLoader_mnist_mnistClassifier_net.py
+89
-27
src/test/resources/target_code/gluon/CNNNet_mnist_mnistClassifier_net.py
...ces/target_code/gluon/CNNNet_mnist_mnistClassifier_net.py
+6
-6
src/test/resources/target_code/gluon/CNNSupervisedTrainer_mnist_mnistClassifier_net.py
...e/gluon/CNNSupervisedTrainer_mnist_mnistClassifier_net.py
+56
-8
src/test/resources/target_code/gluon/reinforcementModel/cartpole/CNNNet_cartpole_master_dqn.py
...reinforcementModel/cartpole/CNNNet_cartpole_master_dqn.py
+7
-6
src/test/resources/target_code/gluon/reinforcementModel/mountaincar/CNNNet_mountaincar_master_actor.py
...ementModel/mountaincar/CNNNet_mountaincar_master_actor.py
+7
-7
src/test/resources/target_code/gluon/reinforcementModel/mountaincar/reinforcement_learning/CNNNet_mountaincar_agent_mountaincarCritic.py
...nt_learning/CNNNet_mountaincar_agent_mountaincarCritic.py
+7
-6
src/test/resources/target_code/gluon/reinforcementModel/torcs/CNNNet_torcs_agent_torcsAgent_dqn.py
...forcementModel/torcs/CNNNet_torcs_agent_torcsAgent_dqn.py
+7
-7
src/test/resources/target_code/gluon/reinforcementModel/torcs_td3/CNNNet_torcs_agent_torcsAgent_actor.py
...entModel/torcs_td3/CNNNet_torcs_agent_torcsAgent_actor.py
+7
-7
src/test/resources/target_code/gluon/reinforcementModel/torcs_td3/reinforcement_learning/CNNNet_torcs_agent_network_torcsCritic.py
...cement_learning/CNNNet_torcs_agent_network_torcsCritic.py
+7
-7
No files found.
.gitlab-ci.yml
View file @
90023d67
...
...
@@ -10,7 +10,7 @@ git masterJobLinux:
stage
:
deploy
image
:
maven:3-jdk-8
script
:
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B clean deploy --settings settings.xml -DskipTests
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B
-U
clean deploy --settings settings.xml -DskipTests
# - cat target/site/jacoco/index.html
# - mvn package sonar:sonar -s settings.xml
only
:
...
...
@@ -21,34 +21,34 @@ integrationMXNetJobLinux:
stage
:
linux
image
:
registry.git.rwth-aachen.de/monticore/embeddedmontiarc/generators/emadl2cpp/integrationtests/mxnet:v0.0.4
script
:
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B clean install --settings settings.xml -Dtest=IntegrationMXNetTest
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B
-U
clean install --settings settings.xml -Dtest=IntegrationMXNetTest
integrationCaffe2JobLinux
:
stage
:
linux
image
:
registry.git.rwth-aachen.de/monticore/embeddedmontiarc/generators/emadl2cpp/integrationtests/caffe2:v0.0.5
script
:
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B clean install --settings settings.xml -Dtest=IntegrationCaffe2Test
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B
-U
clean install --settings settings.xml -Dtest=IntegrationCaffe2Test
integrationGluonJobLinux
:
stage
:
linux
image
:
registry.git.rwth-aachen.de/monticore/embeddedmontiarc/generators/emadl2cpp/integrationtests/mxnet:v0.0.4
script
:
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B clean install --settings settings.xml -Dtest=IntegrationGluonTest
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B
-U
clean install --settings settings.xml -Dtest=IntegrationGluonTest
integrationTensorflowJobLinux
:
stage
:
linux
image
:
registry.git.rwth-aachen.de/monticore/embeddedmontiarc/generators/emadl2cpp/integrationtests/tensorflow
script
:
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B clean install --settings settings.xml -Dtest=IntegrationTensorflowTest
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B
-U
clean install --settings settings.xml -Dtest=IntegrationTensorflowTest
integrationPythonWrapperTest
:
stage
:
linux
image
:
registry.git.rwth-aachen.de/monticore/embeddedmontiarc/generators/emadl2pythonwrapper/tests/mvn-swig:latest
script
:
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B clean install --settings settings.xml -Dtest=IntegrationPythonWrapperTest
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B
-U
clean install --settings settings.xml -Dtest=IntegrationPythonWrapperTest
masterJobWindows
:
...
...
@@ -63,6 +63,6 @@ UnitTestJobLinux:
stage
:
linux
image
:
maven:3-jdk-8
script
:
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B clean install sonar:sonar --settings settings.xml -Dtest="GenerationTest,SymtabTest*"
-
mvn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn -B
-U
clean install sonar:sonar --settings settings.xml -Dtest="GenerationTest,SymtabTest*"
# - cat target/site/jacoco/index.html
pom.xml
View file @
90023d67
...
...
@@ -23,7 +23,7 @@
<cnnarch-caffe2-generator.version>
0.2.14-SNAPSHOT
</cnnarch-caffe2-generator.version>
<cnnarch-gluon-generator.version>
0.2.10-SNAPSHOT
</cnnarch-gluon-generator.version>
<cnnarch-tensorflow-generator.version>
0.1.0-SNAPSHOT
</cnnarch-tensorflow-generator.version>
<Common-MontiCar.version>
0.0.1
4-20180704.113055-2
</Common-MontiCar.version>
<Common-MontiCar.version>
0.0.1
9-SNAPSHOT
</Common-MontiCar.version>
<embedded-montiarc-math-opt-generator>
0.1.6
</embedded-montiarc-math-opt-generator>
<!-- .. Libraries .................................................. -->
...
...
src/test/java/de/monticore/lang/monticar/emadl/IntegrationPythonWrapperTest.java
View file @
90023d67
...
...
@@ -62,14 +62,12 @@ public class IntegrationPythonWrapperTest extends AbstractSymtabTest {
"reinforcement_learning/cnnarch_logger.py"
)
);
/*
assertTrue
(
Paths
.
get
(
"./target/generated-sources-emadl/reinforcement_learning/_torcs_agent_dqn_reward_executor.so"
)
.
toFile
().
exists
());
assertTrue
(
Paths
.
get
(
"./target/generated-sources-emadl/reinforcement_learning/torcs_agent_dqn_reward_executor.py"
)
.
toFile
().
exists
());
*/
}
@Test
...
...
src/test/resources/target_code/CNNCreator_cifar10_cifar10Classifier_net.py
View file @
90023d67
This diff is collapsed.
Click to expand it.
src/test/resources/target_code/gluon/CNNDataLoader_mnist_mnistClassifier_net.py
View file @
90023d67
...
...
@@ -15,7 +15,7 @@ class CNNDataLoader_mnist_mnistClassifier_net:
def
__init__
(
self
):
self
.
_data_dir
=
"data/mnist.LeNetNetwork/"
def
load_data
(
self
,
batch_size
):
def
load_data
(
self
,
batch_size
,
shuffle
=
False
):
train_h5
,
test_h5
=
self
.
load_h5_files
()
train_data
=
{}
...
...
@@ -39,7 +39,8 @@ class CNNDataLoader_mnist_mnistClassifier_net:
train_iter
=
mx
.
io
.
NDArrayIter
(
data
=
train_data
,
label
=
train_label
,
batch_size
=
batch_size
)
batch_size
=
batch_size
,
shuffle
=
shuffle
)
test_iter
=
None
...
...
@@ -64,47 +65,108 @@ class CNNDataLoader_mnist_mnistClassifier_net:
return
train_iter
,
test_iter
,
data_mean
,
data_std
,
train_images
,
test_images
def
load_
data_img
(
self
,
batch_size
,
img_siz
e
):
def
load_
preprocessed_data
(
self
,
batch_size
,
preproc_lib
,
shuffle
=
Fals
e
):
train_h5
,
test_h5
=
self
.
load_h5_files
()
width
=
img_size
[
0
]
height
=
img_size
[
1
]
comb_data
=
{}
wrapper
=
importlib
.
import_module
(
preproc_lib
)
instance
=
getattr
(
wrapper
,
preproc_lib
)()
instance
.
init
()
lib_head
,
_sep
,
tail
=
preproc_lib
.
rpartition
(
'_'
)
inp
=
getattr
(
wrapper
,
lib_head
+
"_input"
)()
train_data
=
{}
train_label
=
{}
data_mean
=
{}
data_std
=
{}
shape_output
=
self
.
preprocess_data
(
instance
,
inp
,
0
,
train_h5
)
train_len
=
len
(
train_h5
[
self
.
_input_names_
[
0
]])
for
input_name
in
self
.
_input_names_
:
train_data
=
train_h5
[
input_name
][:]
test_data
=
test_h5
[
input_name
][:]
if
type
(
getattr
(
shape_output
,
input_name
+
"_out"
))
==
np
.
ndarray
:
cur_shape
=
(
train_len
,)
+
getattr
(
shape_output
,
input_name
+
"_out"
).
shape
else
:
cur_shape
=
(
train_len
,
1
)
train_data
[
input_name
]
=
mx
.
nd
.
zeros
(
cur_shape
)
for
output_name
in
self
.
_output_names_
:
if
type
(
getattr
(
shape_output
,
output_name
+
"_out"
))
==
nd
.
array
:
cur_shape
=
(
train_len
,)
+
getattr
(
shape_output
,
output_name
+
"_out"
).
shape
else
:
cur_shape
=
(
train_len
,
1
)
train_label
[
output_name
]
=
mx
.
nd
.
zeros
(
cur_shape
)
for
i
in
range
(
train_len
):
output
=
self
.
preprocess_data
(
instance
,
inp
,
i
,
train_h5
)
for
input_name
in
self
.
_input_names_
:
train_data
[
input_name
][
i
]
=
getattr
(
output
,
input_name
+
"_out"
)
for
output_name
in
self
.
_output_names_
:
train_label
[
output_name
][
i
]
=
getattr
(
shape_output
,
output_name
+
"_out"
)
for
input_name
in
self
.
_input_names_
:
data_mean
[
input_name
+
'_'
]
=
nd
.
array
(
train_data
[
input_name
][:].
mean
(
axis
=
0
))
data_std
[
input_name
+
'_'
]
=
nd
.
array
(
train_data
[
input_name
][:].
asnumpy
().
std
(
axis
=
0
)
+
1e-5
)
train_shape
=
train_data
.
shape
test_shape
=
test_data
.
shape
if
'images'
in
train_h5
:
train_images
=
train_h5
[
'images'
]
train_iter
=
mx
.
io
.
NDArrayIter
(
data
=
train_data
,
label
=
train_label
,
batch_size
=
batch_size
,
shuffle
=
shuffle
)
comb_data
[
input_name
]
=
mx
.
nd
.
zeros
((
train_shape
[
0
]
+
test_shape
[
0
],
train_shape
[
1
],
width
,
height
))
for
i
,
img
in
enumerate
(
train_data
):
img
=
img
.
transpose
(
1
,
2
,
0
)
comb_data
[
input_name
][
i
]
=
cv2
.
resize
(
img
,
(
width
,
height
)).
reshape
((
train_shape
[
1
],
width
,
height
))
for
i
,
img
in
enumerate
(
test_data
):
img
=
img
.
transpose
(
1
,
2
,
0
)
comb_data
[
input_name
][
i
+
train_shape
[
0
]]
=
cv2
.
resize
(
img
,
(
width
,
height
)).
reshape
((
train_shape
[
1
],
width
,
height
))
test_data
=
{}
test_label
=
{}
data_mean
[
input_name
+
'_'
]
=
nd
.
array
(
comb_data
[
input_name
][:].
mean
(
axis
=
0
)
)
data_std
[
input_name
+
'_'
]
=
nd
.
array
(
comb_data
[
input_name
][:].
asnumpy
().
std
(
axis
=
0
)
+
1e-5
)
shape_output
=
self
.
preprocess_data
(
instance
,
inp
,
0
,
test_h5
)
test_len
=
len
(
test_h5
[
self
.
_input_names_
[
0
]]
)
comb_label
=
{}
for
input_name
in
self
.
_input_names_
:
if
type
(
getattr
(
shape_output
,
input_name
+
"_out"
))
==
np
.
ndarray
:
cur_shape
=
(
test_len
,)
+
getattr
(
shape_output
,
input_name
+
"_out"
).
shape
else
:
cur_shape
=
(
test_len
,
1
)
test_data
[
input_name
]
=
mx
.
nd
.
zeros
(
cur_shape
)
for
output_name
in
self
.
_output_names_
:
train_labels
=
train_h5
[
output_name
][:]
test_labels
=
test_h5
[
output_name
][:]
comb_label
[
output_name
]
=
np
.
append
(
train_labels
,
test_labels
,
axis
=
0
)
if
type
(
getattr
(
shape_output
,
output_name
+
"_out"
))
==
nd
.
array
:
cur_shape
=
(
test_len
,)
+
getattr
(
shape_output
,
output_name
+
"_out"
).
shape
else
:
cur_shape
=
(
test_len
,
1
)
test_label
[
output_name
]
=
mx
.
nd
.
zeros
(
cur_shape
)
for
i
in
range
(
test_len
):
output
=
self
.
preprocess_data
(
instance
,
inp
,
i
,
test_h5
)
for
input_name
in
self
.
_input_names_
:
test_data
[
input_name
][
i
]
=
getattr
(
output
,
input_name
+
"_out"
)
for
output_name
in
self
.
_output_names_
:
test_label
[
output_name
][
i
]
=
getattr
(
shape_output
,
output_name
+
"_out"
)
if
'images'
in
test_h5
:
test_images
=
test_h5
[
'images'
]
t
rain_iter
=
mx
.
io
.
NDArrayIter
(
data
=
comb
_data
,
label
=
comb
_label
,
t
est_iter
=
mx
.
io
.
NDArrayIter
(
data
=
test
_data
,
label
=
test
_label
,
batch_size
=
batch_size
)
test_iter
=
None
return
train_iter
,
test_iter
,
data_mean
,
data_std
,
train_images
,
test_images
return
train_iter
,
test_iter
,
data_mean
,
data_std
def
preprocess_data
(
self
,
instance_wrapper
,
input_wrapper
,
index
,
data_h5
):
for
input_name
in
self
.
_input_names_
:
data
=
data_h5
[
input_name
][
0
]
attr
=
getattr
(
input_wrapper
,
input_name
)
if
(
type
(
data
))
==
np
.
ndarray
:
data
=
np
.
asfortranarray
(
data
).
astype
(
attr
.
dtype
)
else
:
data
=
type
(
attr
)(
data
)
setattr
(
input_wrapper
,
input_name
,
data
)
for
output_name
in
self
.
_output_names_
:
data
=
data_h5
[
output_name
][
0
]
attr
=
getattr
(
input_wrapper
,
output_name
)
if
(
type
(
data
))
==
np
.
ndarray
:
data
=
np
.
asfortranarray
(
data
).
astype
(
attr
.
dtype
)
else
:
data
=
type
(
attr
)(
data
)
setattr
(
input_wrapper
,
output_name
,
data
)
return
instance_wrapper
.
execute
(
input_wrapper
)
def
load_preprocessed_data
(
self
,
batch_size
,
preproc_lib
):
train_h5
,
test_h5
=
self
.
load_h5_files
()
...
...
src/test/resources/target_code/gluon/CNNNet_mnist_mnistClassifier_net.py
View file @
90023d67
...
...
@@ -52,10 +52,10 @@ class Reshape(gluon.HybridBlock):
class
CustomRNN
(
gluon
.
HybridBlock
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
bidirectional
,
**
kwargs
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
dropout
,
bidirectional
,
**
kwargs
):
super
(
CustomRNN
,
self
).
__init__
(
**
kwargs
)
with
self
.
name_scope
():
self
.
rnn
=
gluon
.
rnn
.
RNN
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
self
.
rnn
=
gluon
.
rnn
.
RNN
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
dropout
=
dropout
,
bidirectional
=
bidirectional
,
activation
=
'tanh'
,
layout
=
'NTC'
)
def
hybrid_forward
(
self
,
F
,
data
,
state0
):
...
...
@@ -64,10 +64,10 @@ class CustomRNN(gluon.HybridBlock):
class
CustomLSTM
(
gluon
.
HybridBlock
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
bidirectional
,
**
kwargs
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
dropout
,
bidirectional
,
**
kwargs
):
super
(
CustomLSTM
,
self
).
__init__
(
**
kwargs
)
with
self
.
name_scope
():
self
.
lstm
=
gluon
.
rnn
.
LSTM
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
self
.
lstm
=
gluon
.
rnn
.
LSTM
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
dropout
=
dropout
,
bidirectional
=
bidirectional
,
layout
=
'NTC'
)
def
hybrid_forward
(
self
,
F
,
data
,
state0
,
state1
):
...
...
@@ -76,10 +76,10 @@ class CustomLSTM(gluon.HybridBlock):
class
CustomGRU
(
gluon
.
HybridBlock
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
bidirectional
,
**
kwargs
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
dropout
,
bidirectional
,
**
kwargs
):
super
(
CustomGRU
,
self
).
__init__
(
**
kwargs
)
with
self
.
name_scope
():
self
.
gru
=
gluon
.
rnn
.
GRU
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
self
.
gru
=
gluon
.
rnn
.
GRU
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
dropout
=
dropout
,
bidirectional
=
bidirectional
,
layout
=
'NTC'
)
def
hybrid_forward
(
self
,
F
,
data
,
state0
):
...
...
src/test/resources/target_code/gluon/CNNSupervisedTrainer_mnist_mnistClassifier_net.py
View file @
90023d67
...
...
@@ -143,7 +143,12 @@ class BLEU(mx.metric.EvalMetric):
if
self
.
_size_hyp
>=
self
.
_size_ref
:
return
1
else
:
return
math
.
exp
(
1
-
(
self
.
_size_ref
/
self
.
_size_hyp
))
if
self
.
_size_hyp
>
0
:
size_hyp
=
self
.
_size_hyp
else
:
size_hyp
=
1
return
math
.
exp
(
1
-
(
self
.
_size_ref
/
size_hyp
))
@
staticmethod
def
_get_ngrams
(
sentence
,
n
):
...
...
@@ -191,7 +196,10 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
context
=
'gpu'
,
save_attention_image
=
False
,
use_teacher_forcing
=
False
,
normalize
=
True
):
normalize
=
True
,
shuffle_data
=
False
,
clip_global_grad_norm
=
None
,
preprocessing
=
False
):
if
context
==
'gpu'
:
mx_context
=
mx
.
gpu
()
elif
context
==
'cpu'
:
...
...
@@ -199,6 +207,12 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
else
:
logging
.
error
(
"Context argument is '"
+
context
+
"'. Only 'cpu' and 'gpu are valid arguments'."
)
if
preprocessing
:
preproc_lib
=
"CNNPreprocessor_mnist_mnistClassifier_net_executor"
train_iter
,
test_iter
,
data_mean
,
data_std
,
train_images
,
test_images
=
self
.
_data_loader
.
load_preprocessed_data
(
batch_size
,
preproc_lib
,
shuffle_data
)
else
:
train_iter
,
test_iter
,
data_mean
,
data_std
,
train_images
,
test_images
=
self
.
_data_loader
.
load_data
(
batch_size
,
shuffle_data
)
if
'weight_decay'
in
optimizer_params
:
optimizer_params
[
'wd'
]
=
optimizer_params
[
'weight_decay'
]
del
optimizer_params
[
'weight_decay'
]
...
...
@@ -214,8 +228,6 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
del
optimizer_params
[
'step_size'
]
del
optimizer_params
[
'learning_rate_decay'
]
train_iter
,
test_iter
,
data_mean
,
data_std
,
train_images
,
test_images
=
self
.
_data_loader
.
load_data
(
batch_size
)
if
normalize
:
self
.
_net_creator
.
construct
(
context
=
mx_context
,
data_mean
=
data_mean
,
data_std
=
data_std
)
else
:
...
...
@@ -276,6 +288,15 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
tic
=
None
for
epoch
in
range
(
begin_epoch
,
begin_epoch
+
num_epoch
):
if
shuffle_data
:
if
preprocessing
:
preproc_lib
=
"CNNPreprocessor_mnist_mnistClassifier_net_executor"
train_iter
,
test_iter
,
data_mean
,
data_std
,
train_images
,
test_images
=
self
.
_data_loader
.
load_preprocessed_data
(
batch_size
,
preproc_lib
,
shuffle_data
)
else
:
train_iter
,
test_iter
,
data_mean
,
data_std
,
train_images
,
test_images
=
self
.
_data_loader
.
load_data
(
batch_size
,
shuffle_data
)
global_loss_train
=
0.0
train_batches
=
0
loss_total
=
0
train_iter
.
reset
()
...
...
@@ -304,6 +325,17 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
loss_total
+=
loss
.
sum
().
asscalar
()
global_loss_train
+=
loss
.
sum
().
asscalar
()
train_batches
+=
1
if
clip_global_grad_norm
:
grads
=
[]
for
network
in
self
.
_networks
.
values
():
grads
.
extend
([
param
.
grad
(
mx_context
)
for
param
in
network
.
collect_params
().
values
()])
gluon
.
utils
.
clip_global_norm
(
grads
,
clip_global_grad_norm
)
for
trainer
in
trainers
:
trainer
.
step
(
batch_size
)
...
...
@@ -323,6 +355,8 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
tic
=
time
.
time
()
global_loss_train
/=
(
train_batches
*
batch_size
)
tic
=
None
...
...
@@ -340,10 +374,12 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
nd
.
waitall
()
outputs
=
[]
attentionList
=
[]
lossList
=
[]
attentionList
=
[]
predictions_
=
self
.
_networks
[
0
](
image_
)
outputs
.
append
(
predictions_
)
lossList
.
append
(
loss_function
(
predictions_
,
labels
[
0
]))
if
save_attention_image
==
"True"
:
...
...
@@ -399,6 +435,9 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
else
:
train_metric_score
=
0
global_loss_test
=
0.0
test_batches
=
0
test_iter
.
reset
()
metric
=
mx
.
metric
.
create
(
eval_metric
,
**
eval_metric_params
)
for
batch_i
,
batch
in
enumerate
(
test_iter
):
...
...
@@ -413,10 +452,12 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
nd
.
waitall
()
outputs
=
[]
attentionList
=
[]
lossList
=
[]
attentionList
=
[]
predictions_
=
self
.
_networks
[
0
](
image_
)
outputs
.
append
(
predictions_
)
lossList
.
append
(
loss_function
(
predictions_
,
labels
[
0
]))
if
save_attention_image
==
"True"
:
...
...
@@ -460,6 +501,12 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
os
.
makedirs
(
target_dir
)
plt
.
savefig
(
target_dir
+
'/attention_test.png'
)
plt
.
close
()
loss
=
0
for
element
in
lossList
:
loss
=
loss
+
element
global_loss_test
+=
loss
.
sum
().
asscalar
()
test_batches
+=
1
predictions
=
[]
for
output_name
in
outputs
:
...
...
@@ -472,15 +519,16 @@ class CNNSupervisedTrainer_mnist_mnistClassifier_net:
metric
.
update
(
preds
=
predictions
,
labels
=
labels
)
test_metric_score
=
metric
.
get
()[
1
]
logging
.
info
(
"Epoch[%d] Train: %f, Test: %f"
%
(
epoch
,
train_metric_score
,
test_metric_score
)
)
global_loss_test
/=
(
test_batches
*
batch_size
)
logging
.
info
(
"Epoch[%d] Train metric: %f, Test metric: %f, Train loss: %f, Test loss: %f"
%
(
epoch
,
train_metric_score
,
test_metric_score
,
global_loss_train
,
global_loss_test
))
if
(
epoch
-
begin_epoch
)
%
checkpoint_period
==
0
:
for
i
,
network
in
self
.
_networks
.
items
():
network
.
save_parameters
(
self
.
parameter_path
(
i
)
+
'-'
+
str
(
epoch
).
zfill
(
4
)
+
'.params'
)
for
i
,
network
in
self
.
_networks
.
items
():
network
.
save_parameters
(
self
.
parameter_path
(
i
)
+
'-'
+
str
(
num_epoch
+
begin_epoch
).
zfill
(
4
)
+
'.params'
)
network
.
save_parameters
(
self
.
parameter_path
(
i
)
+
'-'
+
str
(
num_epoch
+
begin_epoch
+
1
).
zfill
(
4
)
+
'.params'
)
network
.
export
(
self
.
parameter_path
(
i
)
+
'_newest'
,
epoch
=
0
)
def
parameter_path
(
self
,
index
):
...
...
src/test/resources/target_code/gluon/reinforcementModel/cartpole/CNNNet_cartpole_master_dqn.py
View file @
90023d67
...
...
@@ -52,10 +52,10 @@ class Reshape(gluon.HybridBlock):
class
CustomRNN
(
gluon
.
HybridBlock
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
bidirectional
,
**
kwargs
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
dropout
,
bidirectional
,
**
kwargs
):
super
(
CustomRNN
,
self
).
__init__
(
**
kwargs
)
with
self
.
name_scope
():
self
.
rnn
=
gluon
.
rnn
.
RNN
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
self
.
rnn
=
gluon
.
rnn
.
RNN
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
dropout
=
dropout
,
bidirectional
=
bidirectional
,
activation
=
'tanh'
,
layout
=
'NTC'
)
def
hybrid_forward
(
self
,
F
,
data
,
state0
):
...
...
@@ -64,10 +64,10 @@ class CustomRNN(gluon.HybridBlock):
class
CustomLSTM
(
gluon
.
HybridBlock
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
bidirectional
,
**
kwargs
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
dropout
,
bidirectional
,
**
kwargs
):
super
(
CustomLSTM
,
self
).
__init__
(
**
kwargs
)
with
self
.
name_scope
():
self
.
lstm
=
gluon
.
rnn
.
LSTM
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
self
.
lstm
=
gluon
.
rnn
.
LSTM
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
dropout
=
dropout
,
bidirectional
=
bidirectional
,
layout
=
'NTC'
)
def
hybrid_forward
(
self
,
F
,
data
,
state0
,
state1
):
...
...
@@ -76,10 +76,10 @@ class CustomLSTM(gluon.HybridBlock):
class
CustomGRU
(
gluon
.
HybridBlock
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
bidirectional
,
**
kwargs
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
dropout
,
bidirectional
,
**
kwargs
):
super
(
CustomGRU
,
self
).
__init__
(
**
kwargs
)
with
self
.
name_scope
():
self
.
gru
=
gluon
.
rnn
.
GRU
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
self
.
gru
=
gluon
.
rnn
.
GRU
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
dropout
=
dropout
,
bidirectional
=
bidirectional
,
layout
=
'NTC'
)
def
hybrid_forward
(
self
,
F
,
data
,
state0
):
...
...
@@ -127,6 +127,7 @@ class Net_0(gluon.HybridBlock):
inputs
=
{}
input_dimensions
=
(
4
)
input_domains
=
(
float
,
float
(
'-inf'
),
float
(
'inf'
))
input_domains
=
(
float
,
0
,
1
)
inputs
[
"state_"
]
=
input_domains
+
(
input_dimensions
,)
return
inputs
...
...
src/test/resources/target_code/gluon/reinforcementModel/mountaincar/CNNNet_mountaincar_master_actor.py
View file @
90023d67
...
...
@@ -52,10 +52,10 @@ class Reshape(gluon.HybridBlock):
class
CustomRNN
(
gluon
.
HybridBlock
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
bidirectional
,
**
kwargs
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
dropout
,
bidirectional
,
**
kwargs
):
super
(
CustomRNN
,
self
).
__init__
(
**
kwargs
)
with
self
.
name_scope
():
self
.
rnn
=
gluon
.
rnn
.
RNN
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
self
.
rnn
=
gluon
.
rnn
.
RNN
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
dropout
=
dropout
,
bidirectional
=
bidirectional
,
activation
=
'tanh'
,
layout
=
'NTC'
)
def
hybrid_forward
(
self
,
F
,
data
,
state0
):
...
...
@@ -64,10 +64,10 @@ class CustomRNN(gluon.HybridBlock):
class
CustomLSTM
(
gluon
.
HybridBlock
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
bidirectional
,
**
kwargs
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
dropout
,
bidirectional
,
**
kwargs
):
super
(
CustomLSTM
,
self
).
__init__
(
**
kwargs
)
with
self
.
name_scope
():
self
.
lstm
=
gluon
.
rnn
.
LSTM
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
self
.
lstm
=
gluon
.
rnn
.
LSTM
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
dropout
=
dropout
,
bidirectional
=
bidirectional
,
layout
=
'NTC'
)
def
hybrid_forward
(
self
,
F
,
data
,
state0
,
state1
):
...
...
@@ -76,10 +76,10 @@ class CustomLSTM(gluon.HybridBlock):
class
CustomGRU
(
gluon
.
HybridBlock
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
bidirectional
,
**
kwargs
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
dropout
,
bidirectional
,
**
kwargs
):
super
(
CustomGRU
,
self
).
__init__
(
**
kwargs
)
with
self
.
name_scope
():
self
.
gru
=
gluon
.
rnn
.
GRU
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
self
.
gru
=
gluon
.
rnn
.
GRU
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
dropout
=
dropout
,
bidirectional
=
bidirectional
,
layout
=
'NTC'
)
def
hybrid_forward
(
self
,
F
,
data
,
state0
):
...
...
@@ -128,7 +128,7 @@ class Net_0(gluon.HybridBlock):
def
getInputs
(
self
):
inputs
=
{}
input_dimensions
=
(
2
)
input_domains
=
(
float
,
float
(
'-inf'
),
float
(
'inf'
)
)
input_domains
=
(
float
,
0
,
1
)
inputs
[
"state_"
]
=
input_domains
+
(
input_dimensions
,)
return
inputs
...
...
src/test/resources/target_code/gluon/reinforcementModel/mountaincar/reinforcement_learning/CNNNet_mountaincar_agent_mountaincarCritic.py
View file @
90023d67
...
...
@@ -52,10 +52,10 @@ class Reshape(gluon.HybridBlock):
class
CustomRNN
(
gluon
.
HybridBlock
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
bidirectional
,
**
kwargs
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
dropout
,
bidirectional
,
**
kwargs
):
super
(
CustomRNN
,
self
).
__init__
(
**
kwargs
)
with
self
.
name_scope
():
self
.
rnn
=
gluon
.
rnn
.
RNN
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
self
.
rnn
=
gluon
.
rnn
.
RNN
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
dropout
=
dropout
,
bidirectional
=
bidirectional
,
activation
=
'tanh'
,
layout
=
'NTC'
)
def
hybrid_forward
(
self
,
F
,
data
,
state0
):
...
...
@@ -64,10 +64,10 @@ class CustomRNN(gluon.HybridBlock):
class
CustomLSTM
(
gluon
.
HybridBlock
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
bidirectional
,
**
kwargs
):
def
__init__
(
self
,
hidden_size
,
num_layers
,
dropout
,
bidirectional
,
**
kwargs
):
super
(
CustomLSTM
,
self
).
__init__
(
**
kwargs
)
with
self
.
name_scope
():
self
.
lstm
=
gluon
.
rnn
.
LSTM
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
self
.
lstm
=
gluon
.
rnn
.
LSTM
(
hidden_size
=
hidden_size
,
num_layers
=
num_layers
,
dropout
=
dropout
,
bidirectional
=
bidirectional
,
layout
=
'NTC'
)
def
hybrid_forward
(
self
,
F
,
data
,
state0
,
state1
):
...
...
@@ -76,10 +76,10 @@ class CustomLSTM(gluon.HybridBlock):