Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
C
CNNArch2Gluon
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
1
Issues
1
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
Package Registry
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
CNNArch2Gluon
Commits
78559a89
Commit
78559a89
authored
Jun 07, 2019
by
Nicola Gatto
Committed by
Evgeny Kusmenko
Jun 07, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement ddpg
parent
736a2256
Changes
86
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
86 changed files
with
11591 additions
and
1962 deletions
+11591
-1962
pom.xml
pom.xml
+3
-3
src/main/java/de/monticore/lang/monticar/cnnarch/gluongenerator/CNNArch2Gluon.java
...e/lang/monticar/cnnarch/gluongenerator/CNNArch2Gluon.java
+61
-13
src/main/java/de/monticore/lang/monticar/cnnarch/gluongenerator/CNNTrain2Gluon.java
.../lang/monticar/cnnarch/gluongenerator/CNNTrain2Gluon.java
+62
-8
src/main/java/de/monticore/lang/monticar/cnnarch/gluongenerator/ReinforcementConfigurationData.java
...nnarch/gluongenerator/ReinforcementConfigurationData.java
+160
-8
src/main/java/de/monticore/lang/monticar/cnnarch/gluongenerator/annotations/ArchitectureAdapter.java
...narch/gluongenerator/annotations/ArchitectureAdapter.java
+88
-0
src/main/java/de/monticore/lang/monticar/cnnarch/gluongenerator/reinforcement/critic/CriticNetworkGenerationException.java
...einforcement/critic/CriticNetworkGenerationException.java
+7
-0
src/main/java/de/monticore/lang/monticar/cnnarch/gluongenerator/reinforcement/critic/CriticNetworkGenerationPair.java
...tor/reinforcement/critic/CriticNetworkGenerationPair.java
+21
-0
src/main/java/de/monticore/lang/monticar/cnnarch/gluongenerator/reinforcement/critic/CriticNetworkGenerator.java
...enerator/reinforcement/critic/CriticNetworkGenerator.java
+211
-0
src/main/resources/templates/gluon/CNNCreator.ftl
src/main/resources/templates/gluon/CNNCreator.ftl
+2
-2
src/main/resources/templates/gluon/CNNNet.ftl
src/main/resources/templates/gluon/CNNNet.ftl
+1
-1
src/main/resources/templates/gluon/elements/Input.ftl
src/main/resources/templates/gluon/elements/Input.ftl
+3
-3
src/main/resources/templates/gluon/reinforcement/Trainer.ftl
src/main/resources/templates/gluon/reinforcement/Trainer.ftl
+101
-109
src/main/resources/templates/gluon/reinforcement/agent/Agent.ftl
...n/resources/templates/gluon/reinforcement/agent/Agent.ftl
+769
-375
src/main/resources/templates/gluon/reinforcement/agent/ReplayMemory.ftl
...rces/templates/gluon/reinforcement/agent/ReplayMemory.ftl
+95
-42
src/main/resources/templates/gluon/reinforcement/agent/Strategy.ftl
...esources/templates/gluon/reinforcement/agent/Strategy.ftl
+172
-0
src/main/resources/templates/gluon/reinforcement/architecture/CriticArchitecture.ftl
...s/gluon/reinforcement/architecture/CriticArchitecture.ftl
+7
-0
src/main/resources/templates/gluon/reinforcement/environment/Environment.ftl
...templates/gluon/reinforcement/environment/Environment.ftl
+41
-29
src/main/resources/templates/gluon/reinforcement/params/DdpgAgentParams.ftl
.../templates/gluon/reinforcement/params/DdpgAgentParams.ftl
+19
-0
src/main/resources/templates/gluon/reinforcement/params/DqnAgentParams.ftl
...s/templates/gluon/reinforcement/params/DqnAgentParams.ftl
+23
-0
src/main/resources/templates/gluon/reinforcement/params/ReplayMemoryParams.ftl
...mplates/gluon/reinforcement/params/ReplayMemoryParams.ftl
+14
-0
src/main/resources/templates/gluon/reinforcement/params/StrategyParams.ftl
...s/templates/gluon/reinforcement/params/StrategyParams.ftl
+39
-0
src/main/resources/templates/gluon/reinforcement/util/Logger.ftl
...n/resources/templates/gluon/reinforcement/util/Logger.ftl
+95
-0
src/main/resources/templates/gluon/reinforcement/util/Util.ftl
...ain/resources/templates/gluon/reinforcement/util/Util.ftl
+187
-51
src/test/java/de/monticore/lang/monticar/cnnarch/gluongenerator/GenerationTest.java
.../lang/monticar/cnnarch/gluongenerator/GenerationTest.java
+98
-3
src/test/java/de/monticore/lang/monticar/cnnarch/gluongenerator/IntegrationPythonWrapperTest.java
.../cnnarch/gluongenerator/IntegrationPythonWrapperTest.java
+8
-4
src/test/java/de/monticore/lang/monticar/cnnarch/gluongenerator/util/TrainedArchitectureMockFactory.java
...h/gluongenerator/util/TrainedArchitectureMockFactory.java
+48
-0
src/test/resources/target_code/CNNNet_Alexnet.py
src/test/resources/target_code/CNNNet_Alexnet.py
+4
-4
src/test/resources/target_code/CNNNet_CifarClassifierNetwork.py
...st/resources/target_code/CNNNet_CifarClassifierNetwork.py
+4
-4
src/test/resources/target_code/CNNNet_VGG16.py
src/test/resources/target_code/CNNNet_VGG16.py
+4
-4
src/test/resources/target_code/ReinforcementConfig1/CNNTrainer_reinforcementConfig1.py
...e/ReinforcementConfig1/CNNTrainer_reinforcementConfig1.py
+74
-59
src/test/resources/target_code/ReinforcementConfig1/reinforcement_learning/action_policy.py
...nforcementConfig1/reinforcement_learning/action_policy.py
+0
-73
src/test/resources/target_code/ReinforcementConfig1/reinforcement_learning/agent.py
...code/ReinforcementConfig1/reinforcement_learning/agent.py
+769
-375
src/test/resources/target_code/ReinforcementConfig1/reinforcement_learning/cnnarch_logger.py
...forcementConfig1/reinforcement_learning/cnnarch_logger.py
+95
-0
src/test/resources/target_code/ReinforcementConfig1/reinforcement_learning/environment.py
...einforcementConfig1/reinforcement_learning/environment.py
+10
-10
src/test/resources/target_code/ReinforcementConfig1/reinforcement_learning/replay_memory.py
...nforcementConfig1/reinforcement_learning/replay_memory.py
+95
-42
src/test/resources/target_code/ReinforcementConfig1/reinforcement_learning/strategy.py
...e/ReinforcementConfig1/reinforcement_learning/strategy.py
+172
-0
src/test/resources/target_code/ReinforcementConfig1/reinforcement_learning/util.py
..._code/ReinforcementConfig1/reinforcement_learning/util.py
+187
-51
src/test/resources/target_code/ReinforcementConfig2/CNNTrainer_reinforcementConfig2.py
...e/ReinforcementConfig2/CNNTrainer_reinforcementConfig2.py
+83
-66
src/test/resources/target_code/ReinforcementConfig2/reinforcement_learning/action_policy.py
...nforcementConfig2/reinforcement_learning/action_policy.py
+0
-73
src/test/resources/target_code/ReinforcementConfig2/reinforcement_learning/agent.py
...code/ReinforcementConfig2/reinforcement_learning/agent.py
+769
-375
src/test/resources/target_code/ReinforcementConfig2/reinforcement_learning/cnnarch_logger.py
...forcementConfig2/reinforcement_learning/cnnarch_logger.py
+95
-0
src/test/resources/target_code/ReinforcementConfig2/reinforcement_learning/environment.py
...einforcementConfig2/reinforcement_learning/environment.py
+0
-7
src/test/resources/target_code/ReinforcementConfig2/reinforcement_learning/replay_memory.py
...nforcementConfig2/reinforcement_learning/replay_memory.py
+95
-42
src/test/resources/target_code/ReinforcementConfig2/reinforcement_learning/strategy.py
...e/ReinforcementConfig2/reinforcement_learning/strategy.py
+172
-0
src/test/resources/target_code/ReinforcementConfig2/reinforcement_learning/util.py
..._code/ReinforcementConfig2/reinforcement_learning/util.py
+187
-51
src/test/resources/target_code/ReinforcementConfig3/CNNTrainer_reinforcementConfig3.py
...e/ReinforcementConfig3/CNNTrainer_reinforcementConfig3.py
+117
-0
src/test/resources/target_code/ReinforcementConfig3/reinforcement_learning/__init__.py
...e/ReinforcementConfig3/reinforcement_learning/__init__.py
+0
-0
src/test/resources/target_code/ReinforcementConfig3/reinforcement_learning/agent.py
...code/ReinforcementConfig3/reinforcement_learning/agent.py
+900
-0
src/test/resources/target_code/ReinforcementConfig3/reinforcement_learning/cnnarch_logger.py
...forcementConfig3/reinforcement_learning/cnnarch_logger.py
+95
-0
src/test/resources/target_code/ReinforcementConfig3/reinforcement_learning/environment.py
...einforcementConfig3/reinforcement_learning/environment.py
+134
-0
src/test/resources/target_code/ReinforcementConfig3/reinforcement_learning/replay_memory.py
...nforcementConfig3/reinforcement_learning/replay_memory.py
+208
-0
src/test/resources/target_code/ReinforcementConfig3/reinforcement_learning/strategy.py
...e/ReinforcementConfig3/reinforcement_learning/strategy.py
+172
-0
src/test/resources/target_code/ReinforcementConfig3/reinforcement_learning/util.py
..._code/ReinforcementConfig3/reinforcement_learning/util.py
+276
-0
src/test/resources/target_code/ReinforcementConfig3/start_training.sh
...ources/target_code/ReinforcementConfig3/start_training.sh
+2
-0
src/test/resources/target_code/ddpg/CNNTrainer_actorNetwork.py
...est/resources/target_code/ddpg/CNNTrainer_actorNetwork.py
+136
-0
src/test/resources/target_code/ddpg/reinforcement_learning/CNNCreator_CriticNetwork.py
...e/ddpg/reinforcement_learning/CNNCreator_CriticNetwork.py
+56
-0
src/test/resources/target_code/ddpg/reinforcement_learning/CNNNet_CriticNetwork.py
..._code/ddpg/reinforcement_learning/CNNNet_CriticNetwork.py
+119
-0
src/test/resources/target_code/ddpg/reinforcement_learning/__init__.py
...urces/target_code/ddpg/reinforcement_learning/__init__.py
+0
-0
src/test/resources/target_code/ddpg/reinforcement_learning/agent.py
...esources/target_code/ddpg/reinforcement_learning/agent.py
+900
-0
src/test/resources/target_code/ddpg/reinforcement_learning/cnnarch_logger.py
...target_code/ddpg/reinforcement_learning/cnnarch_logger.py
+95
-0
src/test/resources/target_code/ddpg/reinforcement_learning/environment.py
...es/target_code/ddpg/reinforcement_learning/environment.py
+64
-0
src/test/resources/target_code/ddpg/reinforcement_learning/replay_memory.py
.../target_code/ddpg/reinforcement_learning/replay_memory.py
+208
-0
src/test/resources/target_code/ddpg/reinforcement_learning/strategy.py
...urces/target_code/ddpg/reinforcement_learning/strategy.py
+172
-0
src/test/resources/target_code/ddpg/reinforcement_learning/util.py
...resources/target_code/ddpg/reinforcement_learning/util.py
+276
-0
src/test/resources/target_code/ddpg/start_training.sh
src/test/resources/target_code/ddpg/start_training.sh
+2
-0
src/test/resources/target_code/reinforcement_learning/action_policy.py
...urces/target_code/reinforcement_learning/action_policy.py
+0
-73
src/test/resources/target_code/reinforcement_learning/strategy.py
.../resources/target_code/reinforcement_learning/strategy.py
+172
-0
src/test/resources/target_code/ros-ddpg/CNNTrainer_rosActorNetwork.py
...ources/target_code/ros-ddpg/CNNTrainer_rosActorNetwork.py
+141
-0
src/test/resources/target_code/ros-ddpg/reinforcement_learning/CNNCreator_RosCriticNetwork.py
...dpg/reinforcement_learning/CNNCreator_RosCriticNetwork.py
+56
-0
src/test/resources/target_code/ros-ddpg/reinforcement_learning/CNNNet_RosCriticNetwork.py
...os-ddpg/reinforcement_learning/CNNNet_RosCriticNetwork.py
+119
-0
src/test/resources/target_code/ros-ddpg/reinforcement_learning/__init__.py
...s/target_code/ros-ddpg/reinforcement_learning/__init__.py
+0
-0
src/test/resources/target_code/ros-ddpg/reinforcement_learning/agent.py
...rces/target_code/ros-ddpg/reinforcement_learning/agent.py
+900
-0
src/test/resources/target_code/ros-ddpg/reinforcement_learning/cnnarch_logger.py
...et_code/ros-ddpg/reinforcement_learning/cnnarch_logger.py
+95
-0
src/test/resources/target_code/ros-ddpg/reinforcement_learning/environment.py
...arget_code/ros-ddpg/reinforcement_learning/environment.py
+149
-0
src/test/resources/target_code/ros-ddpg/reinforcement_learning/replay_memory.py
...get_code/ros-ddpg/reinforcement_learning/replay_memory.py
+208
-0
src/test/resources/target_code/ros-ddpg/reinforcement_learning/strategy.py
...s/target_code/ros-ddpg/reinforcement_learning/strategy.py
+172
-0
src/test/resources/target_code/ros-ddpg/reinforcement_learning/util.py
...urces/target_code/ros-ddpg/reinforcement_learning/util.py
+276
-0
src/test/resources/target_code/ros-ddpg/start_training.sh
src/test/resources/target_code/ros-ddpg/start_training.sh
+2
-0
src/test/resources/valid_tests/ReinforcementConfig1.cnnt
src/test/resources/valid_tests/ReinforcementConfig1.cnnt
+1
-1
src/test/resources/valid_tests/ReinforcementConfig2.cnnt
src/test/resources/valid_tests/ReinforcementConfig2.cnnt
+4
-1
src/test/resources/valid_tests/ReinforcementConfig3.cnnt
src/test/resources/valid_tests/ReinforcementConfig3.cnnt
+44
-0
src/test/resources/valid_tests/ddpg-ros/RosActorNetwork.cnnt
src/test/resources/valid_tests/ddpg-ros/RosActorNetwork.cnnt
+66
-0
src/test/resources/valid_tests/ddpg-ros/comp/RosCriticNetwork.cnna
...resources/valid_tests/ddpg-ros/comp/RosCriticNetwork.cnna
+14
-0
src/test/resources/valid_tests/ddpg-ros/reward/RewardFunction.emadl
...esources/valid_tests/ddpg-ros/reward/RewardFunction.emadl
+15
-0
src/test/resources/valid_tests/ddpg/ActorNetwork.cnnt
src/test/resources/valid_tests/ddpg/ActorNetwork.cnnt
+61
-0
src/test/resources/valid_tests/ddpg/comp/CriticNetwork.cnna
src/test/resources/valid_tests/ddpg/comp/CriticNetwork.cnna
+14
-0
No files found.
pom.xml
View file @
78559a89
...
...
@@ -8,7 +8,7 @@
<groupId>
de.monticore.lang.monticar
</groupId>
<artifactId>
cnnarch-gluon-generator
</artifactId>
<version>
0.2.
0
-SNAPSHOT
</version>
<version>
0.2.
1
-SNAPSHOT
</version>
<!-- == PROJECT DEPENDENCIES ============================================= -->
...
...
@@ -16,8 +16,8 @@
<!-- .. SE-Libraries .................................................. -->
<CNNArch.version>
0.3.0-SNAPSHOT
</CNNArch.version>
<CNNTrain.version>
0.3.
0
-SNAPSHOT
</CNNTrain.version>
<CNNArch2MXNet.version>
0.2.1
4
-SNAPSHOT
</CNNArch2MXNet.version>
<CNNTrain.version>
0.3.
2
-SNAPSHOT
</CNNTrain.version>
<CNNArch2MXNet.version>
0.2.1
5
-SNAPSHOT
</CNNArch2MXNet.version>
<embedded-montiarc-math-opt-generator>
0.1.4
</embedded-montiarc-math-opt-generator>
<EMADL2PythonWrapper.version>
0.0.1
</EMADL2PythonWrapper.version>
...
...
src/main/java/de/monticore/lang/monticar/cnnarch/gluongenerator/CNNArch2Gluon.java
View file @
78559a89
...
...
@@ -20,11 +20,13 @@
*/
package
de.monticore.lang.monticar.cnnarch.gluongenerator
;
import
de.monticore.lang.monticar.cnnarch._symboltable.IOSymbol
;
import
de.monticore.lang.monticar.cnnarch.mxnetgenerator.CNNArch2MxNet
;
import
de.monticore.lang.monticar.cnnarch.mxnetgenerator.Target
;
import
de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol
;
import
de.monticore.lang.monticar.cnnarch.mxnetgenerator.TemplateConfiguration
;
import
de.se_rwth.commons.logging.Log
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -34,35 +36,81 @@ public class CNNArch2Gluon extends CNNArch2MxNet {
//check cocos with CNNArchCocos.checkAll(architecture) before calling this method.
@Override
public
Map
<
String
,
String
>
generateStrings
(
ArchitectureSymbol
architecture
){
TemplateConfiguration
templateConfiguration
=
new
GluonTemplateConfiguration
();
Map
<
String
,
String
>
fileContentMap
=
compileFileContentMap
(
architecture
);
checkValidGeneration
(
architecture
);
return
fileContentMap
;
}
public
Map
<
String
,
String
>
generateStringsAllowMultipleIO
(
ArchitectureSymbol
architecture
,
Boolean
pythonFilesOnly
)
{
Map
<
String
,
String
>
fileContentMap
;
if
(
pythonFilesOnly
)
{
fileContentMap
=
compilePythonFilesOnlyContentMap
(
architecture
);
}
else
{
fileContentMap
=
compileFileContentMap
(
architecture
);
}
checkValidOutputTypes
(
architecture
);
return
fileContentMap
;
}
private
void
checkValidOutputTypes
(
ArchitectureSymbol
architecture
)
{
if
(((
IOSymbol
)
architecture
.
getOutputs
().
get
(
0
)).
getDefinition
().
getType
().
getWidth
()
!=
1
||
((
IOSymbol
)
architecture
.
getOutputs
().
get
(
0
)).
getDefinition
().
getType
().
getHeight
()
!=
1
)
{
Log
.
error
(
"This cnn architecture has a multi-dimensional output, which is currently not supported by"
+
" the code generator."
,
architecture
.
getSourcePosition
());
}
}
private
Map
<
String
,
String
>
compilePythonFiles
(
CNNArch2GluonTemplateController
controller
,
ArchitectureSymbol
architecture
)
{
Map
<
String
,
String
>
fileContentMap
=
new
HashMap
<>();
CNNArch2GluonTemplateController
archTc
=
new
CNNArch2GluonTemplateController
(
architecture
,
templateConfiguration
);
Map
.
Entry
<
String
,
String
>
temp
;
temp
=
archTc
.
process
(
"CNNPredictor"
,
Target
.
CPP
);
fileContentMap
.
put
(
temp
.
getKey
(),
temp
.
getValue
());
temp
=
archTc
.
process
(
"CNNNet"
,
Target
.
PYTHON
);
temp
=
controller
.
process
(
"CNNNet"
,
Target
.
PYTHON
);
fileContentMap
.
put
(
temp
.
getKey
(),
temp
.
getValue
());
if
(
architecture
.
getDataPath
()
!=
null
)
{
temp
=
archTc
.
process
(
"CNNDataLoader"
,
Target
.
PYTHON
);
temp
=
controller
.
process
(
"CNNDataLoader"
,
Target
.
PYTHON
);
fileContentMap
.
put
(
temp
.
getKey
(),
temp
.
getValue
());
}
temp
=
archTc
.
process
(
"CNNCreator"
,
Target
.
PYTHON
);
temp
=
controller
.
process
(
"CNNCreator"
,
Target
.
PYTHON
);
fileContentMap
.
put
(
temp
.
getKey
(),
temp
.
getValue
());
return
fileContentMap
;
}
private
Map
<
String
,
String
>
compileCppFiles
(
CNNArch2GluonTemplateController
controller
)
{
Map
<
String
,
String
>
fileContentMap
=
new
HashMap
<>();
Map
.
Entry
<
String
,
String
>
temp
;
temp
=
controller
.
process
(
"CNNPredictor"
,
Target
.
CPP
);
fileContentMap
.
put
(
temp
.
getKey
(),
temp
.
getValue
());
temp
=
archTc
.
process
(
"execute"
,
Target
.
CPP
);
temp
=
controller
.
process
(
"execute"
,
Target
.
CPP
);
fileContentMap
.
put
(
temp
.
getKey
().
replace
(
".h"
,
""
),
temp
.
getValue
());
temp
=
archTc
.
process
(
"CNNBufferFile"
,
Target
.
CPP
);
temp
=
controller
.
process
(
"CNNBufferFile"
,
Target
.
CPP
);
fileContentMap
.
put
(
"CNNBufferFile.h"
,
temp
.
getValue
());
checkValidGeneration
(
architecture
);
return
fileContentMap
;
}
private
Map
<
String
,
String
>
compileFileContentMap
(
ArchitectureSymbol
architecture
)
{
TemplateConfiguration
templateConfiguration
=
new
GluonTemplateConfiguration
();
Map
<
String
,
String
>
fileContentMap
=
new
HashMap
<>();
CNNArch2GluonTemplateController
archTc
=
new
CNNArch2GluonTemplateController
(
architecture
,
templateConfiguration
);
fileContentMap
.
putAll
(
compilePythonFiles
(
archTc
,
architecture
));
fileContentMap
.
putAll
(
compileCppFiles
(
archTc
));
return
fileContentMap
;
}
}
private
Map
<
String
,
String
>
compilePythonFilesOnlyContentMap
(
ArchitectureSymbol
architecture
)
{
TemplateConfiguration
templateConfiguration
=
new
GluonTemplateConfiguration
();
CNNArch2GluonTemplateController
archTc
=
new
CNNArch2GluonTemplateController
(
architecture
,
templateConfiguration
);
return
compilePythonFiles
(
archTc
,
architecture
);
}
}
\ No newline at end of file
src/main/java/de/monticore/lang/monticar/cnnarch/gluongenerator/CNNTrain2Gluon.java
View file @
78559a89
package
de.monticore.lang.monticar.cnnarch.gluongenerator
;
import
com.google.common.collect.Maps
;
import
de.monticore.lang.monticar.cnnarch.gluongenerator.reinforcement.critic.CriticNetworkGenerationPair
;
import
de.monticore.lang.monticar.cnnarch.gluongenerator.reinforcement.critic.CriticNetworkGenerator
;
import
de.monticore.lang.monticar.cnnarch.gluongenerator.reinforcement.FunctionParameterChecker
;
import
de.monticore.lang.monticar.cnnarch.gluongenerator.reinforcement.RewardFunctionParameterAdapter
;
import
de.monticore.lang.monticar.cnnarch.gluongenerator.reinforcement.RewardFunctionSourceGenerator
;
...
...
@@ -10,7 +12,9 @@ import de.monticore.lang.monticar.cnnarch.mxnetgenerator.CNNTrain2MxNet;
import
de.monticore.lang.monticar.cnnarch.mxnetgenerator.TemplateConfiguration
;
import
de.monticore.lang.monticar.cnntrain._symboltable.ConfigurationSymbol
;
import
de.monticore.lang.monticar.cnntrain._symboltable.LearningMethod
;
import
de.monticore.lang.monticar.cnntrain._symboltable.RLAlgorithm
;
import
de.monticore.lang.monticar.cnntrain._symboltable.RewardFunctionSymbol
;
import
de.monticore.lang.monticar.cnntrain.annotations.TrainedArchitecture
;
import
de.monticore.lang.monticar.generator.FileContent
;
import
de.monticore.lang.monticar.generator.cpp.GeneratorCPP
;
import
de.monticore.lang.monticar.generator.pythonwrapper.GeneratorPythonWrapperStandaloneApi
;
...
...
@@ -25,6 +29,7 @@ import java.nio.file.Files;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.util.*
;
import
java.util.stream.Collectors
;
public
class
CNNTrain2Gluon
extends
CNNTrain2MxNet
{
private
static
final
String
REINFORCEMENT_LEARNING_FRAMEWORK_MODULE
=
"reinforcement_learning"
;
...
...
@@ -62,6 +67,15 @@ public class CNNTrain2Gluon extends CNNTrain2MxNet {
public
void
generate
(
Path
modelsDirPath
,
String
rootModelName
)
{
ConfigurationSymbol
configuration
=
this
.
getConfigurationSymbol
(
modelsDirPath
,
rootModelName
);
if
(
configuration
.
getLearningMethod
().
equals
(
LearningMethod
.
REINFORCEMENT
))
{
throw
new
IllegalStateException
(
"Cannot call generate of reinforcement configuration without specifying "
+
"the trained architecture"
);
}
generateFilesFromConfigurationSymbol
(
configuration
);
}
private
void
generateFilesFromConfigurationSymbol
(
ConfigurationSymbol
configuration
)
{
Map
<
String
,
String
>
fileContents
=
this
.
generateStrings
(
configuration
);
GeneratorCPP
genCPP
=
new
GeneratorCPP
();
genCPP
.
setGenerationTargetPath
(
this
.
getGenerationTargetPath
());
...
...
@@ -78,6 +92,13 @@ public class CNNTrain2Gluon extends CNNTrain2MxNet {
}
}
public
void
generate
(
Path
modelsDirPath
,
String
rootModelName
,
TrainedArchitecture
trainedArchitecture
)
{
ConfigurationSymbol
configurationSymbol
=
this
.
getConfigurationSymbol
(
modelsDirPath
,
rootModelName
);
configurationSymbol
.
setTrainedArchitecture
(
trainedArchitecture
);
this
.
setRootProjectModelsDir
(
modelsDirPath
.
toString
());
generateFilesFromConfigurationSymbol
(
configurationSymbol
);
}
@Override
public
Map
<
String
,
String
>
generateStrings
(
ConfigurationSymbol
configuration
)
{
TemplateConfiguration
templateConfiguration
=
new
GluonTemplateConfiguration
();
...
...
@@ -98,8 +119,30 @@ public class CNNTrain2Gluon extends CNNTrain2MxNet {
fileContentMap
.
put
(
"supervised_trainer.py"
,
cnnSupervisedTrainerContent
);
}
else
if
(
configData
.
isReinforcementLearning
())
{
final
String
trainerName
=
"CNNTrainer_"
+
getInstanceName
();
final
RLAlgorithm
rlAlgorithm
=
configData
.
getRlAlgorithm
();
if
(
rlAlgorithm
.
equals
(
RLAlgorithm
.
DDPG
))
{
CriticNetworkGenerator
criticNetworkGenerator
=
new
CriticNetworkGenerator
();
criticNetworkGenerator
.
setGenerationTargetPath
(
Paths
.
get
(
getGenerationTargetPath
(),
REINFORCEMENT_LEARNING_FRAMEWORK_MODULE
).
toString
());
if
(
getRootProjectModelsDir
().
isPresent
())
{
criticNetworkGenerator
.
setRootModelsDir
(
getRootProjectModelsDir
().
get
());
}
else
{
Log
.
error
(
"No root model dir set"
);
}
CriticNetworkGenerationPair
criticNetworkResult
=
criticNetworkGenerator
.
generateCriticNetworkContent
(
templateConfiguration
,
configuration
);
fileContentMap
.
putAll
(
criticNetworkResult
.
getFileContent
().
entrySet
().
stream
().
collect
(
Collectors
.
toMap
(
k
->
REINFORCEMENT_LEARNING_FRAMEWORK_MODULE
+
"/"
+
k
.
getKey
(),
Map
.
Entry
::
getValue
))
);
ftlContext
.
put
(
"criticInstanceName"
,
criticNetworkResult
.
getCriticNetworkName
());
}
ftlContext
.
put
(
"trainerName"
,
trainerName
);
Map
<
String
,
String
>
rlFrameworkContentMap
=
constructReinforcementLearningFramework
(
templateConfiguration
,
ftlContext
);
Map
<
String
,
String
>
rlFrameworkContentMap
=
constructReinforcementLearningFramework
(
templateConfiguration
,
ftlContext
,
rlAlgorithm
);
fileContentMap
.
putAll
(
rlFrameworkContentMap
);
final
String
reinforcementTrainerContent
=
templateConfiguration
.
processTemplate
(
ftlContext
,
"reinforcement/Trainer.ftl"
);
...
...
@@ -161,23 +204,34 @@ public class CNNTrain2Gluon extends CNNTrain2MxNet {
}
private
Map
<
String
,
String
>
constructReinforcementLearningFramework
(
final
TemplateConfiguration
templateConfiguration
,
final
Map
<
String
,
Object
>
ftlContext
)
{
final
TemplateConfiguration
templateConfiguration
,
final
Map
<
String
,
Object
>
ftlContext
,
RLAlgorithm
rlAlgorithm
)
{
Map
<
String
,
String
>
fileContentMap
=
Maps
.
newHashMap
();
ftlContext
.
put
(
"rlFrameworkModule"
,
REINFORCEMENT_LEARNING_FRAMEWORK_MODULE
);
final
String
reinforcementAgentContent
=
templateConfiguration
.
processTemplate
(
ftlContext
,
"reinforcement/agent/Agent.ftl"
);
final
String
loggerContent
=
templateConfiguration
.
processTemplate
(
ftlContext
,
"reinforcement/util/Logger.ftl"
);
fileContentMap
.
put
(
REINFORCEMENT_LEARNING_FRAMEWORK_MODULE
+
"/cnnarch_logger.py"
,
loggerContent
);
final
String
reinforcementAgentContent
=
templateConfiguration
.
processTemplate
(
ftlContext
,
"reinforcement/agent/Agent.ftl"
);
fileContentMap
.
put
(
REINFORCEMENT_LEARNING_FRAMEWORK_MODULE
+
"/agent.py"
,
reinforcementAgentContent
);
final
String
reinforcementPolicyContent
=
templateConfiguration
.
processTemplate
(
ftlContext
,
"reinforcement/agent/ActionPolicy.ftl"
);
fileContentMap
.
put
(
REINFORCEMENT_LEARNING_FRAMEWORK_MODULE
+
"/action_policy.py"
,
reinforcementPolicyContent
);
final
String
reinforcementStrategyContent
=
templateConfiguration
.
processTemplate
(
ftlContext
,
"reinforcement/agent/Strategy.ftl"
);
fileContentMap
.
put
(
REINFORCEMENT_LEARNING_FRAMEWORK_MODULE
+
"/strategy.py"
,
reinforcementStrategyContent
);
final
String
replayMemoryContent
=
templateConfiguration
.
processTemplate
(
ftlContext
,
"reinforcement/agent/ReplayMemory.ftl"
);
final
String
replayMemoryContent
=
templateConfiguration
.
processTemplate
(
ftlContext
,
"reinforcement/agent/ReplayMemory.ftl"
);
fileContentMap
.
put
(
REINFORCEMENT_LEARNING_FRAMEWORK_MODULE
+
"/replay_memory.py"
,
replayMemoryContent
);
final
String
environmentContent
=
templateConfiguration
.
processTemplate
(
ftlContext
,
"reinforcement/environment/Environment.ftl"
);
final
String
environmentContent
=
templateConfiguration
.
processTemplate
(
ftlContext
,
"reinforcement/environment/Environment.ftl"
);
fileContentMap
.
put
(
REINFORCEMENT_LEARNING_FRAMEWORK_MODULE
+
"/environment.py"
,
environmentContent
);
final
String
utilContent
=
templateConfiguration
.
processTemplate
(
ftlContext
,
"reinforcement/util/Util.ftl"
);
final
String
utilContent
=
templateConfiguration
.
processTemplate
(
ftlContext
,
"reinforcement/util/Util.ftl"
);
fileContentMap
.
put
(
REINFORCEMENT_LEARNING_FRAMEWORK_MODULE
+
"/util.py"
,
utilContent
);
final
String
initContent
=
""
;
...
...
src/main/java/de/monticore/lang/monticar/cnnarch/gluongenerator/ReinforcementConfigurationData.java
View file @
78559a89
package
de.monticore.lang.monticar.cnnarch.gluongenerator
;
import
de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol
;
import
de.monticore.lang.monticar.cnnarch.gluongenerator.reinforcement.RewardFunctionParameterAdapter
;
import
de.monticore.lang.monticar.cnnarch.mxnetgenerator.ConfigurationData
;
import
de.monticore.lang.monticar.cnntrain._symboltable.*
;
import
de.monticore.lang.monticar.cnntrain.annotations.Range
;
import
de.monticore.lang.monticar.cnntrain.annotations.TrainedArchitecture
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.*
;
/**
*
...
...
@@ -25,10 +25,19 @@ public class ReinforcementConfigurationData extends ConfigurationData {
private
static
final
String
AST_ENTRY_AGENT_NAME
=
"agent_name"
;
private
static
final
String
AST_ENTRY_USE_DOUBLE_DQN
=
"use_double_dqn"
;
private
static
final
String
AST_ENTRY_LOSS
=
"loss"
;
private
static
final
String
AST_ENTRY_RL_ALGORITHM
=
"rl_algorithm"
;
private
static
final
String
AST_ENTRY_REPLAY_MEMORY
=
"replay_memory"
;
private
static
final
String
AST_ENTRY_
ACTION_SELECTION
=
"action_selection
"
;
private
static
final
String
AST_ENTRY_
STRATEGY
=
"strategy
"
;
private
static
final
String
AST_ENTRY_ENVIRONMENT
=
"environment"
;
private
static
final
String
AST_ENTRY_START_TRAINING_AT
=
"start_training_at"
;
private
static
final
String
AST_SOFT_TARGET_UPDATE_RATE
=
"soft_target_update_rate"
;
private
static
final
String
AST_EVALUATION_SAMPLES
=
"evaluation_samples"
;
private
static
final
String
ENVIRONMENT_PARAM_REWARD_TOPIC
=
"reward_topic"
;
private
static
final
String
ENVIRONMENT_ROS
=
"ros_interface"
;
private
static
final
String
ENVIRONMENT_GYM
=
"gym"
;
private
static
final
String
STRATEGY_ORNSTEIN_UHLENBECK
=
"ornstein_uhlenbeck"
;
public
ReinforcementConfigurationData
(
ConfigurationSymbol
configuration
,
String
instanceName
)
{
super
(
configuration
,
instanceName
);
...
...
@@ -97,6 +106,67 @@ public class ReinforcementConfigurationData extends ConfigurationData {
?
null
:
(
Boolean
)
retrieveConfigurationEntryValueByKey
(
AST_ENTRY_USE_DOUBLE_DQN
);
}
public
Double
getSoftTargetUpdateRate
()
{
return
!
configurationContainsKey
(
AST_SOFT_TARGET_UPDATE_RATE
)
?
null
:
(
Double
)
retrieveConfigurationEntryValueByKey
(
AST_SOFT_TARGET_UPDATE_RATE
);
}
public
Integer
getStartTrainingAt
()
{
return
!
configurationContainsKey
(
AST_ENTRY_START_TRAINING_AT
)
?
null
:
(
Integer
)
retrieveConfigurationEntryValueByKey
(
AST_ENTRY_START_TRAINING_AT
);
}
public
Integer
getEvaluationSamples
()
{
return
!
configurationContainsKey
(
AST_EVALUATION_SAMPLES
)
?
null
:
(
Integer
)
retrieveConfigurationEntryValueByKey
(
AST_EVALUATION_SAMPLES
);
}
public
RLAlgorithm
getRlAlgorithm
()
{
if
(!
isReinforcementLearning
())
{
return
null
;
}
return
!
configurationContainsKey
(
AST_ENTRY_RL_ALGORITHM
)
?
RLAlgorithm
.
DQN
:
(
RLAlgorithm
)
retrieveConfigurationEntryValueByKey
(
AST_ENTRY_RL_ALGORITHM
);
}
public
String
getInputNameOfTrainedArchitecture
()
{
if
(!
this
.
getConfiguration
().
getTrainedArchitecture
().
isPresent
())
{
throw
new
IllegalStateException
(
"No trained architecture set"
);
}
TrainedArchitecture
trainedArchitecture
=
getConfiguration
().
getTrainedArchitecture
().
get
();
// We allow only one input, the first one is the only input
return
trainedArchitecture
.
getInputs
().
get
(
0
);
}
public
String
getOutputNameOfTrainedArchitecture
()
{
if
(!
this
.
getConfiguration
().
getTrainedArchitecture
().
isPresent
())
{
throw
new
IllegalStateException
(
"No trained architecture set"
);
}
TrainedArchitecture
trainedArchitecture
=
getConfiguration
().
getTrainedArchitecture
().
get
();
// We allow only one output, the first one is the only output
return
trainedArchitecture
.
getOutputs
().
get
(
0
);
}
public
List
<
Integer
>
getStateDim
()
{
if
(!
this
.
getConfiguration
().
getTrainedArchitecture
().
isPresent
())
{
return
null
;
}
final
String
inputName
=
getInputNameOfTrainedArchitecture
();
TrainedArchitecture
trainedArchitecture
=
this
.
getConfiguration
().
getTrainedArchitecture
().
get
();
return
trainedArchitecture
.
getDimensions
().
get
(
inputName
);
}
public
List
<
Integer
>
getActionDim
()
{
if
(!
this
.
getConfiguration
().
getTrainedArchitecture
().
isPresent
())
{
return
null
;
}
final
String
outputName
=
getOutputNameOfTrainedArchitecture
();
TrainedArchitecture
trainedArchitecture
=
this
.
getConfiguration
().
getTrainedArchitecture
().
get
();
return
trainedArchitecture
.
getDimensions
().
get
(
outputName
);
}
public
String
getLoss
()
{
return
!
configurationContainsKey
(
AST_ENTRY_LOSS
)
?
null
:
retrieveConfigurationEntryValueByKey
(
AST_ENTRY_LOSS
).
toString
();
...
...
@@ -106,8 +176,36 @@ public class ReinforcementConfigurationData extends ConfigurationData {
return
getMultiParamEntry
(
AST_ENTRY_REPLAY_MEMORY
,
"method"
);
}
public
Map
<
String
,
Object
>
getActionSelection
()
{
return
getMultiParamEntry
(
AST_ENTRY_ACTION_SELECTION
,
"method"
);
public
Map
<
String
,
Object
>
getStrategy
()
{
assert
isReinforcementLearning
():
"Strategy parameter only for reinforcement learning but called in a "
+
" non reinforcement learning context"
;
Map
<
String
,
Object
>
strategyParams
=
getMultiParamEntry
(
AST_ENTRY_STRATEGY
,
"method"
);
if
(
strategyParams
.
get
(
"method"
).
equals
(
STRATEGY_ORNSTEIN_UHLENBECK
))
{
assert
getConfiguration
().
getTrainedArchitecture
().
isPresent
():
"Architecture not present,"
+
" but reinforcement training"
;
TrainedArchitecture
trainedArchitecture
=
getConfiguration
().
getTrainedArchitecture
().
get
();
final
String
actionPortName
=
getOutputNameOfTrainedArchitecture
();
Range
actionRange
=
trainedArchitecture
.
getRanges
().
get
(
actionPortName
);
if
(
actionRange
.
isLowerLimitInfinity
()
&&
actionRange
.
isUpperLimitInfinity
())
{
strategyParams
.
put
(
"action_low"
,
null
);
strategyParams
.
put
(
"action_high"
,
null
);
}
else
if
(!
actionRange
.
isLowerLimitInfinity
()
&&
actionRange
.
isUpperLimitInfinity
())
{
assert
actionRange
.
getLowerLimit
().
isPresent
();
strategyParams
.
put
(
"action_low"
,
actionRange
.
getLowerLimit
().
get
());
strategyParams
.
put
(
"action_high"
,
null
);
}
else
if
(
actionRange
.
isLowerLimitInfinity
()
&&
!
actionRange
.
isUpperLimitInfinity
())
{
assert
actionRange
.
getUpperLimit
().
isPresent
();
strategyParams
.
put
(
"action_low"
,
null
);
strategyParams
.
put
(
"action_high"
,
actionRange
.
getUpperLimit
().
get
());
}
else
{
assert
actionRange
.
getLowerLimit
().
isPresent
();
assert
actionRange
.
getUpperLimit
().
isPresent
();
strategyParams
.
put
(
"action_low"
,
actionRange
.
getLowerLimit
().
get
());
strategyParams
.
put
(
"action_high"
,
actionRange
.
getUpperLimit
().
get
());
}
}
return
strategyParams
;
}
public
Map
<
String
,
Object
>
getEnvironment
()
{
...
...
@@ -136,6 +234,16 @@ public class ReinforcementConfigurationData extends ConfigurationData {
.
getRewardFunctionParameter
().
orElse
(
null
));
}
public
boolean
isDiscreteRlAlgorithm
()
{
assert
isReinforcementLearning
();
return
getRlAlgorithm
().
equals
(
RLAlgorithm
.
DQN
);
}
public
boolean
isContinuousRlAlgorithm
()
{
assert
isReinforcementLearning
();
return
getRlAlgorithm
().
equals
(
RLAlgorithm
.
DDPG
);
}
public
Map
<
String
,
Object
>
getRewardFunctionStateParameter
()
{
if
(!
getRlRewardFunctionParameter
().
isPresent
()
||
!
getRlRewardFunctionParameter
().
get
().
getInputStateParameterName
().
isPresent
())
{
...
...
@@ -159,6 +267,50 @@ public class ReinforcementConfigurationData extends ConfigurationData {
return
getRlRewardFunctionParameter
().
get
().
getOutputParameterName
().
orElse
(
null
);
}
public
String
getCriticOptimizerName
()
{
if
(!
getConfiguration
().
getCriticOptimizer
().
isPresent
())
{
return
null
;
}
return
getConfiguration
().
getCriticOptimizer
().
get
().
getName
();
}
public
Map
<
String
,
String
>
getCriticOptimizerParams
()
{
// get classes for single enum values
assert
getConfiguration
().
getCriticOptimizer
().
isPresent
():
"Critic optimizer params called although, not present"
;
List
<
Class
>
lrPolicyClasses
=
new
ArrayList
<>();
for
(
LRPolicy
enum_value:
LRPolicy
.
values
())
{
lrPolicyClasses
.
add
(
enum_value
.
getClass
());
}
Map
<
String
,
String
>
mapToStrings
=
new
HashMap
<>();
Map
<
String
,
OptimizerParamSymbol
>
optimizerParams
=
getConfiguration
().
getCriticOptimizer
().
get
().
getOptimizerParamMap
();
for
(
Map
.
Entry
<
String
,
OptimizerParamSymbol
>
entry
:
optimizerParams
.
entrySet
())
{
String
paramName
=
entry
.
getKey
();
String
valueAsString
=
entry
.
getValue
().
toString
();
Class
realClass
=
entry
.
getValue
().
getValue
().
getValue
().
getClass
();
if
(
realClass
==
Boolean
.
class
)
{
valueAsString
=
(
Boolean
)
entry
.
getValue
().
getValue
().
getValue
()
?
"True"
:
"False"
;
}
else
if
(
lrPolicyClasses
.
contains
(
realClass
))
{
valueAsString
=
"'"
+
valueAsString
+
"'"
;
}
mapToStrings
.
put
(
paramName
,
valueAsString
);
}
return
mapToStrings
;
}
public
boolean
hasRosRewardTopic
()
{
Map
<
String
,
Object
>
environmentParameters
=
getMultiParamEntry
(
AST_ENTRY_ENVIRONMENT
,
"environment"
);
if
(
environmentParameters
==
null
||
!
environmentParameters
.
containsKey
(
"environment"
))
{
return
false
;
}
return
environmentParameters
.
containsKey
(
ENVIRONMENT_PARAM_REWARD_TOPIC
);
}
private
Map
<
String
,
Object
>
getMultiParamEntry
(
final
String
key
,
final
String
valueName
)
{
if
(!
configurationContainsKey
(
key
))
{
return
null
;
...
...
src/main/java/de/monticore/lang/monticar/cnnarch/gluongenerator/annotations/ArchitectureAdapter.java
0 → 100644
View file @
78559a89
package
de.monticore.lang.monticar.cnnarch.gluongenerator.annotations
;
import
de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol
;
import
de.monticore.lang.monticar.cnnarch._symboltable.IOSymbol
;
import
de.monticore.lang.monticar.cnntrain.annotations.Range
;
import
de.monticore.lang.monticar.cnntrain.annotations.TrainedArchitecture
;
import
de.monticore.lang.monticar.ranges._ast.ASTRange
;
import
de.monticore.symboltable.CommonSymbol
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
import
static
com
.
google
.
common
.
base
.
Preconditions
.
checkNotNull
;
public
class
ArchitectureAdapter
implements
TrainedArchitecture
{
private
ArchitectureSymbol
architectureSymbol
;
public
ArchitectureAdapter
(
final
ArchitectureSymbol
architectureSymbol
)
{
checkNotNull
(
architectureSymbol
);
this
.
architectureSymbol
=
architectureSymbol
;
}
@Override
public
List
<
String
>
getInputs
()
{
return
getIOInputSymbols
().
stream
()
.
map
(
CommonSymbol:
:
getName
)
.
collect
(
Collectors
.
toList
());
}
@Override
public
List
<
String
>
getOutputs
()
{
return
getIOOutputSymbols
().
stream
()
.
map
(
CommonSymbol:
:
getName
)
.
collect
(
Collectors
.
toList
());
}
@Override
public
Map
<
String
,
List
<
Integer
>>
getDimensions
()
{
return
getAllIOSymbols
().
stream
().
collect
(
Collectors
.
toMap
(
CommonSymbol:
:
getName
,
s
->
s
.
getDefinition
().
getType
().
getDimensions
()));
}
@Override
public
Map
<
String
,
Range
>
getRanges
()
{
return
getAllIOSymbols
().
stream
().
collect
(
Collectors
.
toMap
(
CommonSymbol:
:
getName
,
s
->
astRangeToTrainRange
(
s
.
getDefinition
().
getType
().
getDomain
().
getRangeOpt
().
orElse
(
null
))));