Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
macaon
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
Franck Dary
macaon
Commits
9f9a9f9d
Commit
9f9a9f9d
authored
5 years ago
by
Franck Dary
Browse files
Options
Downloads
Patches
Plain Diff
Added classifier RTLSTM
parent
f5a30e71
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
reading_machine/src/Classifier.cpp
+9
-0
9 additions, 0 deletions
reading_machine/src/Classifier.cpp
torch_modules/include/RTLSTMNetwork.hpp
+22
-0
22 additions, 0 deletions
torch_modules/include/RTLSTMNetwork.hpp
torch_modules/src/RTLSTMNetwork.cpp
+32
-0
32 additions, 0 deletions
torch_modules/src/RTLSTMNetwork.cpp
with
63 additions
and
0 deletions
reading_machine/src/Classifier.cpp
+
9
−
0
View file @
9f9a9f9d
...
...
@@ -2,6 +2,7 @@
#include
"util.hpp"
#include
"OneWordNetwork.hpp"
#include
"ConcatWordsNetwork.hpp"
#include
"RTLSTMNetwork.hpp"
Classifier
::
Classifier
(
const
std
::
string
&
name
,
const
std
::
string
&
topology
,
const
std
::
string
&
tsFile
)
{
...
...
@@ -45,6 +46,14 @@ void Classifier::initNeuralNetwork(const std::string & topology)
this
->
nn
.
reset
(
new
ConcatWordsNetworkImpl
(
this
->
transitionSet
->
size
(),
std
::
stoi
(
sm
[
1
]),
std
::
stoi
(
sm
[
2
]),
std
::
stoi
(
sm
[
3
])));
}
},
{
std
::
regex
(
"RTLSTM
\\
(([+
\\
-]?
\\
d+),([+
\\
-]?
\\
d+),([+
\\
-]?
\\
d+)
\\
)"
),
"RTLSTM(leftBorder,rightBorder,nbStack) : Recursive tree LSTM."
,
[
this
,
topology
](
auto
sm
)
{
this
->
nn
.
reset
(
new
RTLSTMNetworkImpl
(
this
->
transitionSet
->
size
(),
std
::
stoi
(
sm
[
1
]),
std
::
stoi
(
sm
[
2
]),
std
::
stoi
(
sm
[
3
])));
}
},
};
for
(
auto
&
initializer
:
initializers
)
...
...
This diff is collapsed.
Click to expand it.
torch_modules/include/RTLSTMNetwork.hpp
0 → 100644
+
22
−
0
View file @
9f9a9f9d
#ifndef RTLSTMNETWORK__H
#define RTLSTMNETWORK__H
#include
"NeuralNetwork.hpp"
class
RTLSTMNetworkImpl
:
public
NeuralNetworkImpl
{
private
:
torch
::
nn
::
Embedding
wordEmbeddings
{
nullptr
};
torch
::
nn
::
Linear
linear1
{
nullptr
};
torch
::
nn
::
Linear
linear2
{
nullptr
};
torch
::
nn
::
Dropout
dropout
{
nullptr
};
torch
::
nn
::
LSTM
lstm
{
nullptr
};
public
:
RTLSTMNetworkImpl
(
int
nbOutputs
,
int
leftBorder
,
int
rightBorder
,
int
nbStackElements
);
torch
::
Tensor
forward
(
torch
::
Tensor
input
)
override
;
};
#endif
This diff is collapsed.
Click to expand it.
torch_modules/src/RTLSTMNetwork.cpp
0 → 100644
+
32
−
0
View file @
9f9a9f9d
#include
"RTLSTMNetwork.hpp"
RTLSTMNetworkImpl
::
RTLSTMNetworkImpl
(
int
nbOutputs
,
int
leftBorder
,
int
rightBorder
,
int
nbStackElements
)
{
constexpr
int
embeddingsSize
=
100
;
constexpr
int
lstmOutputSize
=
500
;
constexpr
int
hiddenSize
=
500
;
setLeftBorder
(
leftBorder
);
setRightBorder
(
rightBorder
);
setNbStackElements
(
nbStackElements
);
wordEmbeddings
=
register_module
(
"word_embeddings"
,
torch
::
nn
::
Embedding
(
torch
::
nn
::
EmbeddingOptions
(
50000
,
embeddingsSize
)));
linear1
=
register_module
(
"linear1"
,
torch
::
nn
::
Linear
(
lstmOutputSize
,
hiddenSize
));
linear2
=
register_module
(
"linear2"
,
torch
::
nn
::
Linear
(
hiddenSize
,
nbOutputs
));
dropout
=
register_module
(
"dropout"
,
torch
::
nn
::
Dropout
(
0.3
));
lstm
=
register_module
(
"lstm"
,
torch
::
nn
::
LSTM
(
torch
::
nn
::
LSTMOptions
(
embeddingsSize
,
lstmOutputSize
).
batch_first
(
true
)));
}
torch
::
Tensor
RTLSTMNetworkImpl
::
forward
(
torch
::
Tensor
input
)
{
// input dim = {batch, sequence, embeddings}
auto
wordsAsEmb
=
wordEmbeddings
(
input
);
if
(
wordsAsEmb
.
dim
()
==
2
)
wordsAsEmb
=
torch
::
unsqueeze
(
wordsAsEmb
,
0
);
auto
lstmOut
=
lstm
(
wordsAsEmb
).
output
;
// reshaped dim = {sequence, batch, embeddings}
auto
reshaped
=
lstmOut
.
permute
({
1
,
0
,
2
});
auto
res
=
linear2
(
torch
::
relu
(
linear1
(
reshaped
[
-
1
])));
return
res
;
}
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