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()
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()