def minmod(x, y, z): min_of_all = af.minof(af.minof(af.abs(x),af.abs(y)), af.abs(z)) # af.sign(x) = 1 for x<0 and sign(x) for x>0: signx = 1 - 2 * af.sign(x) signy = 1 - 2 * af.sign(y) signz = 1 - 2 * af.sign(z) result = 0.25 * af.abs(signx + signy) * (signx + signz) * min_of_all af.eval(result) return result
def gradient(nn, delta): nabla_b = [] nabla_w = [] # output dact = nn['nonlin'][-1][1] t = nn['zs'][-1] asdf = dact(2) #This is d_relu. It is a binary output dW = average_gradient(delta*af.sign(1e-5 - nn['zs'][-1]), nn['activations'][-2]) nabla_b.append(af.mean(delta)) nabla_w.append(dW) for i in range(len(nn['weights']) - 2, -1, -1): dact = delta * af.max(nn['zs'][i+1]) trans = nn['weights'][i+1].T delta = af.matmul(trans, dact) dW = average_gradient(delta,nn['activations'][i]) nabla_b.append(af.mean(delta*af.max(nn['zs'][i]))) nabla_w.append(dW) return nabla_w, nabla_b
def gradient(nn, delta): nabla_b = [] nabla_w = [] # output dact = nn['nonlin'][-1][1] t = nn['zs'][-1] asdf = dact(2) #This is d_relu. It is a binary output dW = average_gradient(delta * af.sign(1e-5 - nn['zs'][-1]), nn['activations'][-2]) nabla_b.append(af.mean(delta)) nabla_w.append(dW) for i in range(len(nn['weights']) - 2, -1, -1): dact = delta * af.max(nn['zs'][i + 1]) trans = nn['weights'][i + 1].T delta = af.matmul(trans, dact) dW = average_gradient(delta, nn['activations'][i]) nabla_b.append(af.mean(delta * af.max(nn['zs'][i]))) nabla_w.append(dW) return nabla_w, nabla_b
def _softThreshold(self, x): if type(x).__module__ == "arrayfire.array": #POTENTIAL BUG: af.sign implementation does not agree with documentation x = (af.sign(x) - 0.5) * (-2.0) * (af.abs(x) - self.parameter) * ( af.abs(x) > self.parameter) else: x = np.sign(x) * (np.abs(x) - self.parameter) * (np.abs(x) > self.parameter) return x
def simple_arith(verbose = False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(3,3,dtype=af.Dtype.u32) b = af.constant(4, 3, 3, dtype=af.Dtype.u32) display_func(a) display_func(b) c = a + b d = a d += b display_func(c) display_func(d) display_func(a + 2) display_func(3 + a) c = a - b d = a d -= b display_func(c) display_func(d) display_func(a - 2) display_func(3 - a) c = a * b d = a d *= b display_func(c * 2) display_func(3 * d) display_func(a * 2) display_func(3 * a) c = a / b d = a d /= b display_func(c / 2.0) display_func(3.0 / d) display_func(a / 2) display_func(3 / a) c = a % b d = a d %= b display_func(c % 2.0) display_func(3.0 % d) display_func(a % 2) display_func(3 % a) c = a ** b d = a d **= b display_func(c ** 2.0) display_func(3.0 ** d) display_func(a ** 2) display_func(3 ** a) display_func(a < b) display_func(a < 0.5) display_func(0.5 < a) display_func(a <= b) display_func(a <= 0.5) display_func(0.5 <= a) display_func(a > b) display_func(a > 0.5) display_func(0.5 > a) display_func(a >= b) display_func(a >= 0.5) display_func(0.5 >= a) display_func(a != b) display_func(a != 0.5) display_func(0.5 != a) display_func(a == b) display_func(a == 0.5) display_func(0.5 == a) display_func(a & b) display_func(a & 2) c = a c &= 2 display_func(c) display_func(a | b) display_func(a | 2) c = a c |= 2 display_func(c) display_func(a >> b) display_func(a >> 2) c = a c >>= 2 display_func(c) display_func(a << b) display_func(a << 2) c = a c <<= 2 display_func(c) display_func(-a) display_func(+a) display_func(~a) display_func(a) display_func(af.cast(a, af.Dtype.c32)) display_func(af.maxof(a,b)) display_func(af.minof(a,b)) display_func(af.rem(a,b)) a = af.randu(3,3) - 0.5 b = af.randu(3,3) - 0.5 display_func(af.abs(a)) display_func(af.arg(a)) display_func(af.sign(a)) display_func(af.round(a)) display_func(af.trunc(a)) display_func(af.floor(a)) display_func(af.ceil(a)) display_func(af.hypot(a, b)) display_func(af.sin(a)) display_func(af.cos(a)) display_func(af.tan(a)) display_func(af.asin(a)) display_func(af.acos(a)) display_func(af.atan(a)) display_func(af.atan2(a, b)) c = af.cplx(a) d = af.cplx(a,b) display_func(c) display_func(d) display_func(af.real(d)) display_func(af.imag(d)) display_func(af.conjg(d)) display_func(af.sinh(a)) display_func(af.cosh(a)) display_func(af.tanh(a)) display_func(af.asinh(a)) display_func(af.acosh(a)) display_func(af.atanh(a)) a = af.abs(a) b = af.abs(b) display_func(af.root(a, b)) display_func(af.pow(a, b)) display_func(af.pow2(a)) display_func(af.exp(a)) display_func(af.expm1(a)) display_func(af.erf(a)) display_func(af.erfc(a)) display_func(af.log(a)) display_func(af.log1p(a)) display_func(af.log10(a)) display_func(af.log2(a)) display_func(af.sqrt(a)) display_func(af.cbrt(a)) a = af.round(5 * af.randu(3,3) - 1) b = af.round(5 * af.randu(3,3) - 1) display_func(af.factorial(a)) display_func(af.tgamma(a)) display_func(af.lgamma(a)) display_func(af.iszero(a)) display_func(af.isinf(a/b)) display_func(af.isnan(a/a)) a = af.randu(5, 1) b = af.randu(1, 5) c = af.broadcast(lambda x,y: x+y, a, b) display_func(a) display_func(b) display_func(c) @af.broadcast def test_add(aa, bb): return aa + bb display_func(test_add(a, b))
def simple_arith(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(3, 3) b = af.constant(4, 3, 3) display_func(a) display_func(b) c = a + b d = a d += b display_func(c) display_func(d) display_func(a + 2) display_func(3 + a) c = a - b d = a d -= b display_func(c) display_func(d) display_func(a - 2) display_func(3 - a) c = a * b d = a d *= b display_func(c * 2) display_func(3 * d) display_func(a * 2) display_func(3 * a) c = a / b d = a d /= b display_func(c / 2.0) display_func(3.0 / d) display_func(a / 2) display_func(3 / a) c = a % b d = a d %= b display_func(c % 2.0) display_func(3.0 % d) display_func(a % 2) display_func(3 % a) c = a**b d = a d **= b display_func(c**2.0) display_func(3.0**d) display_func(a**2) display_func(3**a) display_func(a < b) display_func(a < 0.5) display_func(0.5 < a) display_func(a <= b) display_func(a <= 0.5) display_func(0.5 <= a) display_func(a > b) display_func(a > 0.5) display_func(0.5 > a) display_func(a >= b) display_func(a >= 0.5) display_func(0.5 >= a) display_func(a != b) display_func(a != 0.5) display_func(0.5 != a) display_func(a == b) display_func(a == 0.5) display_func(0.5 == a) a = af.randu(3, 3, dtype=af.Dtype.u32) b = af.constant(4, 3, 3, dtype=af.Dtype.u32) display_func(a & b) display_func(a & 2) c = a c &= 2 display_func(c) display_func(a | b) display_func(a | 2) c = a c |= 2 display_func(c) display_func(a >> b) display_func(a >> 2) c = a c >>= 2 display_func(c) display_func(a << b) display_func(a << 2) c = a c <<= 2 display_func(c) display_func(-a) display_func(+a) display_func(~a) display_func(a) display_func(af.cast(a, af.Dtype.c32)) display_func(af.maxof(a, b)) display_func(af.minof(a, b)) display_func(af.rem(a, b)) a = af.randu(3, 3) - 0.5 b = af.randu(3, 3) - 0.5 display_func(af.abs(a)) display_func(af.arg(a)) display_func(af.sign(a)) display_func(af.round(a)) display_func(af.trunc(a)) display_func(af.floor(a)) display_func(af.ceil(a)) display_func(af.hypot(a, b)) display_func(af.sin(a)) display_func(af.cos(a)) display_func(af.tan(a)) display_func(af.asin(a)) display_func(af.acos(a)) display_func(af.atan(a)) display_func(af.atan2(a, b)) c = af.cplx(a) d = af.cplx(a, b) display_func(c) display_func(d) display_func(af.real(d)) display_func(af.imag(d)) display_func(af.conjg(d)) display_func(af.sinh(a)) display_func(af.cosh(a)) display_func(af.tanh(a)) display_func(af.asinh(a)) display_func(af.acosh(a)) display_func(af.atanh(a)) a = af.abs(a) b = af.abs(b) display_func(af.root(a, b)) display_func(af.pow(a, b)) display_func(af.pow2(a)) display_func(af.sigmoid(a)) display_func(af.exp(a)) display_func(af.expm1(a)) display_func(af.erf(a)) display_func(af.erfc(a)) display_func(af.log(a)) display_func(af.log1p(a)) display_func(af.log10(a)) display_func(af.log2(a)) display_func(af.sqrt(a)) display_func(af.cbrt(a)) a = af.round(5 * af.randu(3, 3) - 1) b = af.round(5 * af.randu(3, 3) - 1) display_func(af.factorial(a)) display_func(af.tgamma(a)) display_func(af.lgamma(a)) display_func(af.iszero(a)) display_func(af.isinf(a / b)) display_func(af.isnan(a / a)) a = af.randu(5, 1) b = af.randu(1, 5) c = af.broadcast(lambda x, y: x + y, a, b) display_func(a) display_func(b) display_func(c) @af.broadcast def test_add(aa, bb): return aa + bb display_func(test_add(a, b))
af.display(-a) af.display(+a) af.display(~a) af.display(a) af.display(af.cast(a, af.c32)) af.display(af.maxof(a, b)) af.display(af.minof(a, b)) af.display(af.rem(a, b)) a = af.randu(3, 3) - 0.5 b = af.randu(3, 3) - 0.5 af.display(af.abs(a)) af.display(af.arg(a)) af.display(af.sign(a)) af.display(af.round(a)) af.display(af.trunc(a)) af.display(af.floor(a)) af.display(af.ceil(a)) af.display(af.hypot(a, b)) af.display(af.sin(a)) af.display(af.cos(a)) af.display(af.tan(a)) af.display(af.asin(a)) af.display(af.acos(a)) af.display(af.atan(a)) af.display(af.atan2(a, b)) c = af.cplx(a) d = af.cplx(a, b)
af.display(-a) af.display(+a) af.display(~a) af.display(a) af.display(af.cast(a, af.c32)) af.display(af.maxof(a,b)) af.display(af.minof(a,b)) af.display(af.rem(a,b)) a = af.randu(3,3) - 0.5 b = af.randu(3,3) - 0.5 af.display(af.abs(a)) af.display(af.arg(a)) af.display(af.sign(a)) af.display(af.round(a)) af.display(af.trunc(a)) af.display(af.floor(a)) af.display(af.ceil(a)) af.display(af.hypot(a, b)) af.display(af.sin(a)) af.display(af.cos(a)) af.display(af.tan(a)) af.display(af.asin(a)) af.display(af.acos(a)) af.display(af.atan(a)) af.display(af.atan2(a, b)) c = af.cplx(a) d = af.cplx(a,b)