Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
outillage
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package 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
Alice Brenon
outillage
Commits
774ad89b
Commit
774ad89b
authored
1 year ago
by
Alice Brenon
Browse files
Options
Downloads
Patches
Plain Diff
Factorize BERT components into a subclass
parent
ef245f29
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
scripts/ML/BERT.py
+12
-0
12 additions, 0 deletions
scripts/ML/BERT.py
scripts/ML/loaders.py
+6
-4
6 additions, 4 deletions
scripts/ML/loaders.py
scripts/ML/predict.py
+5
-10
5 additions, 10 deletions
scripts/ML/predict.py
with
23 additions
and
14 deletions
scripts/ML/BERT.py
0 → 100644
+
12
−
0
View file @
774ad89b
from
loaders
import
get_device
class
BERT
:
model_name
=
'
bert-base-multilingual-cased
'
def
__init
(
self
,
path
):
print
(
'
Loading BERT tools…
'
)
self
.
tokenizer
=
BertTokenizer
.
from_pretrained
(
BERT
.
model_name
)
print
(
'
✔️ tokenizer
'
)
self
.
device
=
get_device
()
bert
=
BertForSequenceClassification
.
from_pretrained
(
path
)
self
.
model
=
bert
.
to
(
self
.
device
.
type
)
print
(
'
✔️ classifier
'
)
This diff is collapsed.
Click to expand it.
scripts/ML/loaders.py
+
6
−
4
View file @
774ad89b
...
...
@@ -25,7 +25,9 @@ def get_encoder(root_path, create_from=None):
else
:
raise
FileNotFoundError
(
path
)
def
get_tokenizer
():
model_name
=
'
bert-base-multilingual-cased
'
print
(
'
Loading BERT tokenizer...
'
)
return
BertTokenizer
.
from_pretrained
(
model_name
)
def
set_random
():
seed_value
=
42
random
.
seed
(
seed_val
)
np
.
random
.
seed
(
seed_val
)
torch
.
manual_seed
(
seed_val
)
torch
.
cuda
.
manual_seed_all
(
seed_val
)
This diff is collapsed.
Click to expand it.
scripts/ML/predict.py
+
5
−
10
View file @
774ad89b
#!/usr/bin/env python3
import
loaders
import
get_device
,
get_encoder
,
get_tokenizer
from
BERT
import
BERT
from
loaders
import
get_encoder
import
numpy
import
pandas
import
sklearn
...
...
@@ -8,10 +9,10 @@ from sys import argv
from
tqdm
import
tqdm
from
transformers
import
BertForSequenceClassification
,
BertTokenizer
,
TextClassificationPipeline
class
Classifier
:
class
Classifier
(
BERT
)
:
"""
A class wrapping all the different models and classes used throughout a
classification task:
classification task
and based on BERT
:
- tokenizer
- classifier
...
...
@@ -22,16 +23,10 @@ class Classifier:
containing the texts to classify
"""
def
__init__
(
self
,
root_path
):
self
.
device
=
get_device
()
self
.
tokenizer
=
get_tokenizer
()
self
.
_init_model
(
root_path
)
BERT
.
__init__
(
self
,
root_path
)
self
.
_init_pipe
()
self
.
encoder
=
get_encoder
(
root_path
)
def
_init_model
(
self
,
path
):
bert
=
BertForSequenceClassification
.
from_pretrained
(
path
)
self
.
model
=
bert
.
to
(
self
.
device
.
type
)
def
_init_pipe
(
self
):
self
.
pipe
=
TextClassificationPipeline
(
model
=
self
.
model
,
...
...
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