def cost(Weights, X, Y, lambda_param=1.0):
    # Number of samples
    m = Y.dims()[0]

    dim0 = Weights.dims()[0]
    dim1 = Weights.dims()[1] if len(Weights.dims()) > 1 else None
    dim2 = Weights.dims()[2] if len(Weights.dims()) > 2 else None
    dim3 = Weights.dims()[3] if len(Weights.dims()) > 3 else None
    # Make the lambda corresponding to Weights(0) == 0
    lambdat = af.constant(lambda_param, dim0, dim1, dim2, dim3)

    # No regularization for bias weights
    lambdat[0, :] = 0

    # Get the prediction
    H = predict_prob(X, Weights)

    # Cost of misprediction
    Jerr = -1 * af.sum(Y * af.log(H) + (1 - Y) * af.log(1 - H), dim=0)

    # Regularization cost
    Jreg = 0.5 * af.sum(lambdat * Weights * Weights, dim=0)

    # Total cost
    J = (Jerr + Jreg) / m

    # Find the gradient of cost
    D = (H - Y)
    dJ = (af.matmulTN(X, D) + lambdat * Weights) / m

    return J, dJ
예제 #2
0
def _soft_l_value_demapper_af(rx_symbs, M, snr, bits_map):
    num_bits = int(np.log2(M))
    N = rx_symbs.shape[0]
    k = bits_map.shape[1]
    sig = af.np_to_af_array(rx_symbs)
    bit_mtx = af.moddims(af.np_to_af_array(bits_map), 1, num_bits, k, 2)
    tmp = af.sum(af.broadcast(lambda x,y: af.exp(-snr*af.abs(x-y)**2), bit_mtx, sig), dim=2)
    lvl = af.log(tmp[:,:,:,1]) - af.log(tmp[:,:,:,0])
    return np.array(lvl)
예제 #3
0
def log(x):
    if isinstance(x, afnumpy.ndarray):
        s = arrayfire.log(x.d_array)
        return afnumpy.ndarray(x.shape,
                               dtype=pu.typemap(s.dtype()),
                               af_array=s)
    else:
        return numpy.log(x)
    def _cost(self, Weights: af.Array, X: af.Array, Y: af.Array,
              reg_constant: float, penalty: str) -> (af.Array, af.Array):
        # Number of samples
        m = Y.dims()[0]

        dim0 = Weights.dims()[0]
        dim1 = Weights.dims()[1] if len(Weights.dims()) > 1 else None
        dim2 = Weights.dims()[2] if len(Weights.dims()) > 2 else None
        dim3 = Weights.dims()[3] if len(Weights.dims()) > 3 else None

        # Make the lambda corresponding to Weights(0) == 0
        lambdat = af.constant(reg_constant, dim0, dim1, dim2, dim3)

        # No regularization for bias weights
        lambdat[0, :] = 0

        # Get the prediction
        H = self._predict_proba(X, Weights)

        # Cost of misprediction
        Jerr = -1 * af.sum(Y * af.log(H) + (1 - Y) * af.log(1 - H), dim=0)

        # Regularization cost
        penalty_norm = None
        if penalty == 'l2':
            penalty_norm = Weights * Weights
        else:
            penalty_norm = af.abs(Weights)
        Jreg = 0.5 * af.sum(lambdat * penalty_norm, dim=0)

        # Total cost
        J = (Jerr + Jreg) / m

        # Find the gradient of cost
        D = (H - Y)
        dJ = (af.matmulTN(X, D) + lambdat * Weights) / m

        return J, dJ
예제 #5
0
 def log_likelihoods(self,
                     *,
                     outputs: Outputs,
                     inputs: Inputs,
                     timeout: float = None,
                     iterations: int = None) -> CallResult[Sequence[float]]:
     sk_inputs, columns_to_use = self._get_columns_to_fit(
         inputs, self.hyperparams)
     af_inputs = af.from_ndarray(sk_inputs.values)
     weight_by_dist = self._weights == 'distance'
     dist_type = self._get_dist_type(self.hyperparams['dist_type'])
     probs = self._predict_proba(af_inputs, self._data, self._labels,        \
                                 self.hyperparams['n_neighbors'], dist_type, \
                                 weight_by_dist)
     return CallResult(af.log(probs).to_ndarray())
def black_scholes(S, X, R, V, T):
    # S = Underlying stock price
    # X = Strike Price
    # R = Risk free rate of interest
    # V = Volatility
    # T = Time to maturity

    d1 = af.log(S / X)
    d1 = d1 + (R + (V * V) * 0.5) * T
    d1 = d1 / (V * af.sqrt(T))

    d2 = d1 - (V * af.sqrt(T))
    cnd_d1 = cnd(d1)
    cnd_d2 = cnd(d2)

    C = S * cnd_d1 - (X * af.exp((-R) * T) * cnd_d2)
    P = X * af.exp((-R) * T) * (1 - cnd_d2) - (S * (1 -cnd_d1))

    return (C, P)
