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
ce420cb5
Commit
ce420cb5
authored
5 years ago
by
Franck Dary
Browse files
Options
Downloads
Patches
Plain Diff
Added function to get index of a transition
parent
d3ecc26c
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
dev/src/dev.cpp
+4
-67
4 additions, 67 deletions
dev/src/dev.cpp
reading_machine/include/TransitionSet.hpp
+1
-0
1 addition, 0 deletions
reading_machine/include/TransitionSet.hpp
reading_machine/src/TransitionSet.cpp
+8
-0
8 additions, 0 deletions
reading_machine/src/TransitionSet.cpp
with
13 additions
and
67 deletions
dev/src/dev.cpp
+
4
−
67
View file @
ce420cb5
...
@@ -8,69 +8,6 @@
...
@@ -8,69 +8,6 @@
#include
"TestNetwork.hpp"
#include
"TestNetwork.hpp"
#include
"ConfigDataset.hpp"
#include
"ConfigDataset.hpp"
//constexpr int batchSize = 50;
//constexpr int nbExamples = 350000;
//constexpr int embeddingSize = 20;
//constexpr int nbClasses = 15;
//constexpr int nbWordsPerDatapoint = 5;
//constexpr int maxNbEmbeddings = 1000000;
//
//struct NetworkImpl : torch::nn::Module
//{
// torch::nn::Linear linear{nullptr};
// torch::nn::Embedding wordEmbeddings{nullptr};
//
// std::vector<torch::Tensor> _sparseParameters;
// std::vector<torch::Tensor> _denseParameters;
// NetworkImpl()
// {
// linear = register_module("dense_linear", torch::nn::Linear(embeddingSize, nbClasses));
// auto params = linear->parameters();
// _denseParameters.insert(_denseParameters.end(), params.begin(), params.end());
//
// wordEmbeddings = register_module("sparse_word_embeddings", torch::nn::Embedding(torch::nn::EmbeddingOptions(maxNbEmbeddings, embeddingSize).sparse(true)));
// params = wordEmbeddings->parameters();
// _sparseParameters.insert(_sparseParameters.end(), params.begin(), params.end());
// };
// const std::vector<torch::Tensor> & denseParameters()
// {
// return _denseParameters;
// }
// const std::vector<torch::Tensor> & sparseParameters()
// {
// return _sparseParameters;
// }
// torch::Tensor forward(const torch::Tensor & input)
// {
// // I have a batch of sentences (list of word embeddings), so as the sentence embedding I take the mean of the embedding of its words
// auto embeddingsOfInput = wordEmbeddings(input).mean(1);
// return torch::softmax(linear(embeddingsOfInput),1);
// }
//};
//TORCH_MODULE(Network);
//int main(int argc, char * argv[])
//{
// auto nn = Network();
// torch::optim::SparseAdam sparseOptimizer(nn->sparseParameters(), torch::optim::SparseAdamOptions(2e-4).beta1(0.5));
// torch::optim::Adam denseOptimizer(nn->denseParameters(), torch::optim::AdamOptions(2e-4).beta1(0.5));
// std::vector<std::pair<torch::Tensor,torch::Tensor>> batches;
// for (int nbBatch = 0; nbBatch < nbExamples / batchSize; ++nbBatch)
// batches.emplace_back(std::make_pair(torch::randint(maxNbEmbeddings,{batchSize,nbWordsPerDatapoint}, at::kLong), torch::randint(nbClasses, batchSize, at::kLong)));
//
// for (auto & batch : batches)
// {
// sparseOptimizer.zero_grad();
// denseOptimizer.zero_grad();
// auto prediction = nn(batch.first);
// auto loss = torch::nll_loss(torch::log(prediction), batch.second);
// loss.backward();
// sparseOptimizer.step();
// denseOptimizer.step();
// }
// return 0;
//}
int
main
(
int
argc
,
char
*
argv
[])
int
main
(
int
argc
,
char
*
argv
[])
{
{
if
(
argc
!=
5
)
if
(
argc
!=
5
)
...
@@ -110,7 +47,7 @@ int main(int argc, char * argv[])
...
@@ -110,7 +47,7 @@ int main(int argc, char * argv[])
auto
context
=
config
.
extractContext
(
5
,
5
,
dict
);
auto
context
=
config
.
extractContext
(
5
,
5
,
dict
);
contexts
.
push_back
(
torch
::
from_blob
(
context
.
data
(),
{(
long
)
context
.
size
()},
at
::
kLong
).
clone
());
contexts
.
push_back
(
torch
::
from_blob
(
context
.
data
(),
{(
long
)
context
.
size
()},
at
::
kLong
).
clone
());
int
goldIndex
=
3
;
int
goldIndex
=
machine
.
getTransitionSet
().
getTransitionIndex
(
transition
)
;
auto
gold
=
torch
::
zeros
(
1
,
at
::
kLong
);
auto
gold
=
torch
::
zeros
(
1
,
at
::
kLong
);
gold
[
0
]
=
goldIndex
;
gold
[
0
]
=
goldIndex
;
...
@@ -140,10 +77,10 @@ int main(int argc, char * argv[])
...
@@ -140,10 +77,10 @@ int main(int argc, char * argv[])
auto
dataLoader
=
torch
::
data
::
make_data_loader
(
std
::
move
(
dataset
),
torch
::
data
::
DataLoaderOptions
(
batchSize
).
workers
(
0
).
max_jobs
(
0
));
auto
dataLoader
=
torch
::
data
::
make_data_loader
(
std
::
move
(
dataset
),
torch
::
data
::
DataLoaderOptions
(
batchSize
).
workers
(
0
).
max_jobs
(
0
));
TestNetwork
nn
(
machine
.
getTransitionSet
().
size
(),
5
);
TestNetwork
nn
(
machine
.
getTransitionSet
().
size
(),
5
);
torch
::
optim
::
Adam
denseOptimizer
(
nn
->
denseParameters
(),
torch
::
optim
::
AdamOptions
(
2e-
1
).
beta1
(
0.5
));
torch
::
optim
::
Adam
denseOptimizer
(
nn
->
denseParameters
(),
torch
::
optim
::
AdamOptions
(
2e-
3
).
beta1
(
0.5
));
torch
::
optim
::
SparseAdam
sparseOptimizer
(
nn
->
sparseParameters
(),
torch
::
optim
::
SparseAdamOptions
(
2e-
1
).
beta1
(
0.5
));
torch
::
optim
::
SparseAdam
sparseOptimizer
(
nn
->
sparseParameters
(),
torch
::
optim
::
SparseAdamOptions
(
2e-
3
).
beta1
(
0.5
));
for
(
int
epoch
=
1
;
epoch
<=
2
;
++
epoch
)
for
(
int
epoch
=
1
;
epoch
<=
30
;
++
epoch
)
{
{
float
totalLoss
=
0.0
;
float
totalLoss
=
0.0
;
float
lossSoFar
=
0.0
;
float
lossSoFar
=
0.0
;
...
...
This diff is collapsed.
Click to expand it.
reading_machine/include/TransitionSet.hpp
+
1
−
0
View file @
ce420cb5
...
@@ -18,6 +18,7 @@ class TransitionSet
...
@@ -18,6 +18,7 @@ class TransitionSet
TransitionSet
(
const
std
::
string
&
filename
);
TransitionSet
(
const
std
::
string
&
filename
);
std
::
vector
<
std
::
pair
<
Transition
*
,
int
>>
getAppliableTransitionsCosts
(
const
Config
&
c
);
std
::
vector
<
std
::
pair
<
Transition
*
,
int
>>
getAppliableTransitionsCosts
(
const
Config
&
c
);
Transition
*
getBestAppliableTransition
(
const
Config
&
c
);
Transition
*
getBestAppliableTransition
(
const
Config
&
c
);
std
::
size_t
getTransitionIndex
(
const
Transition
*
transition
)
const
;
std
::
size_t
size
()
const
;
std
::
size_t
size
()
const
;
};
};
...
...
This diff is collapsed.
Click to expand it.
reading_machine/src/TransitionSet.cpp
+
8
−
0
View file @
ce420cb5
...
@@ -72,3 +72,11 @@ std::size_t TransitionSet::size() const
...
@@ -72,3 +72,11 @@ std::size_t TransitionSet::size() const
return
transitions
.
size
();
return
transitions
.
size
();
}
}
std
::
size_t
TransitionSet
::
getTransitionIndex
(
const
Transition
*
transition
)
const
{
if
(
!
transition
)
util
::
myThrow
(
"transition is null"
);
return
transition
-
&
transitions
[
0
];
}
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