コード例 #1
0
ファイル: standard.py プロジェクト: ftonolini45/VICI
    def compute_moments(self, x):
        '''
        compute moments of output Gaussian distribution
        INPUTS:
        x -  input
        OUTPUTS:
        mu_y - mean of output Gaussian distribution
        log_sig_sq_y - log variance of output Gaussian distribution
        '''

        hidden1_pre = tfm.add(tfl.matmul(x, self.weights['W_x_to_h1']),
                              self.weights['b_x_to_h1'])
        hidden_post = self.nonlinearity(hidden1_pre)

        num_layers_middle = np.shape(self.N_h)[0] - 1

        for i in range(num_layers_middle):
            ni = i + 2

            hidden_pre = tfm.add(
                tfl.matmul(hidden_post,
                           self.weights['W_h{}_to_h{}'.format(ni - 1, ni)]),
                self.weights['b_h{}_to_h{}'.format(ni - 1, ni)])
            hidden_post = self.nonlinearity(hidden_pre)

        mu_y = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h{}_to_muy'.format(ni)]),
            self.weights['b_h{}_to_muy'.format(ni)])
        mu_y = tf.nn.sigmoid(mu_y)
        log_sig_sq_y = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h{}_to_sy'.format(ni)]),
            self.weights['b_h{}_to_sy'.format(ni)])
        log_sig_sq_y = 100 * (tf.nn.sigmoid(log_sig_sq_y / 100) - 0.5)

        return mu_y, log_sig_sq_y
コード例 #2
0
    def compute_moments(self, x):
        '''
        compute moments of latent Gaussian distribution
        INPUTS:
            x - conditional input
        OUTPUTS:
            mu_z - mean of latent Gaussian distribution
            log_sig_sq_z - log variance of latent Gaussian distribution
        '''

        hidden1_pre = tfm.add(tfl.matmul(x, self.weights['W_x_to_h1']),
                              self.weights['b_x_to_h1'])
        hidden_post = self.nonlinearity(hidden1_pre)

        num_layers_middle = np.shape(self.N_h)[0] - 1

        for i in range(num_layers_middle):
            ni = i + 2

            hidden_pre = tfm.add(
                tfl.matmul(hidden_post,
                           self.weights['W_h{}_to_h{}'.format(ni - 1, ni)]),
                self.weights['b_h{}_to_h{}'.format(ni - 1, ni)])
            hidden_post = self.nonlinearity(hidden_pre)

        mu_z = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h{}_to_muz'.format(ni)]),
            self.weights['b_h{}_to_muz'.format(ni)])
        log_sig_sq_z = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h{}_to_sz'.format(ni)]),
            self.weights['b_h{}_to_sz'.format(ni)])
        log_sig_sq_z = self.sig_lim * (
            tf.nn.sigmoid(log_sig_sq_z / self.sig_lim) - 0.5)

        return mu_z, log_sig_sq_z
コード例 #3
0
ファイル: standard.py プロジェクト: ftonolini45/VICI
    def compute_py(self, x):
        '''
        compute probability for each class
        INPUTS:
        x - input
        OUTPUTS:
        py - histogram of probabilities for each class
        '''

        hidden1_pre = tfm.add(tfl.matmul(x, self.weights['W_x_to_h1']),
                              self.weights['b_x_to_h1'])
        hidden_post = self.nonlinearity(hidden1_pre)

        num_layers_middle = np.shape(self.N_h)[0] - 1

        for i in range(num_layers_middle):
            ni = i + 2

            hidden_pre = tfm.add(
                tfl.matmul(hidden_post,
                           self.weights['W_h{}_to_h{}'.format(ni - 1, ni)]),
                self.weights['b_h{}_to_h{}'.format(ni - 1, ni)])
            hidden_post = self.nonlinearity(hidden_pre)

        p_un = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h{}_to_py'.format(ni)]),
            self.weights['b_h{}_to_py'.format(ni)])
        p_un = tf.nn.sigmoid(p_un) + 1e-6
        py = tfm.divide(
            p_un,
            tf.tile(tf.expand_dims(tfm.reduce_sum(p_un, axis=1), axis=1),
                    [1, self.n_y]))

        return py
