def test_gpu_numpy(self): if swig_paddle.isGpuVersion(): vec = swig_paddle.IVector.create(range(0, 10), True) assert isinstance(vec, swig_paddle.IVector) self.assertTrue(vec.isGpu()) self.assertEqual(vec.getData(), range(0, 10)) num_arr = vec.copyToNumpyArray() assert isinstance(num_arr, np.ndarray) # for code hint. num_arr[4] = 7 self.assertEquals(vec.getData(), range(0, 10)) vec.copyFromNumpyArray(num_arr) expect_vec = range(0, 10) expect_vec[4] = 7 self.assertEqual(vec.getData(), expect_vec)
def test_numpyGpu(self): if swig_paddle.isGpuVersion(): numpy_mat = np.matrix([[1, 2], [3, 4], [5, 6]], dtype='float32') gpu_m = swig_paddle.Matrix.createGpuDenseFromNumpy(numpy_mat) assert isinstance(gpu_m, swig_paddle.Matrix) self.assertEqual((int(gpu_m.getHeight()), int(gpu_m.getWidth())), numpy_mat.shape) self.assertTrue(gpu_m.isGpu()) numpy_mat = gpu_m.copyToNumpyMat() numpy_mat[0, 1] = 3.23 for a, e in zip(gpu_m.getData(), [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]): self.assertAlmostEqual(a, e) gpu_m.copyFromNumpyMat(numpy_mat) for a, e in zip(gpu_m.getData(), [1.0, 3.23, 3.0, 4.0, 5.0, 6.0]): self.assertAlmostEqual(a, e)
self.assertEqual((int(gpu_m.getHeight()), int(gpu_m.getWidth())), numpy_mat.shape) self.assertTrue(gpu_m.isGpu()) numpy_mat = gpu_m.copyToNumpyMat() numpy_mat[0, 1] = 3.23 for a, e in zip(gpu_m.getData(), [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]): self.assertAlmostEqual(a, e) gpu_m.copyFromNumpyMat(numpy_mat) for a, e in zip(gpu_m.getData(), [1.0, 3.23, 3.0, 4.0, 5.0, 6.0]): self.assertAlmostEqual(a, e) def test_numpy(self): numpy_mat = np.matrix([[1, 2], [3, 4], [5, 6]], dtype="float32") m = swig_paddle.Matrix.createDenseFromNumpy(numpy_mat) self.assertEqual((int(m.getHeight()), int(m.getWidth())), numpy_mat.shape) self.assertEqual(m.isGpu(), swig_paddle.isUsingGpu()) for a, e in zip(m.getData(), [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]): self.assertAlmostEqual(a, e) if __name__ == "__main__": swig_paddle.initPaddle("--use_gpu=0") suite = unittest.TestLoader().loadTestsFromTestCase(TestMatrix) unittest.TextTestRunner().run(suite) if swig_paddle.isGpuVersion(): swig_paddle.setUseGpu(True) unittest.main()
def gen_data(batch_size, shape): data = [] for i in xrange(batch_size): each_sample = [] each_sample.append(np.random.random(shape)) data.append(each_sample) return data feeder = DataFeeder([('image', data_type.dense_array(2352))], {'image': 0}) arg = feeder(gen_data(32, (3, 28, 28))) h = arg.getSlotFrameHeight(0) w = arg.getSlotFrameWidth(0) self.assertEqual(h, 28) self.assertEqual(w, 28) arg = feeder(gen_data(32, (3, 30, 32))) h = arg.getSlotFrameHeight(0) w = arg.getSlotFrameWidth(0) self.assertEqual(h, 30) self.assertEqual(w, 32) if __name__ == '__main__': api.initPaddle("--use_gpu=0") suite = unittest.TestLoader().loadTestsFromTestCase(DataFeederTest) unittest.TextTestRunner().run(suite) if api.isGpuVersion(): api.setUseGpu(True) unittest.main()
self.assertTrue(util.doubleEqual(n, v)) numpy_2 = vec.toNumpyArrayInplace() vec[0] = 1.3 for x, y in zip(numpy_arr, numpy_2): self.assertTrue(util.doubleEqual(x, y)) for x, y in zip(numpy_arr, vec): self.assertTrue(util.doubleEqual(x, y)) numpy_3 = vec.copyToNumpyArray() numpy_3[0] = 0.4 self.assertTrue(util.doubleEqual(vec[0], 1.3)) self.assertTrue(util.doubleEqual(numpy_3[0], 0.4)) for i in xrange(1, len(numpy_3)): util.doubleEqual(numpy_3[i], vec[i]) def testCopyFromNumpy(self): vec = swig_paddle.Vector.createZero(1) arr = np.array([1.3, 3.2, 2.4], dtype="float32") vec.copyFromNumpyArray(arr) for i in xrange(len(vec)): self.assertTrue(util.doubleEqual(vec[i], arr[i])) if __name__ == '__main__': swig_paddle.initPaddle("--use_gpu=1" if swig_paddle.isGpuVersion() else "--use_gpu=0") unittest.main()
data = [] for i in xrange(batch_size): a = np.random.randint(10) b = self.sparse_binary_reader(20000, 40, non_empty=True) c = self.dense_reader(100) each_sample = (a, b, c) data.append(each_sample) # test multiple features data_types = [('fea0', data_type.dense_vector(100)), ('fea1', data_type.sparse_binary_vector(20000)), ('fea2', data_type.integer_value(10))] feeder = DataFeeder(data_types, {'fea0': 2, 'fea1': 1, 'fea2': 0}) arg = feeder(data) out_dense = arg.getSlotValue(0).copyToNumpyMat() out_sparse = arg.getSlotValue(1) out_index = arg.getSlotIds(2).copyToNumpyArray() for i in xrange(batch_size): self.assertEqual(out_dense[i].all(), data[i][2].all()) self.assertEqual(out_sparse.getSparseRowCols(i), data[i][1]) self.assertEqual(out_index[i], data[i][0]) if __name__ == '__main__': api.initPaddle("--use_gpu=0") suite = unittest.TestLoader().loadTestsFromTestCase(DataFeederTest) unittest.TextTestRunner().run(suite) if api.isGpuVersion(): api.setUseGpu(True) unittest.main()
self.assertTrue(util.doubleEqual(n, v)) numpy_2 = vec.toNumpyArrayInplace() vec[0] = 1.3 for x, y in zip(numpy_arr, numpy_2): self.assertTrue(util.doubleEqual(x, y)) for x, y in zip(numpy_arr, vec): self.assertTrue(util.doubleEqual(x, y)) numpy_3 = vec.copyToNumpyArray() numpy_3[0] = 0.4 self.assertTrue(util.doubleEqual(vec[0], 1.3)) self.assertTrue(util.doubleEqual(numpy_3[0], 0.4)) for i in xrange(1, len(numpy_3)): util.doubleEqual(numpy_3[i], vec[i]) def testCopyFromNumpy(self): vec = swig_paddle.Vector.createZero(1) arr = np.array([1.3, 3.2, 2.4], dtype="float32") vec.copyFromNumpyArray(arr) for i in xrange(len(vec)): self.assertTrue(util.doubleEqual(vec[i], arr[i])) if __name__ == '__main__': swig_paddle.initPaddle( "--use_gpu=1" if swig_paddle.isGpuVersion() else "--use_gpu=0") unittest.main()