示例#1
0
def PDF_gen_Si_func(costk, costl , phi, Fl, S3, S4, S5, AFB, S7, S8, S9):
    ##sint2k: square of sin(thetak); sin2tk: sin(2thetak); sintk: sin(thetak); 
    cost2k = costk * costk
    sint2k = 1 - cost2k
    sintk =  sqrt(sint2k)
    sin2tk = 2 * sintk * costk


    cost2l = costl * costl
    sint2l = 1 - cost2l
    sintl = sqrt(sint2l)
    sin2tl = 2 * sintl * costl
    cos2tl = 2 * cost2l - 1 

    sinphi = sin(phi)
    cosphi = cos(phi)
    cos2phi = cos(2*phi)
    sin2phi = sin(2*phi)

    dcrate = (0.75 * (1 - Fl) * sint2k + Fl * cost2k
    + 0.25 * (1 - Fl) * sint2k * cos2tl - Fl * cost2k * cos2tl
    + S3 * sint2k * sint2l * cos2phi + S4 * sin2tk * sin2tl * cosphi
    + S5 * sin2tk * sintl * cosphi + 4/3 * AFB * sint2k * costl
    + S7 * sin2tk * sintl * sinphi + S8 * sin2tk * sin2tl * sinphi
    + S9 * sint2k * sint2l * sin2phi)

    return 9 / (32 * 3.14159265) * dcrate
示例#2
0
文件: mnist.py 项目: kcyu1993/keras
def inference(images, hidden1_units, hidden2_units):

    # Hidden 1
    with tf.name_scope('hidden1'):
        weights = tf.Variable(tf.truncated_normal(
            [IMAGE_PIXELS, hidden1_units],
            stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))),
                              name='weights')
        biases = tf.Variable(tf.zeros([hidden1_units]), name='biases')
        hidden1 = tf.nn.relu(tf.matmul(images, weights) + biases)
    with tf.name_scope('hidden2'):
        weights = tf.Variable(tf.truncated_normal(
            [hidden1_units, hidden2_units],
            stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))),
                              name='weights')
        biases = tf.Variable(tf.zeros([hidden1_units]), name='biases')
        hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases)
    with tf.name_scope('softmax_linear'):
        weights = tf.Variable(tf.truncated_normal(
            [hidden2_units, NUM_CLASSES],
            stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))),
                              name='weights')
        biases = tf.Variable(tf.zeros([NUM_CLASSES]), name='biases')
        logits = tf.matmul(hidden2, weights) + biases
    return logits
示例#3
0
def pdf_1D(z, density_name=''):
    assert density_name in AVAILABLE_1D_DISTRIBUTIONS, "Incorrect density name."
    if density_name == '':
        return 1
    elif density_name == 'two_hills':
        y = 0.5
        sigma2 = 0.1
        likelihood = (1 / math.sqrt(2 * pi * sigma2)) * math.exp(-(
            (y - (z**2))**2) / (2 * sigma2))
        prior = (1 / math.sqrt(2 * pi))**math.exp(-(z**2) / 2)
        return likelihood * prior
示例#4
0
def xavier_init(fan_in, fan_out, constant=1):

    low = -constant * tfm.sqrt(6.0 / (tf.cast(fan_in, dtype=tf.float32) +
                                      tf.cast(fan_out, dtype=tf.float32)))
    high = constant * tfm.sqrt(6.0 / (tf.cast(fan_in, dtype=tf.float32) +
                                      tf.cast(fan_out, dtype=tf.float32)))
    #    low = -constant * np.sqrt(6.0 / (np.float32(fan_in) + np.float32(fan_out)))
    #    high = constant * np.sqrt(6.0 / (np.float32(fan_in) + np.float32(fan_out)))
    #    low = -constant * np.sqrt(6.0 / (fan_in +fan_out))
    #    high = constant * np.sqrt(6.0 / (fan_in +fan_out))
    return tf.random.uniform((fan_in, fan_out),
                             minval=low,
                             maxval=high,
                             dtype=tf.float32)