コード例 #4
0
ファイル: standard.py プロジェクト: ftonolini45/VICI
    def compute_py(self, xl):
        '''
        compute moments of output Gaussian distribution
        INPUTS:
        x -  input
        OUTPUTS:
        mu_y - mean of output Gaussian distribution
        log_sig_sq_y - log variance of output Gaussian distribution
        '''

        x, _ = NN_utils.reshape_and_extract(xl, self.sz_im)

        hidden_post = layers.tf_conv_layer(x, self.weights['W_x_to_h1'],
                                           self.weights['b_x_to_h1'],
                                           self.St[0], self.nonlinearity)
        # print(tf.shape(hidden_post).numpy())

        num_layers_1 = np.shape(self.N_h1)[0] - 1

        for i in range(num_layers_1):
            ni = i + 2

            hidden_post = layers.tf_conv_layer(
                hidden_post, self.weights['W_h{}_to_h{}'.format(ni - 1, ni)],
                self.weights['b_h{}_to_h{}'.format(ni - 1, ni)],
                self.St[ni - 1], self.nonlinearity)
            # print(tf.shape(hidden_post).numpy())

        hidden_post = NN_utils.flatten(hidden_post)
        # print(tf.shape(hidden_post).numpy())

        num_layers_F = np.shape(self.NF_h)[0]

        for i in range(num_layers_F):
            ni = ni + 1

            hidden_pre = tfm.add(
                tfl.matmul(hidden_post,
                           self.weights['W_h{}_to_h{}'.format(ni - 1, ni)]),
                self.weights['b_h{}_to_h{}'.format(ni - 1, ni)])
            hidden_post = self.nonlinearity(hidden_pre)
            # print(tf.shape(hidden_post).numpy())

        p_un = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h{}_to_py'.format(ni)]),
            self.weights['b_h{}_to_py'.format(ni)])
        p_un = tf.nn.sigmoid(p_un) + 1e-6
        py = tfm.divide(
            p_un,
            tf.tile(tf.expand_dims(tfm.reduce_sum(p_un, axis=1), axis=1),
                    [1, self.n_y]))

        return py
コード例 #5
0
    def call(self, x, beta):
        if self.res == 1:
            h_stack, v_stack = tf.unstack(x, axis=-1)
        else:
            h_stack = x
            v_stack = x
        #Vertical stack is acted by a vertical convolution
        #equivalent to a masked one
        v_stack = self.ver_cropping(v_stack)
        v_stack = self.ver_padding(v_stack)
        v_stack = self.ver_conv(v_stack)
        v_stack += self.ver_seq(beta)

        #Horizontal stack is acted by a horizontal convolution
        #equivalent to a masked one- h_stack2 is kept for later
        h_stack2 = h_stack
        h_stack = self.hor_cropping(h_stack)
        h_stack = self.hor_padding(h_stack)
        h_stack = self.hor_conv(h_stack)
        h_stack = tfm.add(h_stack, self.hor_seq(beta))

        #Add v_stack to h_stack
        h_stack = tfm.add(h_stack, v_stack)

        #"Gating" performed on horizontal stack
        h_stack0, h_stack1 = tf.split(h_stack, 2, axis=-1)
        h_stack0 = tfk.activations.tanh(h_stack0)
        h_stack1 = tfk.activations.sigmoid(h_stack1)
        h_stack = tfm.multiply(h_stack0, h_stack1)

        #"Gating" and convolving vertical stack
        if not self.last_layer:
            v_stack0, v_stack1 = tf.split(v_stack, 2, axis=-1)
            v_stack0 = tfk.activations.tanh(v_stack0)
            v_stack1 = tfk.activations.sigmoid(v_stack1)
            v_stack = tfm.multiply(v_stack0, v_stack1)

            v_stack = self.ver_conv2(v_stack)

        #Convolve h_stack2, h_stack and connect them
        h_stack = self.hor_conv2(h_stack)
        if self.res:
            h_stack2 = self.res_conv(h_stack2)
            h_stack = tfm.add(h_stack, h_stack2)

        if self.last_layer:
            output = h_stack
        else:
            output = tf.stack([h_stack, v_stack], axis=-1)
        return output