예제 #7
0
def black_scholes(S, X, R, V, T):
    # S = Underlying stock price
    # X = Strike Price
    # R = Risk free rate of interest
    # V = Volatility
    # T = Time to maturity

    d1 = af.log(S / X)
    d1 = d1 + (R + (V * V) * 0.5) * T
    d1 = d1 / (V * af.sqrt(T))

    d2 = d1 - (V * af.sqrt(T))
    cnd_d1 = cnd(d1)
    cnd_d2 = cnd(d2)

    C = S * cnd_d1 - (X * af.exp((-R) * T) * cnd_d2)
    P = X * af.exp((-R) * T) * (1 - cnd_d2) - (S * (1 - cnd_d1))

    return (C, P)
예제 #8
0
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))
예제 #9
0
def log(x):
    if isinstance(x, afnumpy.ndarray):
        s = arrayfire.log(x.d_array)
        return afnumpy.ndarray(x.shape, dtype=pu.typemap(s.dtype()), af_array=s)
    else:
        return numpy.log(x)
def predict_log_prob(X, Weights):
    return af.log(predict_prob(X, Weights))
예제 #11
0
af.display(af.tanh(a))
af.display(af.asinh(a))
af.display(af.acosh(a))
af.display(af.atanh(a))

a = af.abs(a)
b = af.abs(b)

af.display(af.root(a, b))
af.display(af.pow(a, b))
af.display(af.pow2(a))
af.display(af.exp(a))
af.display(af.expm1(a))
af.display(af.erf(a))
af.display(af.erfc(a))
af.display(af.log(a))
af.display(af.log1p(a))
af.display(af.log10(a))
af.display(af.log2(a))
af.display(af.sqrt(a))
af.display(af.cbrt(a))

a = af.round(5 * af.randu(3, 3) - 1)
b = af.round(5 * af.randu(3, 3) - 1)

af.display(af.factorial(a))
af.display(af.tgamma(a))
af.display(af.lgamma(a))
af.display(af.iszero(a))
af.display(af.isinf(a / b))
af.display(af.isnan(a / a))
af.display(af.tanh(a))
af.display(af.asinh(a))
af.display(af.acosh(a))
af.display(af.atanh(a))

a = af.abs(a)
b = af.abs(b)

af.display(af.root(a, b))
af.display(af.pow(a, b))
af.display(af.pow2(a))
af.display(af.exp(a))
af.display(af.expm1(a))
af.display(af.erf(a))
af.display(af.erfc(a))
af.display(af.log(a))
af.display(af.log1p(a))
af.display(af.log10(a))
af.display(af.log2(a))
af.display(af.sqrt(a))
af.display(af.cbrt(a))

a = af.round(5 * af.randu(3,3) - 1)
b = af.round(5 * af.randu(3,3) - 1)

af.display(af.factorial(a))
af.display(af.tgamma(a))
af.display(af.lgamma(a))
af.display(af.iszero(a))
af.display(af.isinf(a/b))
af.display(af.isnan(a/a))
예제 #13
0
파일: random.py 프로젝트: eeeedgar/model_1
def gamma_rand_marsaglia_and_tsang_arrayfire(alpha: float,
                                             lambda_: float,
                                             n: int) \
        -> af.array:
    random_numbers = af.constant(0, n, dtype=Dtype.f32)
    # Gamma(alpha, lambda) generator using Marsaglia and Tsang method
    # Algorithm 4.33
    if alpha >= 1.0:
        d = alpha - 1 / 3
        c = 1.0 / np.sqrt(9.0 * d)

        number_generated = 0
        number_generated_total = 0

        while number_generated < n:
            number_left = n - number_generated

            z = af.randn(number_left, dtype=Dtype.f32)
            y = (1.0 + c * z)
            v = y * y * y

            accept_index_1 = ((z >= -1.0 / c) & (v > 0.0))
            z_accept_1 = z[accept_index_1]
            # del z
            v_accept_1 = v[accept_index_1]
            # del v
            u_accept_1 = af.randu(v_accept_1.elements(), dtype=Dtype.f32)
            # del U

            accept_index_2 = \
                u_accept_1 < af.exp((0.5 * z_accept_1 * z_accept_1 + d - d * v_accept_1 + d * af.log(v_accept_1)))

            x_accept = d * v_accept_1[accept_index_2] / lambda_
            number_accept = x_accept.elements()

            random_numbers[number_generated:np.minimum(n, number_generated + number_accept)] = \
                x_accept[0:np.minimum(number_left, number_accept)]

            number_generated += number_accept
            number_generated_total += number_left

        if GPUOptions.verbose:
            print(f"Acceptance ratio = {n/number_generated_total}")
    else:
        random_numbers = gamma_rand_marsaglia_and_tsang_arrayfire(
            alpha + 1, lambda_, n)
        random_numbers *= af.randu(n, dtype=Dtype.f32)**(1.0 / alpha)

    return random_numbers
def xlogy(x, y):
    return x * af.log(y)
예제 #15
0
 def predict_log_proba(self, X):
     return af.log(self.predict_proba(X))
예제 #16
0
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 _predict_log_proba(self, X: af.Array, Weights: af.Array) -> af.Array:
     return af.log(self._predict_proba(X, Weights))