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

add random_sample and aliases #664

Merged
merged 5 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- [#652](https://github.com/helmholtz-analytics/heat/pull/652) Feature: benchmark scripts and jobscript generation
- [#653](https://github.com/helmholtz-analytics/heat/pull/653) Printing above threshold gathers the data without a buffer now
- [#653](https://github.com/helmholtz-analytics/heat/pull/653) Bugfixes: Update unittests argmax & argmin + force index order in mpi_argmax & mpi_argmin. Add device parameter for tensor creation in dndarray.get_halo().
- [#664](https://github.com/helmholtz-analytics/heat/pull/664) New feature / enhancement: `random.random_sample`, `random.random`, `random.sample`, `random.ranf`, `random.random_integer`
- [#667](https://github.com/helmholtz-analytics/heat/pull/667) Enhancement `reshape`: rename axis parameter

# v0.4.0
Expand Down
43 changes: 43 additions & 0 deletions heat/core/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from . import dndarray
from . import stride_tricks
from . import types
from typing import Type, List, Optional, Tuple


# introduce the global random state variables, will be correctly initialized at the end of file
Expand Down Expand Up @@ -374,6 +375,10 @@ def randint(low, high=None, size=None, dtype=None, split=None, device=None, comm
return dndarray.DNDarray(values, shape, dtype, split, device, comm)


# alias
random_integer = randint


def randn(*args, dtype=types.float32, split=None, device=None, comm=None):
"""
Returns a tensor filled with random numbers from a standard normal distribution with zero mean and variance of one.
Expand Down Expand Up @@ -422,6 +427,44 @@ def randn(*args, dtype=types.float32, split=None, device=None, comm=None):
return normal_tensor


def random_sample(
shape: Optional[Tuple[int]] = None,
dtype=types.float32,
split: Optional[int] = None,
device: Optional[str] = None,
comm=None,
):
"""
Random values in a given shape.
Create a :class:`~heat.core.dndarray.DNDarray` of the given shape and populate it with random samples from a
uniform distribution over [0, 1).

Parameters
mtar marked this conversation as resolved.
Show resolved Hide resolved
----------
shape : Tuple[int]
The shape of the returned array, should all be positive. If no argument is given a single random sample is
generated.
dtype: Type[datatype], optional
The datatype of the returned values. Has to be one of
[:class:`~heat.core.types.float32, :class:`~heat.core.types.float64`].
split: int, optional
The axis along which the array is split and distributed, defaults to no distribution.
device : str, optional
Specifies the :class:`~heat.core.devices.Device` the array shall be allocated on, defaults to globally
set default device.
comm: Communication, optional
Handle to the nodes holding distributed parts or copies of this array.
"""
if not shape:
shape = (1,)
shape = stride_tricks.sanitize_shape(shape)
return rand(*shape, dtype=dtype, split=split, device=device, comm=comm)


# aliases
random = ranf = sample = random_sample


def seed(seed=None):
"""
Seed the generator.
Expand Down
30 changes: 30 additions & 0 deletions heat/core/tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ def test_randint(self):
self.assertTrue(4900 < median < 5100)
self.assertTrue(std < 2900)

# test aliases
ht.random.seed(234)
a = ht.random.randint(10, 50)
ht.random.seed(234)
b = ht.random.random_integer(10, 50)
self.assertTrue(ht.equal(a, b))

def test_randn(self):
# Test that the random values have the correct distribution
ht.random.seed(54321)
Expand Down Expand Up @@ -310,6 +317,29 @@ def test_randn(self):
self.assertFalse(np.allclose(a, c))
self.assertFalse(np.allclose(b, c))

def test_random_sample(self):
# short test
# compare random and aliases with rand
ht.random.seed(534)
a = ht.random.rand(6, 2, 3)
ht.random.seed(534)
b = ht.random.random((6, 2, 3))
ht.random.seed(534)
c = ht.random.random_sample((6, 2, 3))
ht.random.seed(534)
d = ht.random.ranf((6, 2, 3))
ht.random.seed(534)
e = ht.random.sample((6, 2, 3))

self.assertTrue(ht.equal(a, b))
self.assertTrue(ht.equal(a, c))
self.assertTrue(ht.equal(a, d))
self.assertTrue(ht.equal(a, e))

# empty input
a = ht.random.random_sample()
self.assertEqual(a.shape, (1,))

def test_set_state(self):
ht.random.set_state(("Threefry", 12345, 0xFFF))
self.assertEqual(ht.random.get_state(), ("Threefry", 12345, 0xFFF, 0, 0.0))
Expand Down