コード例 #6
0
ファイル: solver.py プロジェクト: nathannau/DLCube2
    def trainModel(self, infos):

        with self.graph.as_default():
            states = tf.constant([i["state"].tolist() for i in infos],
                                 shape=[infos.size, 24])
            rewards = tf.constant([i["reward"] for i in infos],
                                  dtype=tf.float32,
                                  shape=[infos.size, 1])
            # next_states = tf.constant([i["next_state"].tolist() for i in infos], shape=[infos.size, 24])
            actions = tf.constant([i["action"] for i in infos],
                                  dtype=tf.float32,
                                  shape=[infos.size, 12])

            # Qtargets = tf.constant(self.model.predict(next_states, steps=1), shape=[infos.size, 12])
            # Recupere l'etat actuel
            targets = tf.constant(self.model.predict(states, steps=1),
                                  shape=[infos.size, 12])
            # Calcure le mask negatif
            mask = tf.ones([infos.size, 12], dtype=tf.float32)
            mask = tfm.subtract(mask, actions)
            # Applique le mask négatif
            targets = tfm.multiply(targets, mask)

            # Calcure le mask positif
            mask = tfm.multiply(rewards, actions)
            # Applique le mask positif
            targets = tfm.add(targets, mask)

            self.model.fit(states, targets, steps_per_epoch=200)  # 1000
コード例 #7
0
    def time_step(self, x, y1, y2):
        """Take a step through time.

        Parameters
        ----------
        x : Input value(s) at current time step, batched in first dimension
        y1 : Scalar wave field one time step ago (part of the hidden state)
        y2 : Scalar wave field two time steps ago (part of the hidden state)
        """
        dt = self.dt
        c = self.c
        b = self.b

        term_a = 2 + dt**2*math.multiply(c.pow(2), self.compute_laplacian(y1))

        term_two = math.multiply(-1 - dt * b, y2)
        denominator = dt ** (-2) + b * 0.5 * dt ** (-1)
        y = math.multiply(denominator.pow(-1),
                          math.add(term_a, term_two))

        # Insert the source
        y_out = y[:, self.src_x, self.src_y]
        y_out = y_out + tf.broadcast_to(x, tf.shape(y_out))

        return y_out, y1
コード例 #8
0
    def call(self, x, beta):
        if self.res:
            l_stack, m_stack, r_stack = tf.unstack(x, axis=-1)
            m_stack2 = m_stack
        else:
            l_stack = x
            r_stack = x
            m_stack = tf.zeros_like(x)
        #Left stack is cropped, padded followed by convolution
        l_stack = self.l_cropping(l_stack)
        l_stack = self.l_padding(l_stack)
        l_stack = self.l_conv(l_stack)
        #Conditioning on beta
        l_stack += self.l_seq(beta)

        #Right stack is cropped, padded followed by convolution
        r_stack = self.r_cropping(r_stack)
        r_stack = self.r_padding(r_stack)
        r_stack = self.r_conv(r_stack)
        #Conditioning on beta
        r_stack += self.r_seq(beta)

        #Update/initialise m_stack
        m_stack = self.m_conv(m_stack)
        m_stack += tfm.add(l_stack, r_stack)

        #Gating operation on m_stack
        m_stack0, m_stack1 = tf.split(m_stack, 2, axis=-1)
        m_stack0 = tfk.activations.tanh(m_stack0)
        m_stack1 = tfk.activations.sigmoid(m_stack1)
        m_stack = tfm.multiply(m_stack0, m_stack1)

        if not self.last_layer:
            l_stack = self.l_conv2(l_stack)
            r_stack = self.r_conv2(r_stack)

        #Convolve m_stack2, m_stack and connect them
        m_stack = self.m_conv2(m_stack)
        if self.res:
            m_stack2 = self.m_res_conv(m_stack2)
            m_stack = tfm.add(m_stack, m_stack2)

        if self.last_layer:
            output = m_stack
        else:
            output = tf.stack([l_stack, m_stack, r_stack], axis=-1)
        return output
コード例 #9
0
 def call(self, x):
     for i in range(self.net_depth + 1):
         x = self.custom_layers[i](x)
     if self.z2 and x.shape[1] == self.L:
         x_hat = tfm.multiply(x, self.x_hat_mask)
         x_hat = tfm.add(x_hat, self.x_hat_bias)
     else:
         x_hat = x
     return x_hat
