def test_py2java_array(self, java_sc, py_sc): data = [np.random.random((32, 20)) for _ in range(100)] jdata = [jp.array(x) for x in data] # required data = [Dataset(x, x) for x in data] py_rdd = py_sc.parallelize(data) java_rdd = py2javaDatasetRDD(py_rdd, java_sc) data2 = java_rdd.collect() data2 = [data2.get(i) for i in range(data2.size())] assert len(data) == len(data2) for d1, d2 in zip(data, data2): d2 = jp.array(d2.getFeatures()).numpy() assert_allclose(d1.features.numpy(), d2)
def _test_ufunc_inplace(op, shape1, shape2): a_np = np.random.random(shape1) b_np = np.random.random(shape2) a_np2 = a_np.copy() exec('a_np {}= b_np'.format(op)) a_jp = jp.array(a_np2) b_jp = jp.array(b_np) exec('a_jp {}= b_jp'.format(op)) a_jp = a_jp.numpy() assert_allclose(a_jp, a_np)
def _test_ufunc(op, shape1, shape2): a_np = np.random.random(shape1) b_np = np.random.random(shape2) c_np = eval('a_np {} b_np'.format(op)) a_jp = jp.array(a_np) b_jp = jp.array(b_np) c_jp = eval('a_jp {} b_jp'.format(op)) c_jp = c_jp.numpy() assert_allclose(c_jp, c_np)
def test_stack(): shapes = [(2, 3), (2, 3, 4)] for shape in shapes: x1_np = np.random.random(shape) x2_np = np.random.random(shape) x1_jp = jp.array(x1_np) x2_jp = jp.array(x2_np) for axis in range(len(shape)): y_np = np.stack([x1_np, x2_np], axis) y_jp = jp.stack([x1_jp, x2_jp], axis) assert y_jp.shape == y_np.shape
def test_concatenate(): shapes = [[(2, 3, 4), (3, 3, 4), 0], [(2, 3, 5), (2, 4, 5), 1], [(3, 2, 4), (3, 2, 2), 2]] for shape in shapes: x1_np = np.random.random(shape[0]) x2_np = np.random.random(shape[1]) x1_jp = jp.array(x1_np) x2_jp = jp.array(x2_np) axis = shape[2] y_np = np.concatenate([x1_np, x2_np], axis) y_jp = jp.concatenate([x1_jp, x2_jp], axis) assert y_jp.shape == y_np.shape
def test_stack(): shapes = [ (2, 3), (2, 3, 4) ] for shape in shapes: x1_np = np.random.random(shape) x2_np = np.random.random(shape) x1_jp = jp.array(x1_np) x2_jp = jp.array(x2_np) for axis in range(len(shape)): y_np = np.stack([x1_np, x2_np], axis) y_jp = jp.stack([x1_jp, x2_jp], axis) assert y_jp.shape == y_np.shape
def test_expand_dims(): shapes = [(2, 3), (2, 1), (2, 3, 4)] for shape in shapes: x_np = np.random.random(shape) x_jp = jp.array(x_np) for axis in range(len(shape) + 1): y_np = np.expand_dims(x_np, axis) y_jp = jp.expand_dims(x_jp, axis) assert y_jp.shape == y_np.shape
def test_arr_creation(self): x_np = np.random.random((100, 32, 16)) x_jp = jp.array(x_np) x_np_2 = x_jp.numpy() self.assertEquals(x_np.shape, x_jp.shape) self.assertEquals(x_np.shape, x_np_2.shape) x_np = x_np.ravel() x_np_2 = x_np_2.ravel() assertEquals(list(x_np), list(x_np_2))
def test_java2py_array(self, java_sc, py_sc): data = ArrayList() for _ in range(100): arr = jp.array(np.random.random((32, 20))).array data.add(arr) java_rdd = java_sc.parallelize(data) py_rdd = java2pyArrayRDD(java_rdd, py_sc) data2 = py_rdd.collect() data = [data.get(i) for i in range(data.size())] assert len(data) == len(data2) for d1, d2 in zip(data, data2): assert_allclose(jp.array(d1).numpy(), d2)
def test_squeeze(): shapes = [[2, 3, 1, 4], [2, 1, 3]] for shape in shapes: x_np = np.random.random(shape) x_jp = jp.array(x_np) axis = shape.index(1) y_np = np.squeeze(x_np, axis) y_jp = jp.squeeze(x_jp, axis) assert y_jp.shape == y_np.shape
def test_conversion_64(): jp.set_context_dtype('float64') shapes = [(1, 1), (2, 1), (1, 2), (32, 12), (100, 32, 16)] for shape in shapes: x_np = np.random.random(shape) x_jp = jp.array(x_np) x_np += np.random.random(shape) x_jp = x_jp.numpy() assert_allclose(x_jp, x_np)
def _test_reduction_op(op, shape): for axis in range(len(shape)): x_np = np.random.random(shape) y_np = getattr(np, op)(x_np, axis=axis) x_jp = jp.array(x_np) y_jp = getattr(jp, op)(x_jp, axis=axis) x_jp = x_jp.numpy() assert_allclose(x_jp, x_np)
def test_concatenate(): shapes = [ [(2, 3, 4), (3, 3, 4), 0], [(2, 3, 5), (2, 4, 5), 1], [(3, 2, 4), (3, 2, 2), 2] ] for shape in shapes: x1_np = np.random.random(shape[0]) x2_np = np.random.random(shape[1]) x1_jp = jp.array(x1_np) x2_jp = jp.array(x2_np) axis = shape[2] y_np = np.concatenate([x1_np, x2_np], axis) y_jp = jp.concatenate([x1_jp, x2_jp], axis) assert y_jp.shape == y_np.shape
def __init__(self, n=1000): print 'Running tests with [', n, 'x', n, '] dimensionality' self.n = n self.m = 200 self.np_arr = [] self.nd4j_arr = [] for counter in range(0, self.m + 1): self.np_arr.append(np.linspace(1, n * n, n * n).reshape((n, n))) for counter in range(0, self.m + 1): self.nd4j_arr.append(jp.array(self.np_arr[counter]))
def test_transpose(): shapes = [(2, 3), (3, 1), (2, 3, 4)] for shape in shapes: x_np = np.random.random(shape) x_jp = jp.array(x_np) y_np = np.transpose(x_np) y_jp = jp.transpose(x_jp) y_jp = y_jp.numpy() assert y_jp.shape == y_np.shape
def test_permute(): shapes = [[(2, 3), [0, 1], [1, 0]], [(2, 1), [0, 1], [1, 0]], [(2, 3, 4), [0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]] for shape in shapes: x_np = np.random.random(shape[0]) x_jp = jp.array(x_np) for dims in shape[1:]: y_np = np.transpose(x_np, dims) y_jp = jp.transpose(x_jp, dims) assert y_jp.shape == y_np.shape
def test_permute(): shapes = [] shapes.append([(2, 3), [0, 1], [1, 0]]) shapes.append([(2, 1), [0, 1], [1, 0]]) shapes.append([(2, 3, 4), [0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]) for shape in shapes: x_np = np.random.random(shape[0]) x_jp = jp.array(x_np) for dims in shape[1:]: y_np = np.transpose(x_np, dims) y_jp = jp.transpose(x_jp, dims) assert y_jp.shape == y_np.shape
def test_reshape(): jp.set_context_dtype('float64') shapes = [[(2, 3), (6, 1)], [(1, 2, 3), (3, 2)], [(3, 2, 1), (2, -1)], [(3, 1, 2), (-1, 3, 1)]] for shape1, shape2 in shapes: x_np = np.random.random(shape1) y_np = np.reshape(x_np, shape2) x_jp = jp.array(x_np) y_jp = jp.reshape(x_jp, shape2) assert y_jp.shape == y_np.shape
def test_tile(): shapes = [(2, 3), (2, 3, 4)] repeats = [[3, 2], [3, 2, 2]] for i in range(len(shapes)): shape = shapes[i] rep = repeats[i] x_np = np.random.random(shape) x_jp = jp.array(x_np) y_np = np.tile(x_np, rep) y_jp = jp.tile(x_jp, rep) assert y_jp.shape == y_np.shape
def test_reshape(): jp.set_context_dtype('float64') shapes = [ [(2, 3), (6, 1)], [(1, 2, 3), (3, 2)], [(3, 2, 1), (2, -1)], [(3, 1, 2), (-1, 3, 1)] ] for shape1, shape2 in shapes: x_np = np.random.random(shape1) y_np = np.reshape(x_np, shape2) x_jp = jp.array(x_np) y_jp = jp.reshape(x_jp, shape2) assert y_jp.shape == y_np.shape
def test_tile(): shapes = [ (2, 3), (2, 3, 4) ] repeats = [ [3, 2], [3, 2, 2] ] for i in range(len(shapes)): shape = shapes[i] rep = repeats[i] x_np = np.random.random(shape) x_jp = jp.array(x_np) y_np = np.tile(x_np, rep) y_jp = jp.tile(x_jp, rep) assert y_jp.shape == y_np.shape
# # This program and the accompanying materials are made available under the # terms of the Apache License, Version 2.0 which is available at # https://www.apache.org/licenses/LICENSE-2.0. # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. # # SPDX-License-Identifier: Apache-2.0 ################################################################################ import jumpy as jp import numpy as np # You can convert numpy arrays to jumpy and vice versa # Convert numpy to jumpy: np_array = np.zeros((32, 100)) jp_array = jp.array(np_array) # Convert jumpy to numpy: jp_array = jp.zeros((32, 100)) np_array = jp_array.numpy()
def my_broadcast_op(x, y): # x and y are INDArrays x = array(x) # x is now a jumpy array y = array(y) # y is now a jumpy array return 3 * x + 4 * y
def setUp(self): self.x_np = np.random.random((100, 32, 16)) self.x_jp = jp.array(self.x_np) self.x_np_2 = x_jp.numpy()