Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bob.learn.tensorflow
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
bob
bob.learn.tensorflow
Commits
319ef692
Commit
319ef692
authored
7 years ago
by
Guillaume HEUSCH
Browse files
Options
Downloads
Patches
Plain Diff
[datashuffler] added the get_batch_epoch function in Memory datashuffler
parent
21c86cf3
No related branches found
No related tags found
1 merge request
!8
Gan
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bob/learn/tensorflow/datashuffler/Memory.py
+80
-0
80 additions, 0 deletions
bob/learn/tensorflow/datashuffler/Memory.py
with
80 additions
and
0 deletions
bob/learn/tensorflow/datashuffler/Memory.py
+
80
−
0
View file @
319ef692
...
...
@@ -62,6 +62,11 @@ class Memory(Base):
# Seting the seed
numpy
.
random
.
seed
(
seed
)
self
.
data
=
self
.
data
.
astype
(
input_dtype
)
# number of training examples as a 'list'
self
.
indexes
=
numpy
.
array
(
range
(
self
.
data
.
shape
[
0
]))
# shuffle the indexes to get randomized mini-batches
numpy
.
random
.
shuffle
(
self
.
indexes
)
def
get_batch
(
self
):
"""
...
...
@@ -100,3 +105,78 @@ class Memory(Base):
selected_data
=
self
.
normalize_sample
(
selected_data
)
return
[
selected_data
.
astype
(
"
float32
"
),
selected_labels
.
astype
(
"
int64
"
)]
def
get_batch_epoch
(
self
):
"""
get_batch_epoch() -> selected_data, selected_labels
This function selects and returns data to be used in a minibatch iterations.
Note that it works in epochs, i.e. all the training data should be seen
during one epoch, which consists in several minibatch iterations.
**Returns**
selected_data:
Selected samples
selected_labels:
Correspondent labels
"""
# this is done to rebuild the whole list (i.e. at the end of one epoch)
epoch_done
=
False
# returned mini-batch
selected_data
=
numpy
.
zeros
(
shape
=
self
.
shape
)
selected_labels
=
[]
# if there is not enough available data to fill the current mini-batch
# add randomly some examples THAT ARE NOT STILL PRESENT in the dataset !
if
len
(
self
.
indexes
)
<
self
.
batch_size
:
print
"
should add examples to the current minibatch {0}
"
.
format
(
len
(
self
.
indexes
))
# since we reached the end of an epoch, we'll have to reconsider all the data
epoch_done
=
True
number_of_examples_to_add
=
self
.
batch_size
-
len
(
self
.
indexes
)
added_examples
=
0
# generate a list of potential examples to add to this mini-batch
potential_indexes
=
numpy
.
array
(
range
(
self
.
data
.
shape
[
0
]))
numpy
.
random
.
shuffle
(
potential_indexes
)
# add indexes that are not still present in the training data
for
pot_index
in
potential_indexes
:
if
pot_index
not
in
self
.
indexes
:
self
.
indexes
=
numpy
.
append
(
self
.
indexes
,
[
pot_index
])
added_examples
+=
1
# stop if we have enough examples
if
added_examples
==
number_of_examples_to_add
:
break
# populate mini-batch
for
i
in
range
(
self
.
batch_size
):
current_index
=
self
.
batch_size
-
i
-
1
# get the data example
selected_data
[
i
,
...]
=
self
.
data
[
self
.
indexes
[
current_index
],
...]
# normalization
selected_data
[
i
,
...]
=
self
.
normalize_sample
(
selected_data
[
i
,
...])
# label
selected_labels
.
append
(
self
.
labels
[
self
.
indexes
[
current_index
]])
# remove this example from the training set - used once in the epoch
new_indexes
=
numpy
.
delete
(
self
.
indexes
,
current_index
)
self
.
indexes
=
new_indexes
if
isinstance
(
selected_labels
,
list
):
selected_labels
=
numpy
.
array
(
selected_labels
)
# rebuild whole randomly shuffled training dataset
if
epoch_done
:
self
.
indexes
=
numpy
.
array
(
range
(
self
.
data
.
shape
[
0
]))
numpy
.
random
.
shuffle
(
self
.
indexes
)
return
[
selected_data
.
astype
(
"
float32
"
),
selected_labels
.
astype
(
"
int64
"
),
epoch_done
]
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