示例#5
0
 def mlp(self, w, b):
     w = tf.reshape(w, (-1, 1, 1))
     b = tf.reshape(b, (-1, 1, 1))
     denom = tfm.sqrt((w * self.t2 + b + 1) * (w * self.tprime2 + b + 1))
     K = tfm.asin((w * self.tt + b) / denom)
     m = tf.zeros((self.N_p), dtype='float64')
     return m, K
示例#6
0
 def _k_xx(self, X, j, k):
     S_square = tf.matmul(tf.reshape(self.S, (-1, 1)),
                          tf.reshape(self.S, (1, -1)))
     S_square = broadcast_tile(S_square, 7, 7)
     mult = S_square * self.lengthscale * 0.5 * tfm.sqrt(PI)
     return self.kervar**2 * mult * (self.h(X, k, j) +
                                     self.h(X, j, k, primefirst=False))
示例#7
0
 def attention(query, key, value):
     score = matmul(query, key, transpose_b=True)
     dim_key = cast(shape(key)[-1], float32)
     scaled_score = score / math.sqrt(dim_key)
     weights = softmax(scaled_score, axis=-1)
     output = matmul(weights, value)
     return output
示例#8
0
def integralPi_costl(x, limits, norm_range, params, model):
    costk, phi = x.unstack_x()
    Fl = params['Fl']
    P1 = params['P1']
    P3 = params['P3']
    P5p = params['P5p']
    P6p = params['P6p']
    K00 = assoc_legendre_tf(0, 0, costk)
    K20 = assoc_legendre_tf(2, 0, costk)
    K21 = assoc_legendre_tf(2, 1, costk)
    K22 = assoc_legendre_tf(2, 2, costk)
    return 9. / (32 * 3.14159265) * (
        (4.0 / 9.0) * K00 + (4.0 * Fl / 3.0 - 4.0 / 9.0) * K20 +
        (1 - Fl) / 18.0 * P1 * K22 * cos(2 * phi) * 2 +
        (Fl - 1) / 9.0 * P3 * K22 * sin(2 * phi) * 2 +
        (2.0 / 3.0) * sqrt(Fl - Fl * Fl) * P5p * K21 * cos(phi) * pi / 4 +
        (-2.0 / 3.0) * sqrt(Fl - Fl * Fl) * P6p * K21 * sin(phi) * pi / 4) * (
            2)
示例#9
0
def root_sum_squared_error(inputs):

    #if K.ndim(y_true) > 2:
    #	return K.mean(K.sqrt(K.sum(K.square(y_true - y_pred),
    #				axis=K.arange(1, K.ndim(y_true)) )))
    #else:
    return tf_math.sqrt(
        tf_math.reduce_sum(tf_math.square(inputs[0] - inputs[1]),
                           axis=(1, 2, 3, 4)))
示例#10
0
 def k_xf(self, j, X, X2):
     t_prime, t_, t_dist = self.get_distance_matrix(
         t_x=tf.reshape(X[:self.block_size], (-1, )),
         t_y=tf.reshape(X2, (-1, )))
     l = self.lengthscale
     erf_term = tfm.erf(t_dist / l - self.gamma(j)) + tfm.erf(t_ / l +
                                                              self.gamma(j))
     return self.S[j] * l * 0.5 * tfm.sqrt(PI) * tfm.exp(
         self.gamma(j)**2) * tfm.exp(-self.D[j] * t_dist) * erf_term
