[python-arrayfire] 42/58: Adding random engine class and relavent methods
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Wed Sep 28 13:57:07 UTC 2016
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to branch master
in repository python-arrayfire.
commit fe18684550281f9fedfcaf7a76badba177a14aaa
Author: Pavan Yalamanchili <contact at pavanky.com>
Date: Fri Sep 23 14:34:55 2016 -0700
Adding random engine class and relavent methods
- Also added necessary tests
---
arrayfire/data.py | 100 +----------------
arrayfire/features.py | 2 +
arrayfire/random.py | 232 +++++++++++++++++++++++++++++++++++++++
arrayfire/tests/simple/data.py | 10 --
arrayfire/tests/simple/random.py | 38 +++++++
5 files changed, 273 insertions(+), 109 deletions(-)
diff --git a/arrayfire/data.py b/arrayfire/data.py
index 0df045f..786d649 100644
--- a/arrayfire/data.py
+++ b/arrayfire/data.py
@@ -16,6 +16,7 @@ from .library import *
from .array import *
from .util import *
from .util import _is_number
+from .random import randu, randn, set_seed, get_seed
def constant(val, d0, d1=None, d2=None, d3=None, dtype=Dtype.f32):
"""
@@ -186,105 +187,6 @@ def iota(d0, d1=None, d2=None, d3=None, dim=-1, tile_dims=None, dtype=Dtype.f32)
4, ct.pointer(tdims), dtype.value))
return out
-def randu(d0, d1=None, d2=None, d3=None, dtype=Dtype.f32):
- """
- Create a multi dimensional array containing values from a uniform distribution.
-
- Parameters
- ----------
- d0 : int.
- Length of first dimension.
-
- d1 : optional: int. default: None.
- Length of second dimension.
-
- d2 : optional: int. default: None.
- Length of third dimension.
-
- d3 : optional: int. default: None.
- Length of fourth dimension.
-
- dtype : optional: af.Dtype. default: af.Dtype.f32.
- Data type of the array.
-
- Returns
- -------
-
- out : af.Array
- Multi dimensional array whose elements are sampled uniformly between [0, 1].
- - If d1 is None, `out` is 1D of size (d0,).
- - If d1 is not None and d2 is None, `out` is 2D of size (d0, d1).
- - If d1 and d2 are not None and d3 is None, `out` is 3D of size (d0, d1, d2).
- - If d1, d2, d3 are all not None, `out` is 4D of size (d0, d1, d2, d3).
- """
- out = Array()
- dims = dim4(d0, d1, d2, d3)
-
- safe_call(backend.get().af_randu(ct.pointer(out.arr), 4, ct.pointer(dims), dtype.value))
- return out
-
-def randn(d0, d1=None, d2=None, d3=None, dtype=Dtype.f32):
- """
- Create a multi dimensional array containing values from a normal distribution.
-
- Parameters
- ----------
- d0 : int.
- Length of first dimension.
-
- d1 : optional: int. default: None.
- Length of second dimension.
-
- d2 : optional: int. default: None.
- Length of third dimension.
-
- d3 : optional: int. default: None.
- Length of fourth dimension.
-
- dtype : optional: af.Dtype. default: af.Dtype.f32.
- Data type of the array.
-
- Returns
- -------
-
- out : af.Array
- Multi dimensional array whose elements are sampled from a normal distribution with mean 0 and sigma of 1.
- - If d1 is None, `out` is 1D of size (d0,).
- - If d1 is not None and d2 is None, `out` is 2D of size (d0, d1).
- - If d1 and d2 are not None and d3 is None, `out` is 3D of size (d0, d1, d2).
- - If d1, d2, d3 are all not None, `out` is 4D of size (d0, d1, d2, d3).
- """
-
- out = Array()
- dims = dim4(d0, d1, d2, d3)
-
- safe_call(backend.get().af_randn(ct.pointer(out.arr), 4, ct.pointer(dims), dtype.value))
- return out
-
-def set_seed(seed=0):
- """
- Set the seed for the random number generator.
-
- Parameters
- ----------
- seed: int.
- Seed for the random number generator
- """
- safe_call(backend.get().af_set_seed(ct.c_ulonglong(seed)))
-
-def get_seed():
- """
- Get the seed for the random number generator.
-
- Returns
- ----------
- seed: int.
- Seed for the random number generator
- """
- seed = ct.c_ulonglong(0)
- safe_call(backend.get().af_get_seed(ct.pointer(seed)))
- return seed.value
-
def identity(d0, d1, d2=None, d3=None, dtype=Dtype.f32):
"""
Create an identity matrix or batch of identity matrices.
diff --git a/arrayfire/features.py b/arrayfire/features.py
index 21705e2..6e006af 100644
--- a/arrayfire/features.py
+++ b/arrayfire/features.py
@@ -6,9 +6,11 @@
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################
+
"""
Features class used for Computer Vision algorithms.
"""
+
from .library import *
from .array import *
import numbers
diff --git a/arrayfire/random.py b/arrayfire/random.py
new file mode 100644
index 0000000..60148a8
--- /dev/null
+++ b/arrayfire/random.py
@@ -0,0 +1,232 @@
+#######################################################
+# Copyright (c) 2015, ArrayFire
+# All rights reserved.
+#
+# This file is distributed under 3-clause BSD license.
+# The complete license agreement can be obtained at:
+# http://arrayfire.com/licenses/BSD-3-Clause
+########################################################
+
+"""
+Random engine class and functions to generate random numbers.
+"""
+
+from .library import *
+from .array import *
+import numbers
+
+class Random_Engine(object):
+ """
+ Class to handle random number generator engines.
+
+ Parameters
+ ----------
+
+ engine_type : optional: RANDOME_ENGINE. default: RANDOM_ENGINE.PHILOX
+ - Specifies the type of random engine to be created. Can be one of:
+ - RANDOM_ENGINE.PHILOX_4X32_10
+ - RANDOM_ENGINE.THREEFRY_2X32_16
+ - RANDOM_ENGINE.MERSENNE_GP11213
+ - RANDOM_ENGINE.PHILOX (same as RANDOM_ENGINE.PHILOX_4X32_10)
+ - RANDOM_ENGINE.THREEFRY (same as RANDOM_ENGINE.THREEFRY_2X32_16)
+ - RANDOM_ENGINE.DEFAULT
+ - Not used if engine is not None
+
+ seed : optional int. default: 0
+ - Specifies the seed for the random engine
+ - Not used if engine is not None
+
+ engine : optional ctypes.c_void_p. default: None.
+ - Used a handle created by the C api to create the Random_Engine.
+ """
+
+ def __init__(self, engine_type = RANDOM_ENGINE.PHILOX, seed = 0, engine = None):
+ if (engine is None):
+ self.engine = ct.c_void_p(0)
+ safe_call(backend.get().af_create_random_engine(ct.pointer(self.engine), engine_type.value, ct.c_longlong(seed)))
+ else:
+ self.engine = engine
+
+ def __del__(self):
+ safe_call(backend.get().af_release_random_engine(self.engine))
+
+ def set_type(self, engine_type):
+ """
+ Set the type of the random engine.
+ """
+ safe_call(backend.get().af_random_engine_set_type(ct.pointer(self.engine), engine_type.value))
+
+ def get_type(self):
+ """
+ Get the type of the random engine.
+ """
+ __to_random_engine_type = [RANDOM_ENGINE.PHILOX_4X32_10,
+ RANDOM_ENGINE.THREEFRY_2X32_16,
+ RANDOM_ENGINE.MERSENNE_GP11213]
+ rty = ct.c_int(RANDOM_ENGINE.PHILOX.value)
+ safe_call(backend.get().af_random_engine_get_type(ct.pointer(rty), self.engine))
+ return __to_random_engine_type[rty]
+
+ def set_seed(self, seed):
+ """
+ Set the seed for the random engine.
+ """
+ safe_call(backend.get().af_random_engine_set_seed(ct.pointer(self.engine), ct.c_longlong(seed)))
+
+ def get_seed(self):
+ """
+ Get the seed for the random engine.
+ """
+ seed = ct.c_longlong(0)
+ safe_call(backend.get().af_random_engine_get_seed(ct.pointer(seed), self.engine))
+ return seed.value
+
+def randu(d0, d1=None, d2=None, d3=None, dtype=Dtype.f32, random_engine=None):
+ """
+ Create a multi dimensional array containing values from a uniform distribution.
+
+ Parameters
+ ----------
+ d0 : int.
+ Length of first dimension.
+
+ d1 : optional: int. default: None.
+ Length of second dimension.
+
+ d2 : optional: int. default: None.
+ Length of third dimension.
+
+ d3 : optional: int. default: None.
+ Length of fourth dimension.
+
+ dtype : optional: af.Dtype. default: af.Dtype.f32.
+ Data type of the array.
+
+ random_engine : optional: Random_Engine. default: None.
+ If random_engine is None, uses a default engine created by arrayfire.
+
+ Returns
+ -------
+
+ out : af.Array
+ Multi dimensional array whose elements are sampled uniformly between [0, 1].
+ - If d1 is None, `out` is 1D of size (d0,).
+ - If d1 is not None and d2 is None, `out` is 2D of size (d0, d1).
+ - If d1 and d2 are not None and d3 is None, `out` is 3D of size (d0, d1, d2).
+ - If d1, d2, d3 are all not None, `out` is 4D of size (d0, d1, d2, d3).
+ """
+ out = Array()
+ dims = dim4(d0, d1, d2, d3)
+
+ if random_engine is None:
+ safe_call(backend.get().af_randu(ct.pointer(out.arr), 4, ct.pointer(dims), dtype.value))
+ else:
+ safe_call(backend.get().af_random_uniform(ct.pointer(out.arr), 4, ct.pointer(dims), random_engine.engine))
+
+ return out
+
+def randn(d0, d1=None, d2=None, d3=None, dtype=Dtype.f32, random_engine=None):
+ """
+ Create a multi dimensional array containing values from a normal distribution.
+
+ Parameters
+ ----------
+ d0 : int.
+ Length of first dimension.
+
+ d1 : optional: int. default: None.
+ Length of second dimension.
+
+ d2 : optional: int. default: None.
+ Length of third dimension.
+
+ d3 : optional: int. default: None.
+ Length of fourth dimension.
+
+ dtype : optional: af.Dtype. default: af.Dtype.f32.
+ Data type of the array.
+
+ random_engine : optional: Random_Engine. default: None.
+ If random_engine is None, uses a default engine created by arrayfire.
+
+ Returns
+ -------
+
+ out : af.Array
+ Multi dimensional array whose elements are sampled from a normal distribution with mean 0 and sigma of 1.
+ - If d1 is None, `out` is 1D of size (d0,).
+ - If d1 is not None and d2 is None, `out` is 2D of size (d0, d1).
+ - If d1 and d2 are not None and d3 is None, `out` is 3D of size (d0, d1, d2).
+ - If d1, d2, d3 are all not None, `out` is 4D of size (d0, d1, d2, d3).
+ """
+
+ out = Array()
+ dims = dim4(d0, d1, d2, d3)
+
+ if random_engine is None:
+ safe_call(backend.get().af_randn(ct.pointer(out.arr), 4, ct.pointer(dims), dtype.value))
+ else:
+ safe_call(backend.get().af_random_normal(ct.pointer(out.arr), 4, ct.pointer(dims), random_engine.engine))
+
+ return out
+
+def set_seed(seed=0):
+ """
+ Set the seed for the random number generator.
+
+ Parameters
+ ----------
+ seed: int.
+ Seed for the random number generator
+ """
+ safe_call(backend.get().af_set_seed(ct.c_ulonglong(seed)))
+
+def get_seed():
+ """
+ Get the seed for the random number generator.
+
+ Returns
+ ----------
+ seed: int.
+ Seed for the random number generator
+ """
+ seed = ct.c_ulonglong(0)
+ safe_call(backend.get().af_get_seed(ct.pointer(seed)))
+ return seed.value
+
+def set_default_random_engine_type(engine_type):
+ """
+ Set random engine type for default random engine.
+
+ Parameters
+ ----------
+ engine_type : RANDOME_ENGINE.
+ - Specifies the type of random engine to be created. Can be one of:
+ - RANDOM_ENGINE.PHILOX_4X32_10
+ - RANDOM_ENGINE.THREEFRY_2X32_16
+ - RANDOM_ENGINE.MERSENNE_GP11213
+ - RANDOM_ENGINE.PHILOX (same as RANDOM_ENGINE.PHILOX_4X32_10)
+ - RANDOM_ENGINE.THREEFRY (same as RANDOM_ENGINE.THREEFRY_2X32_16)
+ - RANDOM_ENGINE.DEFAULT
+
+ Note
+ ----
+
+ This only affects randu and randn when a random engine is not specified.
+ """
+ safe_call(backend.get().af_set_default_random_engine_type(ct.pointer(self.engine), engine_type.value))
+
+def get_default_random_engine():
+ """
+ Get the default random engine
+
+ Returns
+ ------
+
+ The default random engine used by randu and randn
+ """
+ engine = ct.c_void_p(0)
+ default_engine = ct.c_void_p(0)
+ safe_call(backend.get().af_get_default_random_engine(ct.pointer(default_engine)))
+ safe_call(backend.get().af_retain_random_engine(ct.pointer(engine), default_engine))
+ return Random_Engine(engine=engine)
diff --git a/arrayfire/tests/simple/data.py b/arrayfire/tests/simple/data.py
index 7c95a81..86b900b 100644
--- a/arrayfire/tests/simple/data.py
+++ b/arrayfire/tests/simple/data.py
@@ -24,16 +24,6 @@ def simple_data(verbose=False):
display_func(af.range(3, 3))
display_func(af.iota(3, 3, tile_dims=(2,2)))
- display_func(af.randu(3, 3, 1, 2))
- display_func(af.randu(3, 3, 1, 2, af.Dtype.b8))
- display_func(af.randu(3, 3, dtype=af.Dtype.c32))
-
- display_func(af.randn(3, 3, 1, 2))
- display_func(af.randn(3, 3, dtype=af.Dtype.c32))
-
- af.set_seed(1024)
- assert(af.get_seed() == 1024)
-
display_func(af.identity(3, 3, 1, 2, af.Dtype.b8))
display_func(af.identity(3, 3, dtype=af.Dtype.c32))
diff --git a/arrayfire/tests/simple/random.py b/arrayfire/tests/simple/random.py
new file mode 100644
index 0000000..6b72a91
--- /dev/null
+++ b/arrayfire/tests/simple/random.py
@@ -0,0 +1,38 @@
+#!/usr/bin/python
+#######################################################
+# Copyright (c) 2015, ArrayFire
+# All rights reserved.
+#
+# This file is distributed under 3-clause BSD license.
+# The complete license agreement can be obtained at:
+# http://arrayfire.com/licenses/BSD-3-Clause
+########################################################
+
+import arrayfire as af
+from . import _util
+
+def simple_random(verbose=False):
+ display_func = _util.display_func(verbose)
+ print_func = _util.print_func(verbose)
+
+ display_func(af.randu(3, 3, 1, 2))
+ display_func(af.randu(3, 3, 1, 2, af.Dtype.b8))
+ display_func(af.randu(3, 3, dtype=af.Dtype.c32))
+
+ display_func(af.randn(3, 3, 1, 2))
+ display_func(af.randn(3, 3, dtype=af.Dtype.c32))
+
+ af.set_seed(1024)
+ assert(af.get_seed() == 1024)
+
+ engine = Random_Engine(RANDOM_ENGINE.MERSENNE_GP11213, 100)
+
+ display_func(af.randu(3, 3, 1, 2, engine=engine))
+ display_func(af.randu(3, 3, 1, 2, af.Dtype.s32, engine=engine))
+ display_func(af.randu(3, 3, dtype=af.Dtype.c32, engine=engine))
+
+ display_func(af.randn(3, 3, engine=engine))
+ engine.set_seed(100)
+ assert(engine.get_seed() == 100)
+
+_util.tests['random'] = simple_random
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-arrayfire.git
More information about the debian-science-commits
mailing list