コード例 #10
0
    def call(self, x):
        if self.res:
            h_stack, v_stack = tf.unstack(x, axis=-1)
        else:
            h_stack = x
            v_stack = x
        #Vertical stack is acted by a vertical convolution
        #equivalent to a masked one
        v_stack = self.ver_cropping(v_stack)
        v_stack = self.ver_padding(v_stack)
        v_stack = self.ver_conv(v_stack)

        #Horizontal stack is acted by a horizontal convolution
        #equivalent to a masked one- h_stack2 is kept for later
        h_stack2 = h_stack
        h_stack = self.hor_cropping(h_stack)
        h_stack = self.hor_padding(h_stack)
        h_stack = self.hor_conv(h_stack)

        #Connect horizontal and vertical stacks
        h_stack = tfm.add(h_stack, v_stack)

        #Convolve and act translationally invariant version of Prelu using
        #tf.maximum and passing user-defined scalar variable for alpha
        h_stack = tfm.maximum(self.alpha_scalar * h_stack, h_stack)
        h_stack = self.sec_conv(h_stack)
        if not self.last_layer:
            v_stack = tfm.maximum(self.alpha_scalar2 * h_stack, h_stack)
            v_stack = self.sec_conv2(v_stack)

        #Make a residual connection between input state and output
        if self.res == 1:
            h_stack2 = self.res_conv(h_stack2)
            h_stack = tfm.add(h_stack, h_stack2)

        if self.last_layer:
            output = h_stack
        else:
            output = tf.stack([h_stack, v_stack], axis=-1)
        #Act with non-linear activation function
        return output
コード例 #11
0
    def compute_moments(self, z, constrain=True):
        '''
        compute moments of input/output Gaussian distribution
        INPUTS:
            z -  latent variable
        OPTIONAL INPUTS:
            constrain - whether to force the output mean to be between 0 and 1
        OUTPUTS:
            mu_x - mean of output Gaussian distribution
            log_sig_sq_x - log variance of output Gaussian distribution
        '''

        hidden1_pre = tfm.add(tfl.matmul(z, self.weights['W_z_to_h1']),
                              self.weights['b_z_to_h1'])
        hidden_post = self.nonlinearity(hidden1_pre)

        num_layers_middle = np.shape(self.N_h)[0] - 1

        for i in range(num_layers_middle):
            ni = i + 2

            hidden_pre = tfm.add(
                tfl.matmul(hidden_post,
                           self.weights['W_h{}_to_h{}'.format(ni - 1, ni)]),
                self.weights['b_h{}_to_h{}'.format(ni - 1, ni)])
            hidden_post = self.nonlinearity(hidden_pre)

        mu_x = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h{}_to_mux'.format(ni)]),
            self.weights['b_h{}_to_mux'.format(ni)])
        if constrain == True:
            mu_x = tf.nn.sigmoid(mu_x)

        log_sig_sq_x = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h{}_to_sx'.format(ni)]),
            self.weights['b_h{}_to_sx'.format(ni)])
        log_sig_sq_x = self.sig_lim * (
            tf.nn.sigmoid(log_sig_sq_x / self.sig_lim) - 0.5)

        return mu_x, log_sig_sq_x
コード例 #12
0
ファイル: layers.py プロジェクト: damiangr/non-linear-fx
    def call(self, x):

        X = []
        for i in range(self.n_activations):
            self.i = i
            frame = x[:, :, i:i + 1]

            sigma1 = K.variable(
                np.zeros((self.batch, frame.shape[1], frame.shape[2])))
            for j in range(self.v_order):
                j_fact = math.factorial(j)
                p = tfm.divide(K.pow(frame, j), j_fact)
                sigma1 = tfm.add(sigma1, tfm.multiply(self.v[i][j], p))

            sigma2 = K.variable(
                np.zeros((self.batch, frame.shape[1], frame.shape[2])))
            for j in range(1, self.w_order + 1):
                self.k = j
                sigma2 = tfm.add(sigma2, K.map_fn(self.basis2, frame))

            X.append(tfm.add(sigma1, sigma2))

        output = K.concatenate(X, axis=2)
        return output
コード例 #13
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
コード例 #14
0
    def call(self, x, beta):
        """Produces the network output for a sample

        Args:
            x (float32): A sample configuration of Ising model
            beta (float32): Inverse temperature

        Returns:
            float32: Probabilities of +1 value at every lattice site
        """
        for i in range(self.net_depth):
            x = self.custom_layers[i](x, beta)
        x = self.custom_layers[self.net_depth](x)
        if self.z2 and x.shape[1] == self.L:
            x_hat = tfm.multiply(x, self.x_hat_mask)
            x_hat = tfm.add(x_hat, self.x_hat_bias)
        else:
            x_hat = x
        return x_hat
