Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
Need for Speed - Transformers
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
holzinger
Need for Speed - Transformers
Commits
2634b50a
Commit
2634b50a
authored
2 years ago
by
holzinger
Browse files
Options
Downloads
Patches
Plain Diff
added Dataloader module
parent
756704ed
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
Dataloader.py
+134
-0
134 additions, 0 deletions
Dataloader.py
with
134 additions
and
0 deletions
Dataloader.py
0 → 100644
+
134
−
0
View file @
2634b50a
# imports
import
os
import
re
import
string
import
tensorflow
as
tf
import
tensorflow_datasets
as
tfds
class
Data
:
def
__init__
(
self
,
name
,
batch_size
=
32
,
vectorize
=
True
)
->
None
:
self
.
name
=
name
self
.
bs
=
batch_size
self
.
num_labels
=
1
self
.
vocab_size
=
int
(
1e6
)
self
.
sequence_length
=
512
self
.
vectorize
=
vectorize
self
.
path
=
"
/home/students/holzinger/tmp/arxiv
"
def
load
(
self
):
def
vectorize
(
text
,
label
):
text
=
tf
.
expand_dims
(
text
,
-
1
)
return
self
.
vectorize_data
(
text
),
label
def
transform_data
(
dictionary
):
text
=
dictionary
[
'
sentence
'
]
label
=
dictionary
[
'
label
'
]
return
text
,
label
if
self
.
name
==
"
imdb
"
:
self
.
sequence_length
=
3072
self
.
vectorize_data
=
tf
.
keras
.
layers
.
TextVectorization
(
standardize
=
custom_standardization
,
max_tokens
=
self
.
vocab_size
,
output_mode
=
'
int
'
,
output_sequence_length
=
self
.
sequence_length
)
# load data
train_ds
=
tfds
.
load
(
'
imdb_reviews
'
,
split
=
'
train[:80%]
'
,
batch_size
=
self
.
bs
,
as_supervised
=
True
)
val_ds
=
tfds
.
load
(
'
imdb_reviews
'
,
split
=
'
train[80%:]
'
,
batch_size
=
self
.
bs
,
as_supervised
=
True
)
test_ds
=
tfds
.
load
(
'
imdb_reviews
'
,
split
=
'
test
'
,
batch_size
=
self
.
bs
,
as_supervised
=
True
)
if
self
.
vectorize
:
# vectorize data
train_text
=
train_ds
.
map
(
lambda
text
,
labels
:
text
)
self
.
vectorize_data
.
adapt
(
train_text
)
train_ds
=
train_ds
.
map
(
vectorize
)
val_ds
=
val_ds
.
map
(
vectorize
)
test_ds
=
test_ds
.
map
(
vectorize
)
# configure datasets for performance
train_ds
=
configure_dataset
(
train_ds
)
val_ds
=
configure_dataset
(
val_ds
)
test_ds
=
configure_dataset
(
test_ds
)
elif
self
.
name
==
'
sst2
'
:
self
.
sequence_length
=
64
self
.
vectorize_data
=
tf
.
keras
.
layers
.
TextVectorization
(
standardize
=
custom_standardization
,
max_tokens
=
self
.
vocab_size
,
output_mode
=
'
int
'
,
output_sequence_length
=
self
.
sequence_length
)
# load data
train_ds
=
tfds
.
load
(
'
glue/sst2
'
,
split
=
'
train[:80%]
'
,
batch_size
=
self
.
bs
)
val_ds
=
tfds
.
load
(
'
glue/sst2
'
,
split
=
'
train[80%:]
'
,
batch_size
=
self
.
bs
)
test_ds
=
tfds
.
load
(
'
glue/sst2
'
,
split
=
'
validation
'
,
batch_size
=
self
.
bs
)
train_ds
=
train_ds
.
map
(
transform_data
)
val_ds
=
val_ds
.
map
(
transform_data
)
test_ds
=
test_ds
.
map
(
transform_data
)
if
self
.
vectorize
:
# vectorize data
train_text
=
train_ds
.
map
(
lambda
text
,
labels
:
text
)
self
.
vectorize_data
.
adapt
(
train_text
)
train_ds
=
train_ds
.
map
(
vectorize
)
val_ds
=
val_ds
.
map
(
vectorize
)
test_ds
=
test_ds
.
map
(
vectorize
)
# configure datasets for performance
train_ds
=
configure_dataset
(
train_ds
)
val_ds
=
configure_dataset
(
val_ds
)
test_ds
=
configure_dataset
(
test_ds
)
elif
self
.
name
==
'
arxiv
'
:
self
.
sequence_length
=
8192
self
.
vectorize_data
=
tf
.
keras
.
layers
.
TextVectorization
(
standardize
=
custom_standardization
,
max_tokens
=
self
.
vocab_size
,
output_mode
=
'
int
'
,
output_sequence_length
=
self
.
sequence_length
)
# load data
dataset
=
tf
.
keras
.
utils
.
text_dataset_from_directory
(
self
.
path
)
train_ds
,
val_ds
,
test_ds
=
get_dataset_partitions_tf
(
dataset
,
len
(
dataset
))
if
self
.
vectorize
:
# vectorize data
train_text
=
train_ds
.
map
(
lambda
text
,
labels
:
text
)
self
.
vectorize_data
.
adapt
(
train_text
)
train_ds
=
train_ds
.
map
(
vectorize
)
val_ds
=
val_ds
.
map
(
vectorize
)
test_ds
=
test_ds
.
map
(
vectorize
)
# configure datasets for performance
train_ds
=
configure_dataset
(
train_ds
)
val_ds
=
configure_dataset
(
val_ds
)
test_ds
=
configure_dataset
(
test_ds
)
#self.num_labels = len(dataset.class_names)
return
{
'
train
'
:
train_ds
,
'
val
'
:
val_ds
,
'
test
'
:
test_ds
}
def
custom_standardization
(
input_data
):
lowercase
=
tf
.
strings
.
lower
(
input_data
)
stripped_html
=
tf
.
strings
.
regex_replace
(
lowercase
,
'
<br />
'
,
'
'
)
return
tf
.
strings
.
regex_replace
(
stripped_html
,
'
[%s]
'
%
re
.
escape
(
string
.
punctuation
),
''
)
def
configure_dataset
(
dataset
):
return
dataset
.
cache
().
prefetch
(
buffer_size
=
tf
.
data
.
AUTOTUNE
)
def
get_dataset_partitions_tf
(
ds
,
ds_size
,
train_split
=
0.8
,
val_split
=
0.1
,
test_split
=
0.1
,
shuffle
=
True
,
shuffle_size
=
1000
):
assert
(
train_split
+
test_split
+
val_split
)
==
1
if
shuffle
:
# Specify seed to always have the same split distribution between runs
ds
=
ds
.
shuffle
(
shuffle_size
,
seed
=
12
)
train_size
=
int
(
train_split
*
ds_size
)
val_size
=
int
(
val_split
*
ds_size
)
train_ds
=
ds
.
take
(
train_size
)
val_ds
=
ds
.
skip
(
train_size
).
take
(
val_size
)
test_ds
=
ds
.
skip
(
train_size
).
skip
(
val_size
)
return
train_ds
,
val_ds
,
test_ds
def
main
():
pass
if
__name__
==
"
__main__
"
:
main
()
\ No newline at end of file
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