-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from fszewczyk/dataset
Dataset and DataLoader
- Loading branch information
Showing
11 changed files
with
273 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
build/ | ||
data/ | ||
|
||
docs/html | ||
docs/latex | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* Copyright © 2023 Franciszek Szewczyk. None of the rights reserved. | ||
* This code is released under the Beerware License. If you find this code useful or you appreciate the work, you are | ||
* encouraged to buy the author a beer in return. | ||
* Contact the author at [email protected] for inquiries and support. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "Dataset.hpp" | ||
|
||
namespace shkyera { | ||
|
||
template <typename T, typename U> class DataLoader { | ||
private: | ||
const Dataset<T, U> &_dataset; | ||
size_t _batchSize; | ||
bool _shuffle; | ||
|
||
public: | ||
DataLoader(const Dataset<T, U> &dataset, size_t batchSize = 4, bool shuffle = false); | ||
|
||
size_t getTotalBatches() const; | ||
|
||
class ConstIterator { | ||
private: | ||
std::vector<size_t> _order; | ||
size_t _index; | ||
|
||
const DataLoader<T, U> &_dataLoader; | ||
|
||
public: | ||
ConstIterator(size_t index, const DataLoader<T, U> &dataLoader); | ||
|
||
std::pair<std::vector<T>, std::vector<U>> operator*(); | ||
ConstIterator &operator++(); | ||
bool operator!=(const ConstIterator &other); | ||
}; | ||
|
||
ConstIterator begin() const; | ||
ConstIterator end() const; | ||
}; | ||
|
||
template <typename T, typename U> | ||
DataLoader<T, U>::DataLoader(const Dataset<T, U> &dataset, size_t batchSize, bool shuffle) | ||
: _dataset(dataset), _batchSize(batchSize), _shuffle(shuffle) {} | ||
|
||
template <typename T, typename U> size_t DataLoader<T, U>::getTotalBatches() const { | ||
size_t batches = _dataset.size() / _batchSize; | ||
if (_dataset.size() % _batchSize != 0) | ||
batches++; | ||
return batches; | ||
} | ||
|
||
template <typename T, typename U> | ||
DataLoader<T, U>::ConstIterator::ConstIterator(size_t index, const DataLoader<T, U> &dataLoader) | ||
: _index(index), _dataLoader(dataLoader) { | ||
_order.resize(_dataLoader._dataset.size(), 0); | ||
|
||
std::iota(_order.begin(), _order.end(), 0); | ||
if (_dataLoader._shuffle) | ||
utils::shuffle(_order); | ||
} | ||
|
||
template <typename T, typename U> | ||
std::pair<std::vector<T>, std::vector<U>> DataLoader<T, U>::ConstIterator::operator*() { | ||
size_t beginIndex = _index; | ||
size_t endIndex = std::min(_index + _dataLoader._batchSize, _dataLoader._dataset.size()); | ||
|
||
std::vector<T> inputs(endIndex - beginIndex); | ||
std::vector<U> outputs(endIndex - beginIndex); | ||
|
||
for (size_t i = beginIndex; i < endIndex; ++i) { | ||
auto [in, out] = _dataLoader._dataset[_order[i]]; | ||
inputs[i - beginIndex] = in; | ||
outputs[i - beginIndex] = out; | ||
} | ||
|
||
return {inputs, outputs}; | ||
} | ||
|
||
template <typename T, typename U> | ||
typename DataLoader<T, U>::ConstIterator &DataLoader<T, U>::ConstIterator::operator++() { | ||
_index += _dataLoader._batchSize; | ||
_index = std::min(_index, _dataLoader._dataset.size()); | ||
return *this; | ||
} | ||
|
||
template <typename T, typename U> bool DataLoader<T, U>::ConstIterator::operator!=(const ConstIterator &other) { | ||
return _index != other._index; | ||
} | ||
|
||
template <typename T, typename U> typename DataLoader<T, U>::ConstIterator DataLoader<T, U>::begin() const { | ||
return ConstIterator(0, *this); | ||
} | ||
|
||
template <typename T, typename U> typename DataLoader<T, U>::ConstIterator DataLoader<T, U>::end() const { | ||
return ConstIterator(_dataset.size(), *this); | ||
} | ||
|
||
} // namespace shkyera |
Oops, something went wrong.