Esempio n. 1
0
def solve_test_case(J, f):
    assert J.ndim == 2
    assert f.ndim == 2

    assert J.shape[0] == f.shape[0]
    assert 1 == f.shape[1]

    with tf.Graph().as_default():
        J = tf.constant(name='J', value=J)
        #     JT= tf.transpose(J, name='JT')
        f = tf.constant(name='f', value=f)
        d = tf.norm(J, name='d', axis=0)
        D = tf.diag(d, name='D')

        JD = J / d

        λ = tf.placeholder(name='lambda', dtype=tf.float64, shape=[])

        Dx = tfla.lstsq(JD, -f, l2_regularizer=λ)

        r = tfla.norm(Dx)
        dr, = tf.gradients(r, λ)

        with tf.Session() as sess:
            inputs = {λ: 0.0}
            return sess.run([r, dr], feed_dict=inputs)
Esempio n. 2
0
    def calculate_internal_weight_matrix(self, target_input_matrix):
        # Convert the target matrix into an ordinary weight matrix, by solving
        # the least squares problem of linearly-transforming
        # the target input-matrix into the target output matrix.

        estimated_output_matrix = self.target_matrix
        if self.activation is not None:
            estimated_output_matrix = self.activation(estimated_output_matrix)
        zeros_input_matrix = tf.zeros_like(estimated_output_matrix[:, 0:1, :])

        estimated_recurrent_input_matrix = tf.concat(
            [zeros_input_matrix, estimated_output_matrix[:, :-1, :]], axis=1)
        estimated_full_input_matrix = tf.concat(
            [target_input_matrix, estimated_recurrent_input_matrix], axis=2)
        if self.use_bias:
            # bias nodes need incorporating into the input matrix for the last-squares method to find the bias weights at the same time as the main weights.
            estimated_full_input_matrix = tf.concat([
                tf.ones_like(estimated_full_input_matrix[:, :, 0:1]),
                estimated_full_input_matrix
            ],
                                                    axis=2)
        A = tf.reshape(estimated_full_input_matrix,
                       [-1, estimated_full_input_matrix.get_shape()[2]])
        b = tf.reshape(self.target_matrix,
                       [-1, self.target_matrix.get_shape()[2]])
        full_kernel = lstsq(
            A, b, l2_regularizer=self.pseudoinverse_l2_regularisation)
        return full_kernel
Esempio n. 3
0
    def calculate_internal_weight_matrix(self, input_image, training):
        num_channels = input_image.get_shape()[3]  #.value
        # Convert the target matrix into an ordinary weight matrix, by solving
        # the least squares problem of linearly-transforming
        # the target input-matrix into the target output matrix.

        # Because this is a CNN layer, the rank-4 input tensor needs to have all
        # its patches extracting and then flattening to form an input "matrix"
        # which is suitable for the lstsq solution.
        input_patches = tf.image.extract_patches(
            images=input_image,
            sizes=[1, self.kernel_size[0], self.kernel_size[1], 1],
            strides=[1, 1, 1, 1],
            rates=[1, 1, 1, 1],
            padding=self._padding_op)
        flattened_input = tf.reshape(
            input_patches,
            [-1, self.kernel_size[0] * self.kernel_size[1] * num_channels])
        if self.use_bias:
            # bias nodes need incorporating into the input matrix for the last-squares method to find the bias weights at the same time as the main weights.
            flattened_input = tf.concat(
                [tf.ones_like(flattened_input[:, 0:1]), flattened_input],
                axis=1)
        flattened_kernel = lstsq(flattened_input, self.target_matrix,
                                 self.pseudoinverse_l2_regularisation)

        if self.use_bias:
            b, W = flattened_kernel[0, :], flattened_kernel[1:, :]
        else:
            b, W = None, flattened_kernel
        W = tf.reshape(W, [
            self.kernel_size[0], self.kernel_size[1], num_channels,
            self.filters
        ])
        return [b, W]
Esempio n. 4
0
 def calculate_internal_weight_matrix(self, target_input_matrix):
     # Convert the target matrix into an ordinary weight matrix, by solving
     # the least squares problem of linearly-transforming
     # the target input-matrix into the target output matrix.
     b = self.target_matrix  # This is the target output matrix
     if isinstance(self.realisation_batch_size, list):
         # Note, the realisation_batch_size could be a list if this layer is following after a TSRNNDense layer, e.g. see imdb example code
         # In this case, just treat the second axis as an extension of the batch dimension, so can just merge them with a reshape...
         target_input_matrix = tf.reshape(
             target_input_matrix,
             [-1, target_input_matrix.get_shape()[-1]])
         b = tf.reshape(b, [-1, b.get_shape()[-1]])
     if self.use_bias:
         # bias nodes need incorporating into the input matrix for the last-squares method to find the bias weights at the same time as the main weights.
         inputs_with_bias = tf.concat([
             tf.ones_like(target_input_matrix[:, 0:1]), target_input_matrix
         ],
                                      axis=1)
     else:
         inputs_with_bias = target_input_matrix
     return lstsq(inputs_with_bias,
                  b,
                  l2_regularizer=self.pseudoinverse_l2_regularisation)
Esempio n. 5
0
def computeLayerWeightMatrixCNN(target_matrix, input_image, kernel_side_length, l2_regularizer, name):
    num_channels=input_image.get_shape()[3].value
    input_patches=tf.image.extract_image_patches(images=input_image, ksizes=[1,kernel_side_length,kernel_side_length,1], strides=[1,1,1,1], rates=[1,1,1,1], padding="SAME")
    flattened_input=tf.reshape(input_patches,[-1,kernel_side_length*kernel_side_length*num_channels])
    flattened_input=tf.concat([tf.ones_like(flattened_input[:,0:1]),flattened_input],axis=1)
    return lstsq_alg.lstsq(flattened_input, target_matrix, l2_regularizer)
Esempio n. 6
0
def computeLayerWeightMatrix(target_matrix, input_matrix, l2_regularizer, name):
    weight_matrix=lstsq_alg.lstsq( input_matrix,target_matrix,l2_regularizer) 
    return weight_matrix