示例#11
0
def angle_loss_fn_batch(y_true, y_pred):
    x_p = math.sin(y_pred[:, 0]) * math.cos(y_pred[:, 1])
    y_p = math.sin(y_pred[:, 0]) * math.sin(y_pred[:, 1])
    z_p = math.cos(y_pred[:, 0])

    x_t = math.sin(y_true[:, 0]) * math.cos(y_true[:, 1])
    y_t = math.sin(y_true[:, 0]) * math.sin(y_true[:, 1])
    z_t = math.cos(y_true[:, 0])

    norm_p = math.sqrt(x_p * x_p + y_p * y_p + z_p * z_p)
    norm_t = math.sqrt(x_t * x_t + y_t * y_t + z_t * z_t)

    dot_pt = x_p * x_t + y_p * y_t + z_p * z_t

    angle_value = dot_pt / (norm_p * norm_t)
    angle_value = tf.clip_by_value(angle_value, -0.99999, 0.99999)

    loss_val = (math.acos(angle_value))

    return loss_val
def hybrid_correlation_tf(img1_fft, img2_fft):
    """Unsure if this actually the hybrid correlation Ophus refers to.
    Works on images already in fourier space.
    """
    m = img1_fft * conj(img2_fft)
    M = sqrt(tf.abs(m))
    magnitude = tf.complex(M, M * 0.0)
    theta = angle(m)
    euler = tf.exp(tf.complex(theta * 0.0, theta))
    D = magnitude * euler
    Icorr = real(ifft2(D))
    return Icorr
示例#13
0
def PDF_gen_Pi_func(costk, costl, phi, Fl, P1, P2, P3, P4p, P5p, P6p, P8p):
    ##sint2k: square of sin(thetak); sin2tk: sin(2thetak); sintk: sin(thetak);
    cost2k = costk * costk
    sint2k = 1 - cost2k
    sintk = sqrt(sint2k)
    sin2tk = 2 * sintk * costk

    cost2l = costl * costl
    sint2l = 1 - cost2l
    sintl = sqrt(sint2l)
    sin2tl = 2 * sintl * costl
    cos2tl = 2 * cost2l - 1

    sinphi = sin(phi)
    cosphi = cos(phi)
    cos2phi = cos(2 * phi)
    sin2phi = sin(2 * phi)

    Ft = 1 - Fl

    dcrate = (0.75 * Ft * sint2k + Fl * cost2k + 0.25 * Ft * sint2k * cos2tl -
              Fl * cost2k * cos2tl +
              0.5 * P1 * Ft * sint2k * sint2l * cos2phi +
              sqrt(Fl * Ft) * 0.5 * P4p * sin2tk * sin2tl * cosphi +
              sqrt(Fl * Ft) * P5p * sin2tk * sintl * cosphi -
              sqrt(Fl * Ft) * P6p * sin2tk * sintl * sinphi +
              0.5 * sqrt(Fl * Ft) * P8p * sin2tk * sin2tl * sinphi +
              2 * P2 * Ft * sint2k * costl -
              P3 * Ft * sint2k * sint2l * sin2phi)

    return 9 / (32 * 3.14159265) * dcrate
