Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/176 flatten #501

Merged
merged 22 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [#483](https://github.com/helmholtz-analytics/heat/pull/483) Bugfix: Underlying torch tensor moves to the right device on array initialisation
- [#483](https://github.com/helmholtz-analytics/heat/pull/483) Bugfix:DNDarray.cpu() changes heat device to cpu
- Update documentation theme to "Read the Docs"
- [#501](https://github.com/helmholtz-analytics/heat/pull/501) New Feature: flatten

# v0.3.0

Expand Down
41 changes: 41 additions & 0 deletions heat/core/manipulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"diag",
"diagonal",
"expand_dims",
"flatten",
"hstack",
"resplit",
"sort",
Expand Down Expand Up @@ -556,6 +557,46 @@ def expand_dims(a, axis):
)


def flatten(a):
"""
Flattens an array into one dimension

Parameters
----------
a : DNDarray
array to collapse
Returns
-------
ret : DNDarray
flattened copy
Examples
--------
>>> a = ht.array([[[1,2],[3,4]],[[5,6],[7,8]]])
>>> ht.flatten(a)
tensor([1,2,3,4,5,6,7,8])
"""
if a.split is None:
return factories.array(
torch.flatten(a._DNDarray__array),
dtype=a.dtype,
is_split=None,
device=a.device,
comm=a.comm,
)

a = resplit(a, 0)
a = factories.array(
torch.flatten(a._DNDarray__array),
dtype=a.dtype,
is_split=a.split,
device=a.device,
comm=a.comm,
)
a.balance_()
coquelin77 marked this conversation as resolved.
Show resolved Hide resolved

return a


def hstack(tup):
"""
Stack arrays in sequence horizontally (column wise).
Expand Down
25 changes: 25 additions & 0 deletions heat/core/tests/test_manipulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,31 @@ def test_expand_dims(self):
with self.assertRaises(ValueError):
ht.empty((3, 4, 5), device=ht_device).expand_dims(-5)

def test_flatten(self):
a = ht.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], device=ht_device)
res = ht.array([1, 2, 3, 4, 5, 6, 7, 8], device=ht_device)
self.assertTrue(ht.equal(ht.flatten(a), res))

a = ht.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], split=0, device=ht_device, dtype=ht.int8)
res = ht.array([1, 2, 3, 4, 5, 6, 7, 8], split=0, device=ht_device, dtype=ht.int8)
self.assertTrue(ht.equal(ht.flatten(a), res))

a = ht.array(
[[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]], split=1, device=ht_device
)
res = ht.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0], split=0, device=ht_device)
self.assertTrue(ht.equal(ht.flatten(a), res))

a = ht.array(
[[[False, False], [False, True]], [[True, False], [True, True]]],
split=2,
device=ht_device,
)
res = ht.array(
[False, False, False, True, True, False, True, True], split=0, device=ht_device
)
self.assertTrue(ht.equal(ht.flatten(a), res))

def test_hstack(self):
# cases to test:
# MM===================================
Expand Down