def test_randomly_replace_elements(): for val in (0.0, 0.5, 5): for p in (0.1, 0.2, 0.5, 0.75, 0.99): X = np.random.normal(size=(1024, 2048)).astype(np.float32) Xd = op.to_gpu(X) Xr, M = op.randomly_replace_elements(X, p, val) assert (Xr is X) assert_almost_equal((X == val).mean(), p, decimal=2, err_msg="val: %.1f p: %.1f" % (val, p)) assert_almost_equal(M.mean(), 1 - p, decimal=2, err_msg="M val: %.1f p: %.1f" % (val, p)) Xrd, Md = op.randomly_replace_elements(Xd, p, val) assert (Xrd is Xd) assert_almost_equal(op.to_cpu(op.mean(Xd == val)), p, decimal=2, err_msg="val: %.1f p: %.1f (gpu)" % (val, p)) assert_almost_equal(op.to_cpu(op.mean(Md)), 1 - p, decimal=2, err_msg="M val: %.1f p: %.1f (gpu)" % (val, p))
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 _corrupt_input(self, X, stream=None): if self.dropout > 0.0: return op.randomly_replace_elements(X, self.dropout, self.dropout_value, stream=stream) else: return X, None
def test_randomly_replace_elements(): for val in (0.0, 0.5, 5): for p in (0.1, 0.2, 0.5, 0.75, 0.99): X = np.random.normal(size=(1024, 2048)).astype(np.float32) Xd = op.to_gpu(X) Xr, M = op.randomly_replace_elements(X, p, val) assert(Xr is X) assert_almost_equal((X == val).mean(), p, decimal=2, err_msg="val: %.1f p: %.1f" % (val, p)) assert_almost_equal(M.mean(), 1-p, decimal=2, err_msg="M val: %.1f p: %.1f" % (val, p)) Xrd, Md = op.randomly_replace_elements(Xd, p, val) assert(Xrd is Xd) assert_almost_equal(op.to_cpu(op.mean(Xd == val)), p, decimal=2, err_msg="val: %.1f p: %.1f (gpu)" % (val, p)) assert_almost_equal(op.to_cpu(op.mean(Md)), 1-p, decimal=2, err_msg="M val: %.1f p: %.1f (gpu)" % (val, p))