示例#14
0
文件: PST_func.py 项目: Adigorla/PCNN
def PST(I, LPF, Phase_strength, Warp_strength, Threshold_min, Threshold_max):
    #inverting Threshold_min to simplyfy optimization porcess, so we can clip all variable between 0 and 1
    LPF = ops.convert_to_tensor_v2(LPF)
    Phase_strength = ops.convert_to_tensor_v2(Phase_strength)
    Warp_strength = ops.convert_to_tensor_v2(Warp_strength)
    I = ops.convert_to_tensor_v2(I)
    Threshold_min = ops.convert_to_tensor_v2(Threshold_min)
    Threshold_max = ops.convert_to_tensor_v2(Threshold_max)

    Threshold_min = -Threshold_min
    L = 0.5
    x = tf.linspace(-L, L, I.shape[0])
    y = tf.linspace(-L, L, I.shape[1])
    [X1, Y1] = (tf.meshgrid(x, y))
    X = tf.transpose(X1)
    Y = tf.transpose(Y1)
    [THETA, RHO] = cart2pol(X, Y)
    # Apply localization kernel to the original image to reduce noise
    Image_orig_f = sig.fft2d(tf.dtypes.cast(I, tf.complex64))

    tmp6 = (LPF**2.0) / tfm.log(2.0)
    tmp5 = tfm.sqrt(tmp6)
    tmp4 = (tfm.divide(RHO, tmp5))
    tmp3 = -tfm.pow(tmp4, 2)
    tmp2 = tfm.exp(tmp3)
    expo = fftshift(tmp2)
    Image_orig_filtered = tfm.real(
        sig.ifft2d((tfm.multiply(tf.dtypes.cast(Image_orig_f, tf.complex64),
                                 tf.dtypes.cast(expo, tf.complex64)))))
    # Constructing the PST Kernel
    tp1 = tfm.multiply(RHO, Warp_strength)
    PST_Kernel_1 = tfm.multiply(
        tp1, tfm.atan(tfm.multiply(RHO, Warp_strength))
    ) - 0.5 * tfm.log(1.0 + tfm.pow(tf.multiply(RHO, Warp_strength), 2.0))
    PST_Kernel = PST_Kernel_1 / tfm.reduce_max(PST_Kernel_1) * Phase_strength
    # Apply the PST Kernel
    temp = tfm.multiply(
        fftshift(
            tfm.exp(
                tfm.multiply(tf.dtypes.complex(0.0, -1.0),
                             tf.dtypes.cast(PST_Kernel,
                                            tf.dtypes.complex64)))),
        sig.fft2d(tf.dtypes.cast(Image_orig_filtered, tf.dtypes.complex64)))
    Image_orig_filtered_PST = sig.ifft2d(temp)

    # Calculate phase of the transformed image
    PHI_features = tfm.angle(Image_orig_filtered_PST)

    out = PHI_features
    out = (out / tfm.reduce_max(out)) * 3

    return out
示例#15
0
 def call(self, x):
     input_shape = x.shape.as_list()
     axis = tuple(
         range(0 if self.batch else 1,
               len(input_shape) if input_shape else 4))
     if self.mean:
         x = x - tfm.reduce_mean(x, axis=axis, keepdims=True)
         if self.std:
             std = tfm.sqrt(
                 tfm.reduce_mean(tfm.square(x), axis=axis, keepdims=True))
     elif self.std:
         std = tfm.reduce_std(x, axis=axis, keepdims=True)
     return x / ((self.eps + std) if self.eps else std) if self.std else x
示例#16
0
def kl_normal(mu_1, log_sig_sq_1, mu_2, log_sig_sq_2):
    '''
    Element-wise KL divergence between two normal distributions
    INPUTS:
        mu_1 - mean of firat distribution
        log_sig_sq_1 - log variance of first diatribution
        mu_2 - mean of second distribution
        log_sig_sq_2 - log variance of second diatribution
    OUTPUTS:
        KL - element-wise KL divergence
    '''

    v_mean = mu_2  #2
    aux_mean = mu_1  #1
    v_log_sig_sq = log_sig_sq_2  #2
    aux_log_sig_sq = log_sig_sq_1  #1
    v_log_sig = tfm.log(tfm.sqrt(tfm.exp(v_log_sig_sq)))  #2
    aux_log_sig = tfm.log(tfm.sqrt(tfm.exp(aux_log_sig_sq)))  #1
    KL = v_log_sig - aux_log_sig + tf.divide(
        tfm.exp(aux_log_sig_sq) + tfm.square(aux_mean - v_mean),
        2.0 * tfm.exp(v_log_sig_sq)) - 0.5

    return KL