コード例 #15
0
    def compute_moments(self, z, x, x2, constrain=True):
        '''
        compute moments of output Gaussian distribution
        INPUTS:
            x - conditional input
            x2 - conditional input
            z - latent variable
        OPTIONAL INPUTS:
            constrain - whether to force the output mean to be between 0 and 1
        OUTPUTS:
            mu_y - mean of output Gaussian distribution
            log_sig_sq_y - log variance of output Gaussian distribution
        '''

        # Channel for latent variable alone
        hidden_pre_z = tfm.add(tfl.matmul(z, self.weights['W_z_to_h1z']),
                               self.weights['b_z_to_h1z'])
        hidden_post_z = self.nonlinearity(hidden_pre_z)

        num_layers_middle_z = np.shape(self.N_hz)[0] - 1
        for i in range(num_layers_middle_z):
            ni = i + 2

            hidden_pre_z = tfm.add(
                tfl.matmul(hidden_post_z,
                           self.weights['W_h{}z_to_h{}z'.format(ni - 1, ni)]),
                self.weights['b_h{}z_to_h{}z'.format(ni - 1, ni)])
            hidden_post_z = self.nonlinearity(hidden_pre_z)

        # Channel for first conditional input alone
        hidden_pre_x = tfm.add(tfl.matmul(x, self.weights['W_x_to_h1x']),
                               self.weights['b_x_to_h1x'])
        hidden_post_x = self.nonlinearity(hidden_pre_x)

        num_layers_middle_x = np.shape(self.N_hx)[0] - 1
        for i in range(num_layers_middle_x):
            ni = i + 2

            hidden_pre_x = tfm.add(
                tfl.matmul(hidden_post_x,
                           self.weights['W_h{}x_to_h{}x'.format(ni - 1, ni)]),
                self.weights['b_h{}x_to_h{}x'.format(ni - 1, ni)])
            hidden_post_x = self.nonlinearity(hidden_pre_x)

        # Channel for second conditional input alone
        hidden_pre_x2 = tfm.add(tfl.matmul(x2, self.weights['W_x2_to_h1x2']),
                                self.weights['b_x2_to_h1x2'])
        hidden_post_x2 = self.nonlinearity(hidden_pre_x2)

        num_layers_middle_x2 = np.shape(self.N_hx2)[0] - 1
        for i in range(num_layers_middle_x2):
            ni = i + 2

            hidden_pre_x2 = tfm.add(
                tfl.matmul(hidden_post_x2,
                           self.weights['W_h{}x2_to_h{}x2'.format(ni - 1,
                                                                  ni)]),
                self.weights['b_h{}x2_to_h{}x2'.format(ni - 1, ni)])
            hidden_post_x2 = self.nonlinearity(hidden_pre_x2)

        hidden_post = tf.concat([hidden_post_z, hidden_post_x, hidden_post_x2],
                                1)

        # Channel after combining the inputs
        hidden_pre = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h0_to_h1']),
            self.weights['b_h0_to_h1'])
        hidden_post = self.nonlinearity(hidden_pre)

        num_layers_middle = np.shape(self.N_h)[0] - 1

        for i in range(num_layers_middle):
            ni = i + 2

            hidden_pre = tfm.add(
                tfl.matmul(hidden_post,
                           self.weights['W_h{}_to_h{}'.format(ni - 1, ni)]),
                self.weights['b_h{}_to_h{}'.format(ni - 1, ni)])
            hidden_post = self.nonlinearity(hidden_pre)

        mu_y = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h{}_to_muy'.format(ni)]),
            self.weights['b_h{}_to_muy'.format(ni)])
        if constrain == True:
            mu_y = tf.nn.sigmoid(mu_y)

        log_sig_sq_y = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h{}_to_sy'.format(ni)]),
            self.weights['b_h{}_to_sy'.format(ni)])
        log_sig_sq_y = self.sig_lim * (
            tf.nn.sigmoid(log_sig_sq_y / self.sig_lim) - 0.5)

        return mu_y, log_sig_sq_y
