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
bb5b34c5
Commit
bb5b34c5
authored
3 years ago
by
Maxime Petit
Browse files
Options
Downloads
Patches
Plain Diff
Start RL
parent
04472a67
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
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
Rl.py
+39
-0
39 additions, 0 deletions
Rl.py
Train.py
+40
-0
40 additions, 0 deletions
Train.py
with
80 additions
and
0 deletions
.gitignore
+
1
−
0
View file @
bb5b34c5
__pycache__
bin/*
.idea
This diff is collapsed.
Click to expand it.
Rl.py
0 → 100644
+
39
−
0
View file @
bb5b34c5
import
random
import
torch
################################################################################
class
ReplayMemory
(
object
):
def
__init__
(
self
,
capacity
):
self
.
capacity
=
capacity
self
.
memory
=
[]
self
.
position
=
0
def
push
(
self
,
transition
):
"""
Saves a transition.
"""
if
len
(
self
.
memory
)
<
self
.
capacity
:
self
.
memory
.
append
(
None
)
self
.
memory
[
self
.
position
]
=
transition
self
.
position
=
(
self
.
position
+
1
)
%
self
.
capacity
def
sample
(
self
,
batch_size
):
return
random
.
sample
(
self
.
memory
,
batch_size
)
def
__len__
(
self
):
return
len
(
self
.
memory
)
################################################################################
################################################################################
def
selectAction
(
network
,
state
,
ts
):
sample
=
random
.
random
()
if
sample
>
.
2
:
with
torch
.
no_grad
():
return
ts
[
max
(
torch
.
nn
.
functional
.
softmax
(
network
(
state
),
dim
=
1
))].
name
else
:
return
ts
[
random
.
randrange
(
len
(
ts
))].
name
################################################################################
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Train.py
+
40
−
0
View file @
bb5b34c5
...
...
@@ -6,6 +6,7 @@ from Transition import Transition, getMissingLinks, applyTransition
import
Features
from
Dicts
import
Dicts
from
Util
import
timeStamp
from
Rl
import
ReplayMemory
,
selectAction
import
Networks
import
Decode
import
Config
...
...
@@ -23,6 +24,10 @@ def trainMode(debug, filename, type, modelDir, nbIter, batchSize, devFile, silen
trainModelOracle
(
debug
,
modelDir
,
filename
,
nbIter
,
batchSize
,
devFile
,
transitionSet
,
strategy
,
sentences
,
silent
)
return
if
type
==
"
rl
"
:
trainModelRl
(
debug
,
modelDir
,
filename
,
nbIter
,
batchSize
,
devFile
,
transitionSet
,
strategy
,
sentences
,
silent
)
return
print
(
"
ERROR : unknown type
'
%s
'"
%
type
,
file
=
sys
.
stderr
)
exit
(
1
)
################################################################################
...
...
@@ -110,3 +115,38 @@ def trainModelOracle(debug, modelDir, filename, nbIter, batchSize, devFile, tran
print
(
"
%s : Epoch %d, loss=%.2f%s %s
"
%
(
timeStamp
(),
iter
,
totalLoss
,
devScore
,
"
SAVED
"
if
saved
else
""
),
file
=
sys
.
stderr
)
################################################################################
def
trainModelRl
(
debug
,
modelDir
,
filename
,
nbIter
,
batchSize
,
devFile
,
transitionSet
,
strategy
,
sentences
,
silent
=
False
)
:
memory
=
ReplayMemory
(
1000
)
dicts
=
Dicts
()
dicts
.
readConllu
(
filename
,
[
"
FORM
"
,
"
UPOS
"
])
dicts
.
save
(
modelDir
+
"
/dicts.json
"
)
policy_net
=
Networks
.
BaseNet
(
dicts
,
13
,
len
(
transitionSet
))
target_net
=
Networks
.
BaseNet
(
dicts
,
13
,
len
(
transitionSet
))
target_net
.
load_state_dict
(
policy_net
.
state_dict
())
target_net
.
eval
()
optimizer
=
torch
.
optim
.
Adam
(
policy_net
.
parameters
(),
lr
=
0.0001
)
lossFct
=
torch
.
nn
.
CrossEntropyLoss
()
bestLoss
=
None
bestScore
=
None
for
i_episode
in
range
(
nbIter
):
sentence
=
sentences
[
i_episode
%
len
(
sentences
)]
state
=
Features
.
extractFeaturesPosExtended
(
dicts
,
sentence
)
notDone
=
True
while
notDone
:
action
=
selectAction
(
policy_net
,
state
,
transitionSet
)
print
(
action
,
file
=
sys
.
stderr
)
notDone
=
applyTransition
(
transitionSet
,
strategy
,
sentence
,
action
)
reward
=
getReward
(
state
,
newState
)
reward
=
torch
.
tensor
([
reward
])
if
notDone
:
newState
=
Features
.
extractFeaturesPosExtended
(
dicts
,
sentence
)
else
:
newState
=
None
memory
.
push
((
state
,
action
,
newState
,
reward
))
state
=
newState
optimizeModel
()
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