示例#17
0
    def angle_loss_fn(self, y_true, y_pred):
        x_p = math.sin(y_pred[:, 0]) * math.cos(y_pred[:, 1])
        y_p = math.sin(y_pred[:, 0]) * math.sin(y_pred[:, 1])
        z_p = math.cos(y_pred[:, 0])

        x_t = math.sin(y_true[:, 0]) * math.cos(y_true[:, 1])
        y_t = math.sin(y_true[:, 0]) * math.sin(y_true[:, 1])
        z_t = math.cos(y_true[:, 0])

        norm_p = math.sqrt(x_p * x_p + y_p * y_p + z_p * z_p)
        norm_t = math.sqrt(x_t * x_t + y_t * y_t + z_t * z_t)

        dot_pt = x_p * x_t + y_p * y_t + z_p * z_t

        angle_value = dot_pt / (norm_p * norm_t)
        angle_value = tf.clip_by_value(angle_value, -0.99999, 0.99999)

        loss_val = (math.acos(angle_value))

        # tf.debugging.check_numerics(
        #     loss_val, "Vse propalo", name=None
        # )
        # print(loss_val.shape)
        return loss_val
示例#18
0
def gradAndSq3D(x):
    """@return |dx|, dx^2"""
    knl = np.zeros([2, 2, 2, 1, 1], dtype=np.float32)
    knl[0, 0, 0] = -1
    dz = np.array(knl)
    dz[1, 0, 0] = 1
    dz = tf.nn.conv3d(x, dz, [1] * 5, "SAME")
    dy = np.array(knl)
    dy[0, 1, 0] = 1
    dy = tf.nn.conv3d(x, dy, [1] * 5, "SAME")
    dx = np.array(knl)
    dx[0, 0, 1] = 1
    dx = tf.nn.conv3d(x, dx, [1] * 5, "SAME")

    gradSq = tfm.abs(dz**2 + dy**2 + dx**2)
    grad = tfm.sqrt(gradSq)
    return grad, gradSq
示例#19
0
def reparameterisation_trick(mu, log_sig_sq):
    '''
    Sample from Gaussian such that it stays differentiable
    INPUTS:
        mu - mean of distribution
        log_sig_sq - log variance of diatribution
    OUTPUTS:
        samp - sample from distribution
    '''

    eps = tf.random.normal([tf.shape(mu)[0], tf.shape(mu)[1]],
                           0,
                           1.,
                           dtype=tf.float32)
    samp = tfm.add(mu, tfm.multiply(tfm.sqrt(tfm.exp(log_sig_sq)), eps))

    return samp
示例#20
0
def rmse(y_true, y_pred):
    mask = y_true[:, :, :, 1]
    y_true = y_true[:, :, :, 0]
    output = ((y_true - y_pred)**2) * mask
    return tf_maths.sqrt(
        tf_maths.reduce_sum(output) / tf_maths.reduce_sum(mask))
示例#21
0
文件: PST_func.py 项目: Adigorla/PCNN
def cart2pol(x, y):
    theta = tfm.atan2(y, x)
    rho = tfm.sqrt(tfm.add(tfm.square(x), tfm.square(y)))
    return (theta, rho)
示例#22
0
def gradAndSq2D(x):
    """@return |dx|, dx^2"""
    dy, dx = tf.image.image_gradients(x)
    gradSq = tfm.abs(dy**2 + dx**2)[:, :-1, :-1]
    grad = tfm.sqrt(gradSq)
    return grad, gradSq
示例#23
0
 def k_xx(self, X, j, k, t_y=None):
     '''k_xx(t, tprime)'''
     mult = self.S[j] * self.S[k] * self.lengthscale * 0.5 * tfm.sqrt(PI)
     return self.kervar**2 * mult * (self.h(X, k, j, t_y=t_y) + self.h(
         X, j, k, t_y=t_y, primefirst=False))
示例#24
0
def two_hills_log_pdf(z):
    likelihood = tfd.Normal(loc=z**2, scale=math.sqrt(two_hills_sigma2))
    prior = tfd.Normal(loc=0, scale=1)

    return likelihood.log_prob(two_hills_y) + prior.log_prob(z)
示例#25
0
 def result(self):
     return math.sqrt(math.divide_no_nan(self.total, self.count))
示例#26
0
def nrmse(y_true, y_pred):
    return tfm.sqrt(
        tfm.reduce_mean(tfm.squared_difference(y_true, y_pred)) /
        tfm.reduce_mean(y_true**2))