コード例 #16
0
ファイル: metrics.py プロジェクト: lequytra/SSD
def iou_metrics(Y_true, Y_pred, defaults):
    """
		Input: 
			- Y_true, Y_pred: tensors of shape (batch_size, n_defaults, 1 + numClasses + 4)

		Output: 
			- iou_metric: 
				The total overlapping area for positive coords_pred between Y_true and Y_pred
	"""
    # Create a mask for positive boxes
    pos_mask = tf.cast(tf.reduce_max(Y_true[:, :, 1:-4], axis=-1), 'float64')
    pos_mask = tf.expand_dims(pos_mask, axis=-1)

    Y_true = tf.cast(Y_true, 'float64')
    Y_pred = tf.cast(Y_pred, 'float64')

    defaults = tf.cast(tf.constant(defaults), 'float64')

    xy_pred = add(multiply(Y_pred[:, :, -4:-2], defaults[:, -2:]),
                  defaults[:, :2])
    wh_pred = multiply(exp(Y_pred[:, :, -2:]), defaults[:, -2:])

    xy_true = add(multiply(Y_true[:, :, -4:-2], defaults[:, -2:]),
                  defaults[:, :2])
    wh_true = multiply(exp(Y_true[:, :, -2:]), defaults[:, -2:])

    x1, y1 = tf.split(xy_pred, 2, axis=2)
    w1, h1 = tf.split(wh_pred, 2, axis=2)
    x2, y2 = tf.split(xy_true, 2, axis=2)
    w2, h2 = tf.split(wh_true, 2, axis=2)

    x12 = add(x1, w1)
    x22 = add(x2, w2)
    y12 = add(y1, h1)
    y22 = add(y2, h2)

    topleft_x = maximum(x1, x2)
    topleft_y = maximum(y1, y2)

    botright_x = minimum(x12, x22)
    botright_y = minimum(y12, y22)

    intersect = multiply(
        maximum(botright_x - topleft_x, tf.cast(0, 'float64')),
        maximum(botright_y - topleft_y, tf.cast(0, 'float64')))

    # Calculate areas of every coords_true coords_pred and ground-truth coords_pred
    area_pred = multiply(w1, h1)
    area_truth = multiply(w2, h2)

    # Union of area
    union = add(add(area_pred, area_truth),
                multiply(tf.cast(-1, 'float64'), intersect))

    # Avoid division by 0
    union = maximum(union, 1e-8)

    iou_matrix = maximum(tf.math.divide(intersect, union),
                         tf.cast(0, 'float64'))

    # Only keep the positive boxes
    iou_matrix = multiply(iou_matrix, pos_mask)

    num_pos = K.sum(pos_mask)

    def fn():
        return tf.math.divide(K.sum(iou_matrix), tf.cast(num_pos, 'float64'))

    def fn0():
        return tf.cast(0, 'float64')

    iou_total = tf.cond(tf.equal(num_pos, tf.cast(0, 'float64')),
                        true_fn=lambda: fn0(),
                        false_fn=lambda: fn())

    return iou_total
コード例 #17
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)
コード例 #18
0
def tf_ssim(x, y, is_normalized=False):
    """
    k1 = 0.01
    k2 = 0.03
    L = 1.0 if is_normalized else 255.0
    c1 = np.power(k1 * L, 2)
    c2 = np.power(k2 * L, 2)
    c3 = c2 / 2
    """
    k1 = 0.01
    k2 = 0.03
    L = 1.0 if is_normalized else 255.0
    c1 = tf_pow(multiply(k1, L), 2.0)
    c2 = tf_pow(multiply(k2, L), 2.0)
    c3 = divide(c2, 2.0)

    # if type(x) is np.ndarray:
    #      x = tf.convert_to_tensor(x, dtype=tf.float32)
    # if type(y) is np.ndarray:
    #      y = tf.convert_to_tensor(y, dtype=tf.float32)
    """
    ux = x.mean()
    uy = y.mean()
    """
    ux = tf_mean(x)
    uy = tf_mean(y)
    """
    std_x = x.std()
    std_y = y.std()
    """
    std_x = tf_std(x)
    std_y = tf_std(y)
    """
    xy = (x - ux) * (y - uy)
    std_xy = xy.mean()
    """
    xy = multiply(subtract(x, ux), subtract(y, uy))
    std_xy = tf_mean(xy)
    """
    l_xy = (2 * ux * uy + c1) / (np.power(ux, 2) + np.power(uy, 2) + c1)
    """
    l_son = add(multiOperation(multiply, 2.0, ux, uy), c1)
    l_mom = multiOperation(add, tf_pow(ux, 2.0), tf_pow(uy, 2.0), c1)
    l_xy = divide(l_son, l_mom)
    """
    c_xy = (2 * std_x * std_y + c2) / (np.power(std_x, 2) + np.power(std_y, 2) + c2)
    """
    c_son = add(multiOperation(multiply, 2.0, std_x, std_y), c2)
    c_mom = multiOperation(add, tf_pow(std_x, 2.0), tf_pow(std_y, 2.0), c2)
    c_xy = divide(c_son, c_mom)
    """
    s_xy = (std_xy + c3) / (std_x * std_y + c3)
    """
    s_son = add(std_xy, c3)
    s_mom = add(multiply(std_x, std_y), c3)
    s_xy = divide(s_son, s_mom)

    one = tf.constant(1.0)
    _ssim = multiOperation(multiply, l_xy, c_xy, s_xy)
    _result = tf.cond(greater(_ssim, one), lambda: one, lambda: _ssim)

    return _result
