Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RL-Parsing
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
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
RL-Parsing
Commits
47213de3
Commit
47213de3
authored
3 years ago
by
Franck Dary
Browse files
Options
Downloads
Patches
Plain Diff
Added LSTM network
parent
2038e1bc
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Networks.py
+66
-0
66 additions, 0 deletions
Networks.py
with
66 additions
and
0 deletions
Networks.py
+
66
−
0
View file @
47213de3
...
...
@@ -54,3 +54,69 @@ class BaseNet(nn.Module):
################################################################################
################################################################################
class
LSTMNet
(
nn
.
Module
):
def
__init__
(
self
,
dicts
,
outputSize
,
incremental
)
:
super
().
__init__
()
self
.
dummyParam
=
nn
.
Parameter
(
torch
.
empty
(
0
),
requires_grad
=
False
)
self
.
incremental
=
incremental
self
.
featureFunctionLSTM
=
"
b.-2 b.-1 b.0 b.1 b.2
"
self
.
featureFunction
=
"
s.0 s.1 s.2 s.0.0 s.0.-1 s.0.1 s.1.0 s.1.-1 s.1.1 s.2.0 s.2.-1 s.2.1
"
self
.
historyNb
=
5
self
.
columns
=
[
"
UPOS
"
,
"
FORM
"
]
self
.
embSize
=
64
self
.
nbInputLSTM
=
len
(
self
.
featureFunctionLSTM
.
split
())
self
.
nbInputBase
=
len
(
self
.
featureFunction
.
split
())
self
.
nbTargets
=
self
.
nbInputBase
+
self
.
nbInputLSTM
self
.
inputSize
=
len
(
self
.
columns
)
*
self
.
nbTargets
+
self
.
historyNb
self
.
outputSize
=
outputSize
for
name
in
dicts
.
dicts
:
self
.
add_module
(
"
emb_
"
+
name
,
nn
.
Embedding
(
len
(
dicts
.
dicts
[
name
]),
self
.
embSize
))
self
.
lstmFeat
=
nn
.
LSTM
(
len
(
self
.
columns
)
*
self
.
embSize
,
len
(
self
.
columns
)
*
int
(
self
.
embSize
/
2
),
1
,
batch_first
=
True
,
bidirectional
=
True
)
self
.
lstmHist
=
nn
.
LSTM
(
self
.
embSize
,
int
(
self
.
embSize
/
2
),
1
,
batch_first
=
True
,
bidirectional
=
True
)
self
.
fc1
=
nn
.
Linear
(
self
.
inputSize
*
self
.
embSize
,
1600
)
self
.
fc2
=
nn
.
Linear
(
1600
,
outputSize
)
self
.
dropout
=
nn
.
Dropout
(
0.3
)
self
.
apply
(
self
.
initWeights
)
def
forward
(
self
,
x
)
:
embeddings
=
[]
embeddingsLSTM
=
[]
for
i
in
range
(
len
(
self
.
columns
))
:
embeddings
.
append
(
getattr
(
self
,
"
emb_
"
+
self
.
columns
[
i
])(
x
[...,
i
*
self
.
nbInputBase
:(
i
+
1
)
*
self
.
nbInputBase
]))
for
i
in
range
(
len
(
self
.
columns
))
:
embeddingsLSTM
.
append
(
getattr
(
self
,
"
emb_
"
+
self
.
columns
[
i
])(
x
[...,
len
(
self
.
columns
)
*
self
.
nbInputBase
+
i
*
self
.
nbInputLSTM
:
len
(
self
.
columns
)
*
self
.
nbInputBase
+
(
i
+
1
)
*
self
.
nbInputLSTM
]))
z
=
torch
.
cat
(
embeddingsLSTM
,
-
1
)
z
=
self
.
lstmFeat
(
z
)[
0
]
z
=
z
.
reshape
(
x
.
size
(
0
),
-
1
)
y
=
torch
.
cat
(
embeddings
,
-
1
).
reshape
(
x
.
size
(
0
),
-
1
)
y
=
torch
.
cat
([
y
,
z
],
-
1
)
if
self
.
historyNb
>
0
:
historyEmb
=
getattr
(
self
,
"
emb_HISTORY
"
)(
x
[...,
len
(
self
.
columns
)
*
self
.
nbTargets
:
len
(
self
.
columns
)
*
self
.
nbTargets
+
self
.
historyNb
])
historyEmb
=
self
.
lstmHist
(
historyEmb
)[
0
]
historyEmb
=
historyEmb
.
reshape
(
x
.
size
(
0
),
-
1
)
y
=
torch
.
cat
([
y
,
historyEmb
],
-
1
)
y
=
self
.
dropout
(
y
)
y
=
F
.
relu
(
self
.
dropout
(
self
.
fc1
(
y
)))
y
=
self
.
fc2
(
y
)
return
y
def
currentDevice
(
self
)
:
return
self
.
dummyParam
.
device
def
initWeights
(
self
,
m
)
:
if
type
(
m
)
==
nn
.
Linear
:
torch
.
nn
.
init
.
xavier_uniform_
(
m
.
weight
)
m
.
bias
.
data
.
fill_
(
0.01
)
def
extractFeatures
(
self
,
dicts
,
config
)
:
colsValuesBase
=
Features
.
extractColsFeatures
(
dicts
,
config
,
self
.
featureFunction
,
self
.
columns
,
self
.
incremental
)
colsValuesLSTM
=
Features
.
extractColsFeatures
(
dicts
,
config
,
self
.
featureFunctionLSTM
,
self
.
columns
,
self
.
incremental
)
historyValues
=
Features
.
extractHistoryFeatures
(
dicts
,
config
,
self
.
historyNb
)
return
torch
.
cat
([
colsValuesBase
,
colsValuesLSTM
,
historyValues
])
################################################################################
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