Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
NN Projekt SS22
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Analyze
Contributor 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
kreuzer
NN Projekt SS22
Commits
f152da51
Commit
f152da51
authored
2 years ago
by
wu
Browse files
Options
Downloads
Patches
Plain Diff
Update main_Critic.py
parent
17e13823
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main_Critic.py
+18
-7
18 additions, 7 deletions
main_Critic.py
with
18 additions
and
7 deletions
main_Critic.py
+
18
−
7
View file @
f152da51
...
@@ -19,9 +19,13 @@ def set_parameter_requires_grad(model):
...
@@ -19,9 +19,13 @@ def set_parameter_requires_grad(model):
for
param
in
model
.
parameters
():
for
param
in
model
.
parameters
():
param
.
requires_grad
=
False
# in-place
param
.
requires_grad
=
False
# in-place
# set device
device
=
torch
.
device
(
"
cuda
"
if
torch
.
cuda
.
is_available
()
else
"
cpu
"
)
# loads model ActorOnly
# loads model ActorOnly
model
=
SummarisationModel
()
model
=
SummarisationModel
()
model
.
load_state_dict
(
torch
.
load
(
'
model_actor_only_wts1_0.pth
'
))
#blow up here?
model
.
to
(
device
)
model
.
load_state_dict
(
torch
.
load
(
'
/home/students/kreuzer/ao+cel/aof/nn-projekt-ss22/model_actor_only_wts1_0.pth
'
,
map_location
=
device
))
#blow up here?
model
.
eval
()
model
.
eval
()
set_parameter_requires_grad
(
model
)
set_parameter_requires_grad
(
model
)
...
@@ -30,14 +34,15 @@ set_parameter_requires_grad(model)
...
@@ -30,14 +34,15 @@ set_parameter_requires_grad(model)
batch_loss_tracker
=
open
(
"
CriticBatchLossTracker.txt
"
,
'
a
'
)
batch_loss_tracker
=
open
(
"
CriticBatchLossTracker.txt
"
,
'
a
'
)
logger
=
open
(
"
CriticLogger.txt
"
,
'
a
'
)
logger
=
open
(
"
CriticLogger.txt
"
,
'
a
'
)
device
=
torch
.
device
(
"
cuda
"
if
torch
.
cuda
.
is_available
()
else
"
cpu
"
)
logger
.
write
(
'
Used device: {}
\n
'
.
format
(
str
(
device
)))
logger
.
write
(
'
Used device: {}
\n
'
.
format
(
str
(
device
)))
# initialize Critic
# initialize Critic
critic
=
Critic
()
critic
=
Critic
()
critic
.
to
(
device
)
optimizer
=
torch
.
optim
.
Adam
(
critic
.
parameters
(),
lr
=
0.001
)
optimizer
=
torch
.
optim
.
Adam
(
critic
.
parameters
(),
lr
=
0.001
)
loss_fn
=
nn
.
MSELoss
()
loss_fn
=
nn
.
MSELoss
()
m
.
to
(
device
)
# loads dataset cnn_dailymail
# loads dataset cnn_dailymail
train_dataset
=
PreprocessedDataSet
([
'
/workspace/students/kreuzer/complete/train
'
])
train_dataset
=
PreprocessedDataSet
([
'
/workspace/students/kreuzer/complete/train
'
])
...
@@ -79,17 +84,23 @@ for epoch in range(epochs):
...
@@ -79,17 +84,23 @@ for epoch in range(epochs):
logger
.
write
(
"
Warning! This datapoint has a too short document and will be ignored
\n
"
)
logger
.
write
(
"
Warning! This datapoint has a too short document and will be ignored
\n
"
)
continue
continue
# move "datapoint" to gpu
p_searchspace
=
datapoint
.
p_searchspace
.
to
(
device
)
sent_vecs
=
datapoint
.
sent_vecs
.
to
(
device
)
gold_sent_vecs
=
datapoint
.
gold_sent_vecs
.
to
(
device
)
top_rouge
=
datapoint
.
top_rouge
.
to
(
device
)
# positive sampling
# positive sampling
k
=
np
.
random
.
choice
(
len
(
datapoint
.
p_searchspace
))
k
=
np
.
random
.
choice
(
len
(
p_searchspace
))
sample
=
datapoint
.
sent_vecs
.
masked_select
(
datapoint
.
p_searchspace
[
k
].
bool
())
# not padded sent embeddngs
sample
=
sent_vecs
.
masked_select
(
p_searchspace
[
k
].
bool
())
# not padded sent embeddngs
# hidden states, cell states of document encoder from model ActorOnly
# hidden states, cell states of document encoder from model ActorOnly
_
,
document_vec_1
=
model
.
encode_document
(
sample
)
_
,
document_vec_1
=
model
.
encode_document
(
sample
)
_
,
document_vec_2
=
model
.
encode_document
(
datapoint
.
gold_sent_vecs
)
_
,
document_vec_2
=
model
.
encode_document
(
gold_sent_vecs
)
# critic
# critic
score
=
critic
(
document_vec_1
,
document_vec_2
)
score
=
critic
(
document_vec_1
,
document_vec_2
)
loss
=
loss_fn
(
score
,
datapoint
.
top_rouge
[
k
])
loss
=
loss_fn
(
score
,
top_rouge
[
k
])
batch_loss
+=
loss
batch_loss
+=
loss
...
...
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