コード例 #19
0
ファイル: layers.py プロジェクト: damiangr/non-linear-fx
 def f1():
     return tfm.add(
         tfm.subtract(tfm.divide(tfm.multiply(x, x), 2),
                      tfm.multiply(k_1, x)),
         tfm.divide(tfm.multiply(k_1, k_1), 2))
コード例 #20
0
ファイル: layers.py プロジェクト: damiangr/non-linear-fx
 def f2():
     val1 = tfm.divide(
         tfm.multiply(tfm.subtract(k, k_1), tfm.subtract(k, k_1)), 2)
     val2 = tfm.multiply(tfm.subtract(k, k_1), tfm.subtract(x, k))
     val = tfm.add(val1, val2)
     return val
コード例 #21
0
    def compute_moments(self, y, x, x2):
        '''
        compute moments of latent Gaussian distribution
        INPUTS:
            x - conditional input
            y - output to encode
        OUTPUTS:
            mu_z - mean of output Gaussian distribution
            log_sig_sq_z - log variance of output Gaussian distribution
        '''

        # Channel for input/output alone
        hidden_pre_y = tfm.add(tfl.matmul(y, self.weights['W_y_to_h1y']),
                               self.weights['b_y_to_h1y'])
        hidden_post_y = self.nonlinearity(hidden_pre_y)

        num_layers_middle_y = np.shape(self.N_hy)[0] - 1
        for i in range(num_layers_middle_y):
            ni = i + 2

            hidden_pre_y = tfm.add(
                tfl.matmul(hidden_post_y,
                           self.weights['W_h{}y_to_h{}y'.format(ni - 1, ni)]),
                self.weights['b_h{}y_to_h{}y'.format(ni - 1, ni)])
            hidden_post_y = self.nonlinearity(hidden_pre_y)

        # Channel for conditional input alone
        hidden_pre_x = tfm.add(tfl.matmul(x, self.weights['W_x_to_h1x']),
                               self.weights['b_x_to_h1x'])
        hidden_post_x = self.nonlinearity(hidden_pre_x)

        num_layers_middle_x = np.shape(self.N_hx)[0] - 1
        for i in range(num_layers_middle_x):
            ni = i + 2

            hidden_pre_x = tfm.add(
                tfl.matmul(hidden_post_x,
                           self.weights['W_h{}x_to_h{}x'.format(ni - 1, ni)]),
                self.weights['b_h{}x_to_h{}x'.format(ni - 1, ni)])
            hidden_post_x = self.nonlinearity(hidden_pre_x)

        # Channel for second conditional input alone
        hidden_pre_x2 = tfm.add(tfl.matmul(x2, self.weights['W_x2_to_h1x2']),
                                self.weights['b_x2_to_h1x2'])
        hidden_post_x2 = self.nonlinearity(hidden_pre_x2)

        num_layers_middle_x2 = np.shape(self.N_hx2)[0] - 1
        for i in range(num_layers_middle_x2):
            ni = i + 2

            hidden_pre_x2 = tfm.add(
                tfl.matmul(hidden_post_x2,
                           self.weights['W_h{}x2_to_h{}x2'.format(ni - 1,
                                                                  ni)]),
                self.weights['b_h{}x2_to_h{}x2'.format(ni - 1, ni)])
            hidden_post_x2 = self.nonlinearity(hidden_pre_x2)

        hidden_post = tf.concat([hidden_post_y, hidden_post_x, hidden_post_x2],
                                1)

        # Channel after combining the inputs
        hidden_pre = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h0_to_h1']),
            self.weights['b_h0_to_h1'])
        hidden_post = self.nonlinearity(hidden_pre)

        num_layers_middle = np.shape(self.N_h)[0] - 1

        for i in range(num_layers_middle):
            ni = i + 2

            hidden_pre = tfm.add(
                tfl.matmul(hidden_post,
                           self.weights['W_h{}_to_h{}'.format(ni - 1, ni)]),
                self.weights['b_h{}_to_h{}'.format(ni - 1, ni)])
            hidden_post = self.nonlinearity(hidden_pre)

        mu_z = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h{}_to_muz'.format(ni)]),
            self.weights['b_h{}_to_muz'.format(ni)])
        log_sig_sq_z = tfm.add(
            tfl.matmul(hidden_post, self.weights['W_h{}_to_sz'.format(ni)]),
            self.weights['b_h{}_to_sz'.format(ni)])
        log_sig_sq_z = self.sig_lim * (
            tf.nn.sigmoid(log_sig_sq_z / self.sig_lim) - 0.5)

        return mu_z, log_sig_sq_z
