Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
MLAir
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
Container registry
Model registry
Operate
Environments
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
esde
machine-learning
MLAir
Commits
fc298e80
Commit
fc298e80
authored
5 years ago
by
lukas leufen
Browse files
Options
Downloads
Patches
Plain Diff
set default for permutation to False, added some tests
parent
545146bb
No related branches found
No related tags found
2 merge requests
!50
release for v0.7.0
,
!44
Felix issue057 permutate data for minibatches
Pipeline
#30911
passed
5 years ago
Stage: test
Stage: pages
Stage: deploy
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/data_handling/data_distributor.py
+7
-7
7 additions, 7 deletions
src/data_handling/data_distributor.py
test/test_data_handling/test_data_distributor.py
+15
-5
15 additions, 5 deletions
test/test_data_handling/test_data_distributor.py
with
22 additions
and
12 deletions
src/data_handling/data_distributor.py
+
7
−
7
View file @
fc298e80
...
...
@@ -12,12 +12,12 @@ import numpy as np
class
Distributor
(
keras
.
utils
.
Sequence
):
def
__init__
(
self
,
generator
:
keras
.
utils
.
Sequence
,
model
:
keras
.
models
,
batch_size
:
int
=
256
,
fit_call
:
bool
=
True
,
permut
at
e_data
:
bool
=
Tru
e
):
fit_call
:
bool
=
True
,
permute_data
:
bool
=
Fals
e
):
self
.
generator
=
generator
self
.
model
=
model
self
.
batch_size
=
batch_size
self
.
fit_call
=
fit_call
self
.
permutat
e_data
=
permut
at
e_data
self
.
do_data_
permutat
ion
=
permute_data
def
_get_model_rank
(
self
):
mod_out
=
self
.
model
.
output_shape
...
...
@@ -34,11 +34,11 @@ class Distributor(keras.utils.Sequence):
def
_get_number_of_mini_batches
(
self
,
values
):
return
math
.
ceil
(
values
[
0
].
shape
[
0
]
/
self
.
batch_size
)
def
_permut
at
e_data
(
self
,
x
,
y
):
def
_permute_data
(
self
,
x
,
y
):
"""
Permut
at
e inputs x and labels y
Permute inputs x and labels y
"""
if
self
.
permutat
e_data
:
if
self
.
do_data_
permutat
ion
:
p
=
np
.
random
.
permutation
(
len
(
x
))
# equiv to .shape[0]
x
=
x
[
p
]
y
=
y
[
p
]
...
...
@@ -53,8 +53,8 @@ class Distributor(keras.utils.Sequence):
num_mini_batches
=
self
.
_get_number_of_mini_batches
(
v
)
x_total
=
np
.
copy
(
v
[
0
])
y_total
=
np
.
copy
(
v
[
1
])
# permut
at
e order for mini-batches
x_total
,
y_total
=
self
.
_permut
at
e_data
(
x_total
,
y_total
)
# permute order for mini-batches
x_total
,
y_total
=
self
.
_permute_data
(
x_total
,
y_total
)
for
prev
,
curr
in
enumerate
(
range
(
1
,
num_mini_batches
+
1
)):
x
=
x_total
[
prev
*
self
.
batch_size
:
curr
*
self
.
batch_size
,
...]
y
=
[
y_total
[
prev
*
self
.
batch_size
:
curr
*
self
.
batch_size
,
...]
for
_
in
range
(
mod_rank
)]
...
...
This diff is collapsed.
Click to expand it.
test/test_data_handling/test_data_distributor.py
+
15
−
5
View file @
fc298e80
...
...
@@ -38,6 +38,7 @@ class TestDistributor:
def
test_init_defaults
(
self
,
distributor
):
assert
distributor
.
batch_size
==
256
assert
distributor
.
fit_call
is
True
assert
distributor
.
do_data_permutation
is
False
def
test_get_model_rank
(
self
,
distributor
,
model_with_minor_branch
):
assert
distributor
.
_get_model_rank
()
==
1
...
...
@@ -74,10 +75,18 @@ class TestDistributor:
expected
=
math
.
ceil
(
len
(
gen
[
0
][
0
])
/
256
)
+
math
.
ceil
(
len
(
gen
[
1
][
0
])
/
256
)
assert
len
(
d
)
==
expected
def
test_permut
at
e_data
(
self
,
distributor
):
def
test_permute_data
_no_permutation
(
self
,
distributor
):
x
=
np
.
array
(
range
(
20
)).
reshape
(
2
,
10
).
T
y
=
np
.
array
(
range
(
10
)).
reshape
(
10
,
1
)
x_perm
,
y_perm
=
distributor
.
_permutate_data
(
x
,
y
)
x_perm
,
y_perm
=
distributor
.
_permute_data
(
x
,
y
)
assert
np
.
testing
.
assert_equal
(
x
,
x_perm
)
is
None
assert
np
.
testing
.
assert_equal
(
y
,
y_perm
)
is
None
def
test_permute_data
(
self
,
distributor
):
x
=
np
.
array
(
range
(
20
)).
reshape
(
2
,
10
).
T
y
=
np
.
array
(
range
(
10
)).
reshape
(
10
,
1
)
distributor
.
do_data_permutation
=
True
x_perm
,
y_perm
=
distributor
.
_permute_data
(
x
,
y
)
assert
x_perm
[
0
,
0
]
==
y_perm
[
0
]
assert
x_perm
[
0
,
1
]
==
y_perm
[
0
]
+
10
assert
x_perm
[
5
,
0
]
==
y_perm
[
5
]
...
...
@@ -87,5 +96,6 @@ class TestDistributor:
# resort x_perm and compare if equal to x
x_perm
.
sort
(
axis
=
0
)
y_perm
.
sort
(
axis
=
0
)
assert
(
x
==
x_perm
).
all
()
assert
(
y
==
y_perm
).
all
()
assert
np
.
testing
.
assert_equal
(
x
,
x_perm
)
is
None
assert
np
.
testing
.
assert_equal
(
y
,
y_perm
)
is
None
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