bigdl.util package¶
Submodules¶
bigdl.util.common module¶
-
class
bigdl.util.common.
EvaluatedResult
(result, total_num, method)[source]¶ A testing result used to benchmark the model quality.
-
class
bigdl.util.common.
JTensor
(storage, shape, bigdl_type='float', indices=None)[source]¶ Bases:
object
A wrapper to easy our work when need to pass or return Tensor to/from Scala.
>>> import numpy as np >>> from bigdl.util.common import JTensor >>> np.random.seed(123) >>>
-
classmethod
from_ndarray
(a_ndarray, bigdl_type='float')[source]¶ Convert a ndarray to a DenseTensor which would be used in Java side.
>>> import numpy as np >>> from bigdl.util.common import JTensor >>> from bigdl.util.common import callBigDlFunc >>> np.random.seed(123) >>> data = np.random.uniform(0, 1, (2, 3)).astype("float32") >>> result = JTensor.from_ndarray(data) >>> print(result) JTensor: storage: [[ 0.69646919 0.28613934 0.22685145] [ 0.55131477 0.71946895 0.42310646]], shape: [2 3], float >>> result JTensor: storage: [[ 0.69646919 0.28613934 0.22685145] [ 0.55131477 0.71946895 0.42310646]], shape: [2 3], float >>> data_back = result.to_ndarray() >>> (data == data_back).all() True >>> tensor1 = callBigDlFunc("float", "testTensor", JTensor.from_ndarray(data)) # noqa >>> array_from_tensor = tensor1.to_ndarray() >>> (array_from_tensor == data).all() True
-
classmethod
sparse
(a_ndarray, i_ndarray, shape, bigdl_type='float')[source]¶ Convert a three ndarray to SparseTensor which would be used in Java side. For example: a_ndarray = [1, 3, 2, 4] i_ndarray = [[0, 0, 1, 2], [0, 3, 2, 1]] shape = [3, 4] Present a dense tensor [[ 1, 0, 0, 3], [ 0, 0, 2, 0], [ 0, 4, 0, 0]]
:param a_ndarray non-zero elements in this SparseTensor :param i_ndarray zero-based indices for non-zero element i_ndarray’s shape should be (shape.size, a_ndarray.size) And the i-th non-zero elements indices is i_ndarray[:, 1] :param shape shape as a DenseTensor.
>>> import numpy as np >>> from bigdl.util.common import JTensor >>> from bigdl.util.common import callBigDlFunc >>> np.random.seed(123) >>> data = np.arange(1, 7).astype("float32") >>> indices = np.arange(1, 7) >>> shape = np.array([10]) >>> result = JTensor.sparse(data, indices, shape) >>> result JTensor: storage: [ 1. 2. 3. 4. 5. 6.], shape: [10] ,indices [1 2 3 4 5 6], float >>> tensor1 = callBigDlFunc("float", "testTensor", result) # noqa >>> array_from_tensor = tensor1.to_ndarray() >>> expected_ndarray = np.array([0, 1, 2, 3, 4, 5, 6, 0, 0, 0]) >>> (array_from_tensor == expected_ndarray).all() True
-
classmethod
-
class
bigdl.util.common.
Sample
(features, label, bigdl_type='float')[source]¶ Bases:
object
-
classmethod
from_jtensor
(features, label, bigdl_type='float')[source]¶ Convert a sequence of JTensor to Sample, which would be used in Java side. :param features: an JTensor or a list of JTensor :param label: an JTensor or a scalar :param bigdl_type: “double” or “float”
>>> import numpy as np >>> data = np.random.uniform(0, 1, (6)).astype("float32") >>> indices = np.arange(1, 7) >>> shape = np.array([10]) >>> feature0 = JTensor.sparse(data, indices, shape) >>> feature1 = JTensor.from_ndarray(np.random.uniform(0, 1, (2, 3)).astype("float32")) >>> sample = Sample.from_jtensor([feature0, feature1], 1)
-
classmethod
from_ndarray
(features, label, bigdl_type='float')[source]¶ Convert a ndarray of features and label to Sample, which would be used in Java side. :param features: an ndarray or a list of ndarrays :param label: an ndarray or a scalar :param bigdl_type: “double” or “float”
>>> import numpy as np >>> from bigdl.util.common import callBigDlFunc >>> from numpy.testing import assert_allclose >>> np.random.seed(123) >>> sample = Sample.from_ndarray(np.random.random((2,3)), np.random.random((2,3))) >>> sample_back = callBigDlFunc("float", "testSample", sample) >>> assert_allclose(sample.features[0].to_ndarray(), sample_back.features[0].to_ndarray()) >>> assert_allclose(sample.label.to_ndarray(), sample_back.label.to_ndarray()) >>> print(sample) Sample: features: [JTensor: storage: [[ 0.69646919 0.28613934 0.22685145] [ 0.55131477 0.71946895 0.42310646]], shape: [2 3], float], label: JTensor: storage: [[ 0.98076421 0.68482971 0.48093191] [ 0.39211753 0.343178 0.72904968]], shape: [2 3], float,
-
classmethod
-
bigdl.util.common.
get_spark_context
(conf=None)[source]¶ Get the current active spark context and create one if no active instance :param conf: combining bigdl configs into spark conf :return: SparkContext
-
bigdl.util.common.
redire_spark_logs
(bigdl_type='float', log_path='/var/jenkins_home/workspace/BigDL-Doc-Release/BigDL/pyspark/docs/bigdl.log')[source]¶ Redirect spark logs to the specified path. :param bigdl_type: “double” or “float” :param log_path: the file path to be redirected to; the default file is under the current workspace named bigdl.log.