コード例 #22
0
 def call(self, x):
     x_hat = self.net(x)
     if self.z2:
         x_hat = tfm.multiply(x_hat, self.x_hat_mask)
         x_hat = tfm.add(x_hat, self.x_hat_bias)
     return x_hat
コード例 #23
0
ファイル: standard.py プロジェクト: ftonolini45/VICI
    def compute_moments(self, xl):
        '''
        compute moments of output Gaussian distribution
        INPUTS:
        x -  input
        OUTPUTS:
        mu_y - mean of output Gaussian distribution
        log_sig_sq_y - log variance of output Gaussian distribution
        '''

        x, l = NN_utils.reshape_and_extract(xl, self.sz_im)

        hidden_post = layers.tf_conv_layer(x, self.weights['W_x_to_h1'],
                                           self.weights['b_x_to_h1'],
                                           self.St[0], self.nonlinearity)
        # print(tf.shape(hidden_post).numpy())

        num_layers_1 = np.shape(self.N_h1)[0] - 1

        for i in range(num_layers_1):
            ni = i + 2

            hidden_post = layers.tf_conv_layer(
                hidden_post, self.weights['W_h{}_to_h{}'.format(ni - 1, ni)],
                self.weights['b_h{}_to_h{}'.format(ni - 1, ni)],
                self.St[ni - 1], self.nonlinearity)
            # print(tf.shape(hidden_post).numpy())

        hidden_post = NN_utils.flatten(hidden_post)
        hidden_post = tf.concat([hidden_post, l], axis=1)
        # print(tf.shape(hidden_post).numpy())

        num_layers_F = np.shape(self.NF_h)[0]

        for i in range(num_layers_F):
            ni = ni + 1

            hidden_pre = tfm.add(
                tfl.matmul(hidden_post,
                           self.weights['W_h{}_to_h{}'.format(ni - 1, ni)]),
                self.weights['b_h{}_to_h{}'.format(ni - 1, ni)])
            hidden_post = self.nonlinearity(hidden_pre)
            # print(tf.shape(hidden_post).numpy())

        hidden_post = NN_utils.reshape_to_images(hidden_post, self.Sz2[0, :])
        # print(tf.shape(hidden_post).numpy())

        num_layers_2 = np.shape(self.N_h2)[0]

        for i in range(num_layers_2):
            ni = ni + 1

            hidden_post = layers.tf_conv_layer(
                hidden_post, self.weights['W_h{}_to_h{}'.format(ni - 1, ni)],
                self.weights['b_h{}_to_h{}'.format(ni - 1, ni)],
                self.St[ni - 1], self.nonlinearity)
            # print(tf.shape(hidden_post).numpy())

        mu_y = layers.tf_conv_layer(hidden_post,
                                    self.weights['W_h{}_to_muy'.format(ni)],
                                    self.weights['b_h{}_to_muy'.format(ni)], 1,
                                    self.nonlinearity)
        mu_y = tf.nn.sigmoid(mu_y)

        log_sig_sq_y = layers.tf_conv_layer(
            hidden_post, self.weights['W_h{}_to_sy'.format(ni)],
            self.weights['b_h{}_to_sy'.format(ni)], 1, self.nonlinearity)
        log_sig_sq_y = 100 * (tf.nn.sigmoid(log_sig_sq_y / 100) - 0.5)

        mu_y = NN_utils.flatten(mu_y)
        mu_y = tf.concat([mu_y, tf.zeros([tf.shape(mu_y)[0], 1])], axis=1)
        log_sig_sq_y = NN_utils.flatten(log_sig_sq_y)
        log_sig_sq_y = tf.concat(
            [log_sig_sq_y,
             tf.zeros([tf.shape(log_sig_sq_y)[0], 1])], axis=1)

        return mu_y, log_sig_sq_y