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
a4844d76
Commit
a4844d76
authored
5 years ago
by
Franck Dary
Browse files
Options
Downloads
Patches
Plain Diff
Changed the loss to CrossEntropy, to avoid using log (caused exploding gradient)
parent
096b59d9
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
torch_modules/src/ConcatWordsNetwork.cpp
+1
-3
1 addition, 3 deletions
torch_modules/src/ConcatWordsNetwork.cpp
torch_modules/src/OneWordNetwork.cpp
+1
-1
1 addition, 1 deletion
torch_modules/src/OneWordNetwork.cpp
trainer/src/Trainer.cpp
+5
-3
5 additions, 3 deletions
trainer/src/Trainer.cpp
with
7 additions
and
7 deletions
torch_modules/src/ConcatWordsNetwork.cpp
+
1
−
3
View file @
a4844d76
...
@@ -35,8 +35,6 @@ torch::Tensor ConcatWordsNetworkImpl::forward(torch::Tensor input)
...
@@ -35,8 +35,6 @@ torch::Tensor ConcatWordsNetworkImpl::forward(torch::Tensor input)
// reshaped dim = {batch, sequence of embeddings}
// reshaped dim = {batch, sequence of embeddings}
auto
reshaped
=
wordsAsEmb
.
dim
()
==
3
?
torch
::
reshape
(
wordsAsEmb
,
{
wordsAsEmb
.
size
(
0
),
wordsAsEmb
.
size
(
1
)
*
wordsAsEmb
.
size
(
2
)})
:
torch
::
reshape
(
wordsAsEmb
,
{
wordsAsEmb
.
size
(
0
)
*
wordsAsEmb
.
size
(
1
)});
auto
reshaped
=
wordsAsEmb
.
dim
()
==
3
?
torch
::
reshape
(
wordsAsEmb
,
{
wordsAsEmb
.
size
(
0
),
wordsAsEmb
.
size
(
1
)
*
wordsAsEmb
.
size
(
2
)})
:
torch
::
reshape
(
wordsAsEmb
,
{
wordsAsEmb
.
size
(
0
)
*
wordsAsEmb
.
size
(
1
)});
auto
res
=
torch
::
softmax
(
linear2
(
torch
::
relu
(
linear1
(
reshaped
))),
reshaped
.
dim
()
==
2
?
1
:
0
);
return
linear2
(
torch
::
relu
(
linear1
(
reshaped
)));
return
res
;
}
}
This diff is collapsed.
Click to expand it.
torch_modules/src/OneWordNetwork.cpp
+
1
−
1
View file @
a4844d76
...
@@ -45,7 +45,7 @@ torch::Tensor OneWordNetworkImpl::forward(torch::Tensor input)
...
@@ -45,7 +45,7 @@ torch::Tensor OneWordNetworkImpl::forward(torch::Tensor input)
if
(
reshaped
.
dim
()
==
3
)
if
(
reshaped
.
dim
()
==
3
)
reshaped
=
wordsAsEmb
.
permute
({
1
,
0
,
2
});
reshaped
=
wordsAsEmb
.
permute
({
1
,
0
,
2
});
auto
res
=
torch
::
softmax
(
linear
(
reshaped
[
focusedIndex
])
,
reshaped
.
dim
()
==
3
?
1
:
0
)
;
auto
res
=
linear
(
reshaped
[
focusedIndex
]);
return
res
;
return
res
;
}
}
...
...
This diff is collapsed.
Click to expand it.
trainer/src/Trainer.cpp
+
5
−
3
View file @
a4844d76
...
@@ -58,8 +58,8 @@ void Trainer::createDataset(SubConfig & config, bool debug)
...
@@ -58,8 +58,8 @@ void Trainer::createDataset(SubConfig & config, bool debug)
dataLoader
=
torch
::
data
::
make_data_loader
(
Dataset
(
contexts
,
classes
).
map
(
torch
::
data
::
transforms
::
Stack
<>
()),
torch
::
data
::
DataLoaderOptions
(
batchSize
).
workers
(
0
).
max_jobs
(
0
));
dataLoader
=
torch
::
data
::
make_data_loader
(
Dataset
(
contexts
,
classes
).
map
(
torch
::
data
::
transforms
::
Stack
<>
()),
torch
::
data
::
DataLoaderOptions
(
batchSize
).
workers
(
0
).
max_jobs
(
0
));
denseOptimizer
.
reset
(
new
torch
::
optim
::
Adam
(
machine
.
getClassifier
()
->
getNN
()
->
denseParameters
(),
torch
::
optim
::
AdamOptions
(
2e-
4
).
beta1
(
0.5
)));
denseOptimizer
.
reset
(
new
torch
::
optim
::
Adam
(
machine
.
getClassifier
()
->
getNN
()
->
denseParameters
(),
torch
::
optim
::
AdamOptions
(
2e-
3
).
beta1
(
0.5
)));
sparseOptimizer
.
reset
(
new
torch
::
optim
::
SparseAdam
(
machine
.
getClassifier
()
->
getNN
()
->
sparseParameters
(),
torch
::
optim
::
SparseAdamOptions
(
2e-
4
).
beta1
(
0.5
)));
sparseOptimizer
.
reset
(
new
torch
::
optim
::
SparseAdam
(
machine
.
getClassifier
()
->
getNN
()
->
sparseParameters
(),
torch
::
optim
::
SparseAdamOptions
(
2e-
3
).
beta1
(
0.5
)));
}
}
float
Trainer
::
epoch
(
bool
printAdvancement
)
float
Trainer
::
epoch
(
bool
printAdvancement
)
...
@@ -70,6 +70,8 @@ float Trainer::epoch(bool printAdvancement)
...
@@ -70,6 +70,8 @@ float Trainer::epoch(bool printAdvancement)
int
nbExamplesUntilPrint
=
printInterval
;
int
nbExamplesUntilPrint
=
printInterval
;
int
currentBatchNumber
=
0
;
int
currentBatchNumber
=
0
;
auto
lossFct
=
torch
::
nn
::
CrossEntropyLoss
();
for
(
auto
&
batch
:
*
dataLoader
)
for
(
auto
&
batch
:
*
dataLoader
)
{
{
denseOptimizer
->
zero_grad
();
denseOptimizer
->
zero_grad
();
...
@@ -80,7 +82,7 @@ float Trainer::epoch(bool printAdvancement)
...
@@ -80,7 +82,7 @@ float Trainer::epoch(bool printAdvancement)
auto
prediction
=
machine
.
getClassifier
()
->
getNN
()(
data
);
auto
prediction
=
machine
.
getClassifier
()
->
getNN
()(
data
);
auto
loss
=
torch
::
nll_loss
(
torch
::
log
(
prediction
)
,
labels
);
auto
loss
=
lossFct
(
prediction
,
labels
);
try
try
{
{
totalLoss
+=
loss
.
item
<
float
>
();
totalLoss
+=
loss
.
item
<
float
>
();
...
...
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