Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
libdrt
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
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Klemm, Carl Philipp
libdrt
Commits
a13bbd25
Commit
a13bbd25
authored
2 years ago
by
Carl Philipp Klemm
Browse files
Options
Downloads
Patches
Plain Diff
add bounds to optimization
parent
581cb391
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
CMakeLists.txt
+12
-2
12 additions, 2 deletions
CMakeLists.txt
drt.cpp
+23
-10
23 additions, 10 deletions
drt.cpp
eigentorchconversions.h
+2
-0
2 additions, 0 deletions
eigentorchconversions.h
eisdrt/drt.h
+0
-0
0 additions, 0 deletions
eisdrt/drt.h
main.cpp
+11
-12
11 additions, 12 deletions
main.cpp
with
48 additions
and
24 deletions
CMakeLists.txt
+
12
−
2
View file @
a13bbd25
cmake_minimum_required
(
VERSION 3.19
)
cmake_minimum_required
(
VERSION 3.19
)
project
(
drt
wrap
LANGUAGES CXX
)
project
(
eis
drt LANGUAGES CXX
)
link_directories
(
${
CMAKE_CURRENT_BINARY_DIR
}
)
link_directories
(
${
CMAKE_CURRENT_BINARY_DIR
}
)
...
@@ -17,10 +17,15 @@ function(dump_variables)
...
@@ -17,10 +17,15 @@ function(dump_variables)
endforeach
()
endforeach
()
endfunction
()
endfunction
()
if
(
CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT
)
set
(
CMAKE_INSTALL_PREFIX
"/usr"
CACHE PATH
"..."
FORCE
)
endif
(
CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT
)
add_library
(
${
PROJECT_NAME
}
SHARED eistotorch.cpp drt.cpp
)
add_library
(
${
PROJECT_NAME
}
SHARED eistotorch.cpp drt.cpp
)
target_link_libraries
(
${
PROJECT_NAME
}
${
TORCH_LIBRARIES
}
${
EIGEN3_LIBRARIES
}
eisgenerator
)
target_link_libraries
(
${
PROJECT_NAME
}
${
TORCH_LIBRARIES
}
${
EIGEN3_LIBRARIES
}
eisgenerator
)
target_include_directories
(
${
PROJECT_NAME
}
PUBLIC
${
TORCH_INCLUDE_DIRS
}
${
EIGEN3_INCLUDE_DIRS
}
./LBFG
)
target_include_directories
(
${
PROJECT_NAME
}
PUBLIC
${
TORCH_INCLUDE_DIRS
}
${
EIGEN3_INCLUDE_DIRS
}
./LBFG
)
set_target_properties
(
${
PROJECT_NAME
}
PROPERTIES COMPILE_FLAGS
"-Wall -O2 -march=native -g"
LINK_FLAGS
"-flto"
)
set_target_properties
(
${
PROJECT_NAME
}
PROPERTIES COMPILE_FLAGS
"-Wall -O2 -march=native -g"
LINK_FLAGS
"-flto"
)
install
(
TARGETS
${
PROJECT_NAME
}
DESTINATION lib
)
link_directories
(
${
CMAKE_CURRENT_BINARY_DIR
}
)
link_directories
(
${
CMAKE_CURRENT_BINARY_DIR
}
)
add_executable
(
${
PROJECT_NAME
}
_test main.cpp
)
add_executable
(
${
PROJECT_NAME
}
_test main.cpp
)
...
@@ -28,5 +33,10 @@ add_dependencies(${PROJECT_NAME}_test ${PROJECT_NAME})
...
@@ -28,5 +33,10 @@ add_dependencies(${PROJECT_NAME}_test ${PROJECT_NAME})
target_link_libraries
(
${
PROJECT_NAME
}
_test -l
${
PROJECT_NAME
}
${
TORCH_LIBRARIES
}
eisgenerator
)
target_link_libraries
(
${
PROJECT_NAME
}
_test -l
${
PROJECT_NAME
}
${
TORCH_LIBRARIES
}
eisgenerator
)
target_include_directories
(
${
PROJECT_NAME
}
_test PRIVATE .
${
TORCH_INCLUDE_DIRS
}
)
target_include_directories
(
${
PROJECT_NAME
}
_test PRIVATE .
${
TORCH_INCLUDE_DIRS
}
)
set_target_properties
(
${
PROJECT_NAME
}
PROPERTIES COMPILE_FLAGS
"-Wall -O2 -march=native -g"
LINK_FLAGS
"-flto"
)
set_target_properties
(
${
PROJECT_NAME
}
PROPERTIES COMPILE_FLAGS
"-Wall -O2 -march=native -g"
LINK_FLAGS
"-flto"
)
install
(
TARGETS
${
PROJECT_NAME
}
RUNTIME DESTINATION bin
)
install
(
TARGETS
${
PROJECT_NAME
}
RUNTIME DESTINATION bin
)
set
(
API_HEADERS_DIR drt/
)
set
(
API_HEADERS
${
API_HEADERS_DIR
}
/drt.h
)
install
(
FILES
${
API_HEADERS
}
DESTINATION include/
${
PROJECT_NAME
}
)
This diff is collapsed.
Click to expand it.
drt.cpp
+
23
−
10
View file @
a13bbd25
#include
"drt/drt.h"
#include
"
eis
drt/drt.h"
#include
<ATen/ops/ones.h>
#include
<ATen/ops/zeros.h>
#include
<Eigen/Core>
#include
<Eigen/Core>
#include
<eisgenerator/eistype.h>
#include
<eisgenerator/eistype.h>
#include
"tensoroptions.h"
#include
"tensoroptions.h"
#include
"eigentorchconversions.h"
#include
"eigentorchconversions.h"
#include
"eistotorch.h"
#include
"eistotorch.h"
#include
"LBFG/LBFGS.h"
#include
"LBFG/LBFGS
B
.h"
static
torch
::
Tensor
guesStartingPoint
(
torch
::
Tensor
&
omega
,
torch
::
Tensor
&
impedanceSpectra
)
static
torch
::
Tensor
guesStartingPoint
(
torch
::
Tensor
&
omega
,
torch
::
Tensor
&
impedanceSpectra
)
{
{
...
@@ -85,15 +87,13 @@ public:
...
@@ -85,15 +87,13 @@ public:
fvalue
function
(
const
torch
::
Tensor
&
x
)
fvalue
function
(
const
torch
::
Tensor
&
x
)
{
{
assert
(
checkTorchType
<
fvalue
>
(
x
));
assert
(
x
.
sizes
().
size
()
==
1
);
auto
xAccessor
=
x
.
accessor
<
fvalue
,
1
>
();
auto
xAccessor
=
x
.
accessor
<
fvalue
,
1
>
();
int64_t
size
=
x
.
numel
();
int64_t
size
=
x
.
numel
();
torch
::
Tensor
xLeft
=
x
.
narrow
(
0
,
0
,
x
.
numel
()
-
1
);
torch
::
Tensor
xLeft
=
x
.
narrow
(
0
,
0
,
x
.
numel
()
-
1
);
torch
::
Tensor
MSE_re
=
torch
::
sum
(
torch
::
pow
(
xAccessor
[
size
-
1
]
+
torch
::
matmul
(
aMatrixReal
,
xLeft
)
-
torch
::
real
(
impedanceSpectra
),
2
));
torch
::
Tensor
MSE_re
=
torch
::
sum
(
torch
::
pow
(
xAccessor
[
size
-
1
]
+
torch
::
matmul
(
aMatrixReal
,
xLeft
)
-
torch
::
real
(
impedanceSpectra
),
2
)
,
torch
::
typeMetaToScalarType
(
x
.
dtype
())
);
torch
::
Tensor
MSE_im
=
torch
::
sum
(
torch
::
pow
(
torch
::
matmul
(
aMatrixImag
,
xLeft
)
-
torch
::
imag
(
impedanceSpectra
),
2
));
torch
::
Tensor
MSE_im
=
torch
::
sum
(
torch
::
pow
(
torch
::
matmul
(
aMatrixImag
,
xLeft
)
-
torch
::
imag
(
impedanceSpectra
),
2
)
,
torch
::
typeMetaToScalarType
(
x
.
dtype
())
);
torch
::
Tensor
reg_term
=
el
/
2
*
torch
::
sum
(
torch
::
pow
(
xLeft
,
2
));
torch
::
Tensor
reg_term
=
el
/
2
*
torch
::
sum
(
torch
::
pow
(
xLeft
,
2
)
,
torch
::
typeMetaToScalarType
(
x
.
dtype
())
);
torch
::
Tensor
obj
=
MSE_re
+
MSE_im
+
reg_term
;
torch
::
Tensor
obj
=
MSE_re
+
MSE_im
+
reg_term
;
return
obj
.
item
().
to
<
fvalue
>
();
return
obj
.
item
().
to
<
fvalue
>
();
}
}
...
@@ -127,23 +127,36 @@ public:
...
@@ -127,23 +127,36 @@ public:
}
}
};
};
static
torch
::
Tensor
calcBounds
(
torch
::
Tensor
&
impedanceSpectra
,
torch
::
Tensor
startTensor
)
{
torch
::
Tensor
lowerBounds
=
torch
::
zeros
({
1
,
startTensor
.
numel
()},
tensorOptCpu
<
fvalue
>
());
torch
::
Tensor
upperBounds
=
torch
::
ones
({
1
,
startTensor
.
numel
()},
tensorOptCpu
<
fvalue
>
())
*
torch
::
max
(
torch
::
abs
(
impedanceSpectra
));
return
torch
::
cat
({
lowerBounds
,
upperBounds
},
0
);
}
torch
::
Tensor
calcDrt
(
torch
::
Tensor
&
impedanceSpectra
,
torch
::
Tensor
&
omegaTensor
,
FitMetics
&
fm
,
const
FitParameters
&
fp
)
torch
::
Tensor
calcDrt
(
torch
::
Tensor
&
impedanceSpectra
,
torch
::
Tensor
&
omegaTensor
,
FitMetics
&
fm
,
const
FitParameters
&
fp
)
{
{
torch
::
Tensor
aMatrixImag
=
aImag
(
omegaTensor
);
torch
::
Tensor
aMatrixImag
=
aImag
(
omegaTensor
);
torch
::
Tensor
aMatrixReal
=
aReal
(
omegaTensor
);
torch
::
Tensor
aMatrixReal
=
aReal
(
omegaTensor
);
LBFGSpp
::
LBFGSParam
<
fvalue
>
fitParam
;
LBFGSpp
::
LBFGS
B
Param
<
fvalue
>
fitParam
;
fitParam
.
epsilon
=
fp
.
epsilon
;
fitParam
.
epsilon
=
fp
.
epsilon
;
fitParam
.
max_iterations
=
fp
.
maxIter
;
fitParam
.
max_iterations
=
fp
.
maxIter
;
fitParam
.
max_linesearch
=
fp
.
maxIter
*
10
;
fitParam
.
max_linesearch
=
fp
.
maxIter
*
10
;
LBFGSpp
::
LBFGSSolver
<
fvalue
>
solver
(
fitParam
);
LBFGSpp
::
LBFGS
B
Solver
<
fvalue
>
solver
(
fitParam
);
RtFunct
funct
(
impedanceSpectra
,
aMatrixImag
,
aMatrixReal
,
0.01
,
fp
.
step
);
RtFunct
funct
(
impedanceSpectra
,
aMatrixImag
,
aMatrixReal
,
0.01
,
fp
.
step
);
torch
::
Tensor
startTensor
=
guesStartingPoint
(
omegaTensor
,
impedanceSpectra
);
torch
::
Tensor
startTensor
=
guesStartingPoint
(
omegaTensor
,
impedanceSpectra
);
torch
::
Tensor
bounds
=
calcBounds
(
impedanceSpectra
,
startTensor
);
torch
::
Tensor
lowerBoundTensor
=
bounds
.
select
(
0
,
0
);
torch
::
Tensor
upperBoundTensor
=
bounds
.
select
(
0
,
1
);
Eigen
::
VectorX
<
fvalue
>
lowerbound
=
libtorch2eigenVector
<
fvalue
>
(
lowerBoundTensor
);
Eigen
::
VectorX
<
fvalue
>
upperbound
=
libtorch2eigenVector
<
fvalue
>
(
upperBoundTensor
);
Eigen
::
VectorX
<
fvalue
>
x
=
libtorch2eigenVector
<
fvalue
>
(
startTensor
);
Eigen
::
VectorX
<
fvalue
>
x
=
libtorch2eigenVector
<
fvalue
>
(
startTensor
);
fm
.
iterations
=
solver
.
minimize
(
funct
,
x
,
fm
.
fx
);
fm
.
iterations
=
solver
.
minimize
(
funct
,
x
,
fm
.
fx
,
lowerbound
,
upperbound
);
torch
::
Tensor
xT
=
eigenVector2libtorch
<
fvalue
>
(
x
);
torch
::
Tensor
xT
=
eigenVector2libtorch
<
fvalue
>
(
x
);
return
xT
;
return
xT
;
...
...
This diff is collapsed.
Click to expand it.
eigentorchconversions.h
+
2
−
0
View file @
a13bbd25
...
@@ -67,6 +67,7 @@ Eigen::Matrix<V, Eigen::Dynamic, Eigen::Dynamic> libtorch2eigenMaxtrix(torch::Te
...
@@ -67,6 +67,7 @@ Eigen::Matrix<V, Eigen::Dynamic, Eigen::Dynamic> libtorch2eigenMaxtrix(torch::Te
MatrixXrm uses Eigen::RowMajor for compatibility.
MatrixXrm uses Eigen::RowMajor for compatibility.
*/
*/
assert
(
checkTorchType
<
V
>
(
Tin
));
assert
(
checkTorchType
<
V
>
(
Tin
));
Tin
=
Tin
.
contiguous
();
auto
T
=
Tin
.
to
(
torch
::
kCPU
);
auto
T
=
Tin
.
to
(
torch
::
kCPU
);
Eigen
::
Map
<
MatrixXrm
<
V
>>
E
(
T
.
data_ptr
<
V
>
(),
T
.
size
(
0
),
T
.
size
(
1
));
Eigen
::
Map
<
MatrixXrm
<
V
>>
E
(
T
.
data_ptr
<
V
>
(),
T
.
size
(
0
),
T
.
size
(
1
));
return
E
;
return
E
;
...
@@ -77,6 +78,7 @@ Eigen::Vector<V, Eigen::Dynamic> libtorch2eigenVector(torch::Tensor &Tin)
...
@@ -77,6 +78,7 @@ Eigen::Vector<V, Eigen::Dynamic> libtorch2eigenVector(torch::Tensor &Tin)
{
{
assert
(
Tin
.
sizes
().
size
()
==
1
);
assert
(
Tin
.
sizes
().
size
()
==
1
);
assert
(
checkTorchType
<
V
>
(
Tin
));
assert
(
checkTorchType
<
V
>
(
Tin
));
Tin
=
Tin
.
contiguous
();
auto
T
=
Tin
.
to
(
torch
::
kCPU
);
auto
T
=
Tin
.
to
(
torch
::
kCPU
);
Eigen
::
Map
<
Eigen
::
Vector
<
V
,
Eigen
::
Dynamic
>>
E
(
T
.
data_ptr
<
V
>
(),
T
.
numel
());
Eigen
::
Map
<
Eigen
::
Vector
<
V
,
Eigen
::
Dynamic
>>
E
(
T
.
data_ptr
<
V
>
(),
T
.
numel
());
return
E
;
return
E
;
...
...
This diff is collapsed.
Click to expand it.
drt/drt.h
→
eis
drt/drt.h
+
0
−
0
View file @
a13bbd25
File moved
This diff is collapsed.
Click to expand it.
main.cpp
+
11
−
12
View file @
a13bbd25
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
#include
<eisgenerator/model.h>
#include
<eisgenerator/model.h>
#include
<eisgenerator/eistype.h>
#include
<eisgenerator/eistype.h>
#include
"drt/drt.h"
#include
"
eis
drt/drt.h"
void
printImpedance
(
const
std
::
vector
<
eis
::
DataPoint
>&
data
)
void
printImpedance
(
const
std
::
vector
<
eis
::
DataPoint
>&
data
)
{
{
...
@@ -25,13 +25,12 @@ int main(int argc, char** argv)
...
@@ -25,13 +25,12 @@ int main(int argc, char** argv)
{
{
std
::
cout
<<
std
::
scientific
;
std
::
cout
<<
std
::
scientific
;
for
(
size_t
i
=
0
;
i
<
10
;
++
i
)
eis
::
Range
omega
(
1
,
1e6
,
25
,
true
);
{
eis
::
Range
omega
(
1
,
1e6
,
50
,
true
);
std
::
vector
<
fvalue
>
omegaVector
=
omega
.
getRangeVector
();
std
::
vector
<
fvalue
>
omegaVector
=
omega
.
getRangeVector
();
eis
::
Model
model
(
"r{10}-r{50}p{0.02, 0.8}"
);
eis
::
Model
model
(
"r{10}-r{50}p{0.02, 0.8}"
);
for
(
size_t
i
=
0
;
i
<
2
;
++
i
)
{
std
::
vector
<
eis
::
DataPoint
>
data
=
model
.
executeSweep
(
omega
);
std
::
vector
<
eis
::
DataPoint
>
data
=
model
.
executeSweep
(
omega
);
printImpedance
(
data
);
printImpedance
(
data
);
...
...
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