Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Data Augmentation for Metonymy Resolution
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
friebolin
Data Augmentation for Metonymy Resolution
Commits
586ba564
Commit
586ba564
authored
2 years ago
by
friebolin
Browse files
Options
Downloads
Patches
Plain Diff
Update inference
parent
ef32f86f
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
Code/inference.py
+14
-12
14 additions, 12 deletions
Code/inference.py
with
14 additions
and
12 deletions
Code/inference.py
+
14
−
12
View file @
586ba564
import
argparse
import
torch
import
preprocess
import
train
import
models
from
transformers
import
BertTokenizer
,
RobertaTokenizer
,
BertModel
,
RobertaModel
,
RobertaPreTrainedModel
,
RobertaConfig
,
BertConfig
,
BertPreTrainedModel
,
PreTrainedModel
,
AutoConfig
,
AutoModel
,
AutoTokenizer
import
re
import
models
import
train
from
torch.utils.data
import
DataLoader
,
RandomSampler
# Get user input
print
(
"
Enter a sentence:
"
)
sentence
=
input
()
sentence
=
sentence
.
split
()
...
...
@@ -16,22 +18,21 @@ target_pos = input()
print
(
"
Enter the label: 0 for literal, 1 for non-literal
"
)
label
=
int
(
input
())
data_sample
=
{
"
sentence
"
:
sentence
,
"
pos
"
:
target_pos
,
"
label
"
:
label
}
print
(
data_sample
)
filepath
=
"
./saved_models/bert_baseline.pt
"
model
=
models
.
BertForWordClassification
.
from_pretrained
(
"
bert-base-uncased
"
)
#tokenizer=AutoTokenizer.from_pretrained(args.architecture)
# Load model
device
=
torch
.
device
(
'
cuda
'
if
torch
.
cuda
.
is_available
()
else
'
cpu
'
)
model
.
load_state_dict
(
torch
.
load
(
filepath
))
model
.
eval
()
#loads saved model
model
=
models
.
BertForWordClassification
.
from_pretrained
(
"
bert-base-uncased
"
)
model_path
=
"
saved_models/bert_baseline.pth
"
model
=
torch
.
load
(
model_path
,
map_location
=
device
)
train_dataset
=
[{
"
sentence
"
:
[
"
Yet
"
,
"
how
"
,
"
many
"
,
"
times
"
,
"
has
"
,
"
America
"
,
"
sided
"
,
"
with
"
,
"
Israeli
"
,
"
aggression
"
,
"
against
"
,
"
the
"
,
"
people
"
,
"
of
"
,
"
Palestine?
"
],
"
pos
"
:
[
5
,
6
],
"
label
"
:
1
}]
train_sampler
=
RandomSampler
(
train_dataset
)
train_dataloader
=
DataLoader
(
train_dataset
,
sampler
=
train_sampler
,
batch_size
=
1
)
tokenizer
=
AutoTokenizer
.
from_pretrained
(
"
bert-base-uncased
"
)
train_sampler
=
RandomSampler
(
data_sample
)
train_dataloader
=
DataLoader
(
data_sample
,
sampler
=
train_sampler
,
batch_size
=
1
)
for
batch
in
train_dataloader
:
inputs
=
{
'
input_ids
'
:
batch
[
0
],
...
...
@@ -44,4 +45,5 @@ for batch in train_dataloader:
start_positions
=
batch
[
3
]
end_positions
=
batch
[
4
]
outputs
=
model
(
**
inputs
)
print
(
"
Outputs:
"
,
outputs
)
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