def inplace_saltpepper(X, overall_rate, salt_rate, salt_value, stream=None): M = op.rand_uniform_like(X, stream=op.streams[0]) D = op.rand_uniform_like(X, stream=op.streams[1]) if isinstance(X, op.gpuarray.GPUArray): __cuda_inplace_saltpepper(X, M, D, overall_rate, salt_rate, salt_value, stream=stream) else: M = M > overall_rate D = D < salt_rate D = D.astype(X.dtype) if salt_value != 1.0: D *= salt_value X[~M.astype(np.bool)] = D[~M.astype(np.bool)] return X, M
def inplace_dropin(X, rate, value, stream=None): if isinstance(X, op.gpuarray.GPUArray): return op.randomly_replace_elements(X, rate, value, stream=stream) else: M = (op.rand_uniform_like(X) < rate) X[M.astype(np.bool)] = value return X, M
def test_rand(): X = np.empty((1000, 1000), dtype=np.float32) Y = op.rand_uniform_like(X) rtol = 1e-3 assert (Y.mean() - 0.5) < rtol, "mean: %f" % Y.mean() assert Y.min() >= 0.0, "min: %f" % Y.min() assert Y.min() - 0.0 < rtol, "min: %f" % Y.min() assert Y.max() <= 1.0, "max: %f" % Y.max() assert Y.max() - 1.0 - rtol, "max: %f" % Y.max() Y = np.empty_like(X) out = op.rand_uniform_like(X, out=Y) assert out is Y assert (Y.mean() - 0.5) < rtol, "mean: %f" % Y.mean() assert Y.min() >= 0.0, "min: %f" % Y.min() assert Y.min() - 0.0 < rtol, "min: %f" % Y.min() assert Y.max() <= 1.0, "max: %f" % Y.max() assert Y.max() - 1.0 - rtol, "max: %f" % Y.max() Xd = op.to_gpu(X) Yd = gpuarray.empty_like(Xd) Y = op.to_cpu(op.rand_uniform_like(Xd)) assert (Y.mean() - 0.5) < rtol, "mean: %f" % Y.mean() assert Y.min() >= 0.0, "min: %f" % Y.min() assert Y.min() - 0.0 < rtol, "min: %f" % Y.min() assert Y.max() <= 1.0, "max: %f" % Y.max() assert Y.max() - 1.0 - rtol, "max: %f" % Y.max() out = op.rand_uniform_like(Xd, out=Yd) assert out is Yd Y = op.to_cpu(Yd) assert (Y.mean() - 0.5) < rtol, "mean: %f" % Y.mean() assert Y.min() >= 0.0, "min: %f" % Y.min() assert Y.min() - 0.0 < rtol, "min: %f" % Y.min() assert Y.max() <= 1.0, "max: %f" % Y.max() assert Y.max() - 1.0 - rtol, "max: %f" % Y.max()