Exemple #1
0
    def antenna_jones(lm, stokes, alpha, ref_freq):
        """
        Compute the jones terms for each antenna.

        lm, stokes and alpha are the source variables.
        """

        # Compute the complex phase
        cplx_phase = rime.phase(lm, D.uvw, D.frequency, CT=CT)

        # Check for nans/infs in the complex phase
        phase_msg = ("Check that '1 - l**2  - m**2 >= 0' holds "
                    "for all your lm coordinates. This is required "
                    "for 'n = sqrt(1 - l**2 - m**2) - 1' "
                    "to be finite.")

        phase_real = tf.check_numerics(tf.real(cplx_phase), phase_msg)
        phase_imag = tf.check_numerics(tf.imag(cplx_phase), phase_msg)

        # Compute the square root of the brightness matrix
        # (as well as the sign)
        bsqrt, sgn_brightness = rime.b_sqrt(stokes, alpha,
            D.frequency, ref_freq, CT=CT,
            polarisation_type=polarisation_type)

        # Check for nans/infs in the bsqrt
        bsqrt_msg = ("Check that your stokes parameters "
                    "satisfy I**2 >= Q**2 + U**2 + V**2. "
                    "Montblanc performs a cholesky decomposition "
                    "of the brightness matrix and the above must "
                    "hold for this to produce valid values.")

        bsqrt_real = tf.check_numerics(tf.real(bsqrt), bsqrt_msg)
        bsqrt_imag = tf.check_numerics(tf.imag(bsqrt), bsqrt_msg)

        # Compute the direction dependent effects from the beam
        ejones = rime.e_beam(lm, D.frequency,
            D.pointing_errors, D.antenna_scaling,
            beam_sin, beam_cos,
            D.beam_extents, D.beam_freq_map, D.ebeam)

        deps = [phase_real, phase_imag, bsqrt_real, bsqrt_imag]
        deps = [] # Do nothing for now

        # Combine the brightness square root, complex phase,
        # feed rotation and beam dde's
        with tf.control_dependencies(deps):
            antenna_jones = rime.create_antenna_jones(bsqrt, cplx_phase,
                                                    feed_rotation, ejones, FT=FT)
            return antenna_jones, sgn_brightness
  def _compareMulGradient(self, data):
    # data is a float matrix of shape [n, 4].  data[:, 0], data[:, 1],
    # data[:, 2], data[:, 3] are real parts of x, imaginary parts of
    # x, real parts of y and imaginary parts of y.
    with self.test_session():
      inp = tf.convert_to_tensor(data)
      xr, xi, yr, yi = tf.split(1, 4, inp)

      def vec(x):  # Reshape to a vector
        return tf.reshape(x, [-1])
      xr, xi, yr, yi = vec(xr), vec(xi), vec(yr), vec(yi)

      def cplx(r, i):  # Combine to a complex vector
        return tf.complex(r, i)
      x, y = cplx(xr, xi), cplx(yr, yi)
      # z is x times y in complex plane.
      z = x * y
      # Defines the loss function as the sum of all coefficients of z.
      loss = tf.reduce_sum(tf.real(z) + tf.imag(z))
      epsilon = 0.005
      jacob_t, jacob_n = tf.test.compute_gradient(inp,
                                                  list(data.shape),
                                                  loss,
                                                  [1],
                                                  x_init_value=data,
                                                  delta=epsilon)
    self.assertAllClose(jacob_t, jacob_n, rtol=epsilon, atol=epsilon)
Exemple #3
0
    def _inference(self, x, dropout):
        with tf.name_scope('conv1'):
            # Transform to Fourier domain
            x_2d = tf.reshape(x, [-1, 28, 28])
            x_2d = tf.complex(x_2d, 0)
            xf_2d = tf.fft2d(x_2d)
            xf = tf.reshape(xf_2d, [-1, NFEATURES])
            xf = tf.expand_dims(xf, 1)  # NSAMPLES x 1 x NFEATURES
            xf = tf.transpose(xf)  # NFEATURES x 1 x NSAMPLES
            # Filter
            Wreal = self._weight_variable([int(NFEATURES/2), self.F, 1])
            Wimg = self._weight_variable([int(NFEATURES/2), self.F, 1])
            W = tf.complex(Wreal, Wimg)
            xf = xf[:int(NFEATURES/2), :, :]
            yf = tf.matmul(W, xf)  # for each feature
            yf = tf.concat([yf, tf.conj(yf)], axis=0)
            yf = tf.transpose(yf)  # NSAMPLES x NFILTERS x NFEATURES
            yf_2d = tf.reshape(yf, [-1, 28, 28])
            # Transform back to spatial domain
            y_2d = tf.ifft2d(yf_2d)
            y_2d = tf.real(y_2d)
            y = tf.reshape(y_2d, [-1, self.F, NFEATURES])
            # Bias and non-linearity
            b = self._bias_variable([1, self.F, 1])
#            b = self._bias_variable([1, self.F, NFEATURES])
            y += b  # NSAMPLES x NFILTERS x NFEATURES
            y = tf.nn.relu(y)
        with tf.name_scope('fc1'):
            W = self._weight_variable([self.F*NFEATURES, NCLASSES])
            b = self._bias_variable([NCLASSES])
            y = tf.reshape(y, [-1, self.F*NFEATURES])
            y = tf.matmul(y, W) + b
        return y
    def get_reconstructed_image(self, real, imag, name=None):
        """
        :param real:
        :param imag:
        :param name:
        :return:
        """
        complex_k_space_label = tf.complex(real=tf.squeeze(real), imag=tf.squeeze(imag), name=name+"_complex_k_space")
        rec_image_complex = tf.expand_dims(tf.ifft2d(complex_k_space_label), axis=1)
        
        rec_image_real = tf.reshape(tf.real(rec_image_complex), shape=[-1, 1, self.dims_out[1], self.dims_out[2]])
        rec_image_imag = tf.reshape(tf.imag(rec_image_complex), shape=[-1, 1, self.dims_out[1], self.dims_out[2]])

        # Shifting
        top, bottom = tf.split(rec_image_real, num_or_size_splits=2, axis=2)
        top_left, top_right = tf.split(top, num_or_size_splits=2, axis=3)
        bottom_left, bottom_right = tf.split(bottom, num_or_size_splits=2, axis=3)

        top_shift = tf.concat(axis=3, values=[bottom_right, bottom_left])
        bottom_shift = tf.concat(axis=3, values=[top_right, top_left])
        shifted_image = tf.concat(axis=2, values=[top_shift, bottom_shift])


        # Shifting
        top_imag, bottom_imag = tf.split(rec_image_imag, num_or_size_splits=2, axis=2)
        top_left_imag, top_right_imag = tf.split(top_imag, num_or_size_splits=2, axis=3)
        bottom_left_imag, bottom_right_imag = tf.split(bottom_imag, num_or_size_splits=2, axis=3)

        top_shift_imag = tf.concat(axis=3, values=[bottom_right_imag, bottom_left_imag])
        bottom_shift_imag = tf.concat(axis=3, values=[top_right_imag, top_left_imag])
        shifted_image_imag = tf.concat(axis=2, values=[top_shift_imag, bottom_shift_imag])

        shifted_image_two_channels = tf.stack([shifted_image[:,0,:,:], shifted_image_imag[:,0,:,:]], axis=1)
        return shifted_image_two_channels
    def fft_conv(self, source, filters, width, height, stride, activation='relu', name='fft_conv'):
        # This function implements a convolution using a spectrally parameterized filter with the normal
        # tf.nn.conv2d() convolution operation. This is done by transforming the spectral filter to spatial
        # via tf.batch_ifft2d()

        channels = source.get_shape().as_list()[3]

        with tf.variable_scope(name):
            init = self.random_spatial_to_spectral(channels, filters, height, width)

            if name in self.initialization:
                init = self.initialization[name]

            # Option 1: Over-Parameterize fully in the spectral domain
            # w_real = tf.Variable(init.real, dtype=tf.float32, name='real')
            # w_imag = tf.Variable(init.imag, dtype=tf.float32, name='imag')
            # w = tf.cast(tf.complex(w_real, w_imag), tf.complex64)

            # Option 2: Parameterize only 'free' parameters in the spectral domain to enforce conjugate symmetry
            #           This is very slow.
            w = self.spectral_to_variable(init)

            b = tf.Variable(tf.constant(0.1, shape=[filters]))

        # Transform the spectral parameters into a spatial filter
        # and reshape for tf.nn.conv2d
        complex_spatial_filter = tf.batch_ifft2d(w)
        spatial_filter = tf.real(complex_spatial_filter)
        spatial_filter = tf.transpose(spatial_filter, [2, 3, 0, 1])

        conv = tf.nn.conv2d(source, spatial_filter, strides=[1, stride, stride, 1], padding='SAME')
        output = tf.nn.bias_add(conv, b)
        output = tf.nn.relu(output) if activation is 'relu' else output

        return output, spatial_filter, w
Exemple #6
0
  def get_mu_tensor(self):
    const_fact = self._dist_to_opt_avg**2 * self._h_min**2 / 2 / self._grad_var
    coef = tf.Variable([-1.0, 3.0, 0.0, 1.0], dtype=tf.float32, name="cubic_solver_coef")
    coef = tf.scatter_update(coef, tf.constant(2), -(3 + const_fact) )        
    roots = tf.py_func(np.roots, [coef], Tout=tf.complex64, stateful=False)
    
    # filter out the correct root
    root_idx = tf.logical_and(tf.logical_and(tf.greater(tf.real(roots), tf.constant(0.0) ),
      tf.less(tf.real(roots), tf.constant(1.0) ) ), tf.less(tf.abs(tf.imag(roots) ), 1e-5) )
    # in case there are two duplicated roots satisfying the above condition
    root = tf.reshape(tf.gather(tf.gather(roots, tf.where(root_idx) ), tf.constant(0) ), shape=[] )
    tf.assert_equal(tf.size(root), tf.constant(1) )

    dr = self._h_max / self._h_min
    mu = tf.maximum(tf.real(root)**2, ( (tf.sqrt(dr) - 1)/(tf.sqrt(dr) + 1) )**2)    
    return mu
    def __call__(self, inputs, state, scope=None ):
        with tf.variable_scope(scope or type(self).__name__):
            unitary_hidden_state, secondary_cell_hidden_state = tf.split(1,2,state)


            mat_in = tf.get_variable('mat_in', [self.input_size, self.state_size*2])
            mat_out = tf.get_variable('mat_out', [self.state_size*2, self.output_size])
            in_proj = tf.matmul(inputs, mat_in)            
            in_proj_c = tf.complex(tf.split(1,2,in_proj))
            out_state = modReLU( in_proj_c + 
                ulinear(unitary_hidden_state, self.state_size),
                tf.get_variable(name='bias', dtype=tf.float32, shape=tf.shape(unitary_hidden_state), initializer = tf.constant_initalizer(0.)),
                scope=scope)


        with tf.variable_scope('unitary_output'):
            '''computes data linear, unitary linear and summation -- TODO: should be complex output'''
            unitary_linear_output_real = linear.linear([tf.real(out_state), tf.imag(out_state), inputs], True, 0.0)
        

        with tf.variable_scope('scale_nonlinearity'):
            modulus = tf.complex_abs(unitary_linear_output_real)
            rescale = tf.maximum(modulus + hidden_bias, 0.) / (modulus + 1e-7)

        #transition to data shortcut connection


        #out_ = tf.matmul(tf.concat(1,[tf.real(out_state), tf.imag(out_state), ] ), mat_out) + out_bias

        #hidden state is complex but output is completely real
        return out_, out_state #complex 
Exemple #8
0
def sparse_dot_product0(emb, tuples, use_matmul=True, output_type='real'):
    """
    Compute the dot product of complex vectors.
    It uses complex vectors but tensorflow does not optimize in the complex space (or there is a bug in the gradient
    propagation with complex numbers...)
    :param emb: embeddings
    :param tuples: indices at which we compute dot products
    :return: scores (dot products)
    """
    n_t = tuples.get_shape()[0].value
    rk = emb.get_shape()[1].value
    emb_sel_a = tf.gather(emb, tuples[:, 0])
    emb_sel_b = tf.gather(emb, tuples[:, 1])
    if use_matmul:
        pred_cplx = tf.squeeze(tf.batch_matmul(
                tf.reshape(emb_sel_a, [n_t, rk, 1]),
                tf.reshape(emb_sel_b, [n_t, rk, 1]), adj_x=True))
    else:
        pred_cplx = tf.reduce_sum(tf.mul(tf.conj(emb_sel_a), emb_sel_b), 1)
    if output_type == 'complex':
        return pred_cplx
    elif output_type == 'real':
        return tf.real(pred_cplx) + tf.imag(pred_cplx)
    elif output_type == 'real':
        return tf.abs(pred_cplx)
    elif output_type == 'angle':
        raise NotImplementedError('No argument or inverse-tanh function for complex number in Tensorflow')
    else:
        raise NotImplementedError()
Exemple #9
0
def visualize_data_transformations():
    records = glob.glob(os.path.join(utils.working_dir, 'train_fragment_*.tfrecords'))
    dataset = tf.data.TFRecordDataset(records)
    dataset = dataset.map(parse_tfrecord_raw)
    dataset = dataset.repeat()
    dataset = dataset.shuffle(buffer_size=10)
    dataset = dataset.prefetch(2)
    it = dataset.make_one_shot_iterator()

    data_x = tf.placeholder(tf.float32, shape=(utils.sample_rate * utils.audio_clip_len,))
    data_y = tf.placeholder(tf.float32, shape=(utils.timesteps,))
    stfts = tf.contrib.signal.stft(data_x, frame_length=utils.frame_length, frame_step=utils.frame_step,
                                   fft_length=4096)
    power_stfts = tf.real(stfts * tf.conj(stfts))
    magnitude_spectrograms = tf.abs(stfts)
    power_magnitude_spectrograms = tf.abs(power_stfts)

    num_spectrogram_bins = magnitude_spectrograms.shape[-1].value

    # scale frequency to mel scale and put into bins to reduce dimensionality
    lower_edge_hertz, upper_edge_hertz = 30.0, 17000.0
    num_mel_bins = utils.mel_bins_base * 4
    linear_to_mel_weight_matrix = tf.contrib.signal.linear_to_mel_weight_matrix(
        num_mel_bins, num_spectrogram_bins, utils.sample_rate, lower_edge_hertz,
        upper_edge_hertz)
    mel_spectrograms = tf.tensordot(magnitude_spectrograms, linear_to_mel_weight_matrix, 1)
    mel_spectrograms.set_shape(
        magnitude_spectrograms.shape[:-1].concatenate(linear_to_mel_weight_matrix.shape[-1:]))

    # log scale the mel bins to better represent human loudness perception
    log_offset = 1e-6
    log_mel_spectrograms = tf.log(mel_spectrograms + log_offset)

    # compute first order differential and concat. "It indicates a raise or reduction of the energy for each
    # frequency bin at a frame relative to its predecessor"
    first_order_diff = tf.abs(
        tf.subtract(log_mel_spectrograms, tf.manip.roll(log_mel_spectrograms, shift=1, axis=1)))
    mel_fod = tf.concat([log_mel_spectrograms, first_order_diff], 1)

    with tf.Session() as sess:
        while True:
            try:
                raw_x, raw_y = sess.run(it.get_next())
                np_stfts = sess.run(power_stfts, feed_dict={data_x: raw_x})
                np_magnitude_spectrograms = sess.run(power_magnitude_spectrograms, feed_dict={data_x: raw_x})
                np_mel_spectrograms = sess.run(mel_spectrograms, feed_dict={data_x: raw_x})
                np_log_mel_spectrograms = sess.run(log_mel_spectrograms, feed_dict={data_x: raw_x})
                np_mel_fod = sess.run(mel_fod, feed_dict={data_x: raw_x})

                utils.plot_signal_transforms(raw_x,
                                            np_stfts,
                                            np_magnitude_spectrograms,
                                            np_mel_spectrograms,
                                            np_log_mel_spectrograms,
                                            np_mel_fod)
                print('wank')

            except tf.errors.OutOfRangeError:
                break
Exemple #10
0
    def bilinear_pool(self, x1, x2):

        p1 = tf.matmul(x1, self.C[0])
        p2 = tf.matmul(x2, self.C[1])
        pc1 = tf.complex(p1, tf.zeros_like(p1))
        pc2 = tf.complex(p2, tf.zeros_like(p2))

        conved = tf.batch_ifft(tf.batch_fft(pc1) * tf.batch_fft(pc2))
        return tf.real(conved)
 def _compareRealImag(self, cplx, use_gpu):
   np_real, np_imag = np.real(cplx), np.imag(cplx)
   with self.test_session(use_gpu=use_gpu) as sess:
     inx = tf.convert_to_tensor(cplx)
     tf_real = tf.real(inx)
     tf_imag = tf.imag(inx)
     tf_real_val, tf_imag_val = sess.run([tf_real, tf_imag])
   self.assertAllEqual(np_real, tf_real_val)
   self.assertAllEqual(np_imag, tf_imag_val)
   self.assertShapeEqual(np_real, tf_real)
   self.assertShapeEqual(np_imag, tf_imag)
Exemple #12
0
    def compute_spectrograms(self, waveforms, labels=None):
        
        """Computes spectrograms for a batch of waveforms."""
        
        s = self.settings
        
        # Set final dimension of waveforms, which comes to us as `None`.
        self._set_waveforms_shape(waveforms)

        # Compute STFTs.
        waveforms = tf.cast(waveforms, tf.float32)
        stfts = tf.contrib.signal.stft(
            waveforms, self.window_size, self.hop_size,
            fft_length=self.dft_size, window_fn=self.window_fn)
        
        # Slice STFTs along frequency axis.
        stfts = stfts[..., self.freq_start_index:self.freq_end_index]
        
        # Get STFT magnitudes squared, i.e. squared spectrograms.
        grams = tf.real(stfts * tf.conj(stfts))
        # gram = tf.abs(stft) ** 2
        
        # Take natural log of squared spectrograms. Adding an epsilon
        # avoids log-of-zero errors.
        grams = tf.log(grams + s.spectrogram_log_epsilon)
        
        # Clip spectrograms if indicated.
        if s.spectrogram_clipping_enabled:
            grams = tf.clip_by_value(
                grams, s.spectrogram_clipping_min, s.spectrogram_clipping_max)
            
        # Normalize spectrograms if indicated.
        if s.spectrogram_normalization_enabled:
            grams = \
                s.spectrogram_normalization_scale_factor * grams + \
                s.spectrogram_normalization_offset
        
        # Reshape spectrograms for input into Keras neural network.
        grams = self._reshape_grams(grams)
        
        # Create features dictionary.
        features = {self.output_feature_name: grams}
        
        if labels is None:
            
            return features
        
        else:
            # have labels
        
            # Reshape labels into a single 2D column.
            labels = tf.reshape(labels, (-1, 1))
            
            return features, labels
 def one_bp_iteration(self, xe_v2c_pre_iter, H_sumC_to_V, H_sumV_to_C, xe_0):
     xe_tanh = tf.tanh(tf.to_double(tf.truediv(xe_v2c_pre_iter, [2.0])))
     xe_tanh = tf.to_float(xe_tanh)
     xe_tanh_temp = tf.sign(xe_tanh)
     xe_sum_log_img = tf.matmul(H_sumC_to_V, tf.multiply(tf.truediv((1 - xe_tanh_temp), [2.0]), [3.1415926]))
     xe_sum_log_real = tf.matmul(H_sumC_to_V, tf.log(1e-8 + tf.abs(xe_tanh)))
     xe_sum_log_complex = tf.complex(xe_sum_log_real, xe_sum_log_img)
     xe_product = tf.real(tf.exp(xe_sum_log_complex))
     xe_product_temp = tf.multiply(tf.sign(xe_product), -2e-7)
     xe_pd_modified = tf.add(xe_product, xe_product_temp)
     xe_v_sumc = tf.multiply(self.atanh(xe_pd_modified), [2.0])
     xe_c_sumv = tf.add(xe_0, tf.matmul(H_sumV_to_C, xe_v_sumc))
     return xe_v_sumc, xe_c_sumv
Exemple #14
0
 def _checkGrad(self, func, x, y, use_gpu=False):
     with self.test_session(use_gpu=use_gpu):
         inx = tf.convert_to_tensor(x)
         iny = tf.convert_to_tensor(y)
         # func is a forward or inverse FFT function (batched or unbatched)
         z = func(tf.complex(inx, iny))
         # loss = sum(|z|^2)
         loss = tf.reduce_sum(tf.real(z * tf.conj(z)))
         ((x_jacob_t, x_jacob_n), (y_jacob_t, y_jacob_n)) = tf.test.compute_gradient(
             [inx, iny], [list(x.shape), list(y.shape)], loss, [1], x_init_value=[x, y], delta=1e-2
         )
     self.assertAllClose(x_jacob_t, x_jacob_n, rtol=1e-2, atol=1e-2)
     self.assertAllClose(y_jacob_t, y_jacob_n, rtol=1e-2, atol=1e-2)
Exemple #15
0
 def __call__(self, inputs, state, scope=None ):
     zero_initer = tf.constant_initializer(0.)
     with tf.variable_scope(scope or type(self).__name__):
         mat_in = tf.get_variable('W_in', [self.input_size, self.state_size*2])
         mat_out = tf.get_variable('W_out', [self.state_size*2, self.output_size])
         in_proj = tf.matmul(inputs, mat_in)
         in_proj_c = tf.complex( in_proj[:, :self.state_size], in_proj[:, self.state_size:] )
         out_state = modrelu_c( in_proj_c + 
             ulinear_c(state,transform=self.transform),
             tf.get_variable(name='B', dtype=tf.float32, shape=[self.state_size], initializer=zero_initer)
             )
         out_bias = tf.get_variable(name='B_out', dtype=tf.float32, shape=[self.output_size], initializer = zero_initer)
         out = tf.matmul( tf.concat(1,[tf.real(out_state), tf.imag(out_state)] ), mat_out ) + out_bias
     return out, out_state
Exemple #16
0
def c2q1d(x):
    """ An internal function to convert a 1D Complex vector back to a real
    array,  which is twice the height of x.
    """
    # Input has shape [batch, r, c, 2]
    r, c = x.get_shape().as_list()[1:3]
    x1 = tf.real(x)
    x2 = tf.imag(x)
    # Stack 2 inputs of shape [batch, r, c] to [batch, r, 2, c]
    y = tf.stack([x1, x2], axis=-2)
    # Reshaping interleaves the results
    y = tf.reshape(y, [-1, 2 * r, c])

    return y
Exemple #17
0
 def _compareGradient(self, x):
     # x[:, 0] is real, x[:, 1] is imag.  We combine real and imag into
     # complex numbers. Then, we extract real and imag parts and
     # computes the squared sum. This is obviously the same as sum(real
     # * real) + sum(imag * imag). We just want to make sure the
     # gradient function is checked.
     with self.test_session():
         inx = tf.convert_to_tensor(x)
         real, imag = tf.split(1, 2, inx)
         real, imag = tf.reshape(real, [-1]), tf.reshape(imag, [-1])
         cplx = tf.complex(real, imag)
         cplx = tf.conj(cplx)
         loss = tf.reduce_sum(tf.square(tf.real(cplx))) + tf.reduce_sum(tf.square(tf.imag(cplx)))
         epsilon = 1e-3
         jacob_t, jacob_n = tf.test.compute_gradient(inx, list(x.shape), loss, [1], x_init_value=x, delta=epsilon)
     self.assertAllClose(jacob_t, jacob_n, rtol=epsilon, atol=epsilon)
  def __init__(self, **kwargs):
    """
    """
    def _interleaveVectors(vec1, vec2):
        vec1 = tf.expand_dims(vec1, 3)
        vec2 = tf.expand_dims(vec2, 3)
        interleaved = tf.concat([vec1, vec2], 3)
        interleaved = tf.reshape(interleaved, (tf.shape(vec1)[0], tf.shape(vec1)[1], tf.shape(vec1)[2] * 2))
        return interleaved
    super(ComplexToAlternatingRealLayer, self).__init__(**kwargs)

    input_placeholder = self.input_data.get_placeholder_as_batch_major()

    real_value = tf.real(input_placeholder)
    imag_value = tf.imag(input_placeholder)
    self.output.placeholder = _interleaveVectors(real_value, imag_value)
    self.output.size_placeholder = {0: self.input_data.size_placeholder[self.input_data.time_dim_axis_excluding_batch]}
    def __call__(self, inputs, state, scope=None ):
        zero_initer = tf.constant_initializer(0.)
        with tf.variable_scope(scope or type(self).__name__):

            #nick there are these two matrix multiplications and they are used to convert regular input sizes to complex outputs -- makes sense -- we can further modify this for lstm configurations
            mat_in = tf.get_variable('W_in', [self.input_size, self.state_size*2])
            mat_out = tf.get_variable('W_out', [self.state_size*2, self.output_size])

            in_proj = tf.matmul(inputs, mat_in)
            in_proj_c = tf.complex( in_proj[:, :self.state_size], in_proj[:, self.state_size:] )
            out_state = modrelu_c( in_proj_c + 
                ulinear_c(state,transform=self.transform),
                tf.get_variable(name='B', dtype=tf.float32, shape=[self.state_size], initializer=zero_initer)
                )
            out_bias = tf.get_variable(name='B_out', dtype=tf.float32, shape=[self.output_size], initializer = zero_initer)
            out = tf.matmul( tf.concat(1,[tf.real(out_state), tf.imag(out_state)] ), mat_out ) + out_bias
        return out, out_state
def bilinear_pool(x2, x1, output_size):
    """ Computes approximation of bilinear pooling with respect to x1, x2.
    For detailed explaination, see the paper (https://arxiv.org/abs/1511.06062)

    Args:
      x1: A `Tensor` with shape (batch_size, x1_size).
      x2: A `Tensor` with shape ((batch_size, x2_size).
      output_size: Output projection size. (`int`)

    Returns:
       A Tensor with shape (batch_size, output_size).
    """
    p1 = count_sketch(x1, output_size)
    p2 = count_sketch(x2, output_size)
    pc1 = tf.complex(p1, tf.zeros_like(p1))
    pc2 = tf.complex(p2, tf.zeros_like(p2))

    conved = tf.batch_ifft(tf.batch_fft(pc1) * tf.batch_fft(pc2))
    return tf.real(conved)
def compute_tensorflow_spectrogram(waveform, window_size, hop_size):
    
    waveform_ = tf.placeholder(tf.float32)
    window_fn = functools.partial(tf.signal.hann_window, periodic=True)
    stft = tf.signal.stft(
        waveform_, window_size, hop_size, window_fn=window_fn)
    gram = tf.real(stft * tf.conj(stft))
    
    with tf.Session() as sess:
        
        print('Computing TensorFlow spectrogram...')
        start_time = time.time()
        g = sess.run(gram, feed_dict={waveform_: waveform})
        end_time = time.time()
        print('Done.')
        
    report_performance(g, start_time, end_time)
        
    return g
def bilinear_pool(x, output_size):
    """ Computes approximation of bilinear pooling with respect to x1, x2.
    For detailed explaination, see the paper (https://arxiv.org/abs/1511.06062)

    Args:
      x1: A `Tensor` with shape (batch_size, x1_size).
      x2: A `Tensor` with shape ((batch_size, x2_size).
      output_size: Output projection size. (`int`)

    Returns:
       A Tensor with shape (batch_size, output_size).
    """

    #this equals to
    # p1 = count_sketch(x1, output_size)
    # p2 = count_sketch(x2, output_size)
    # p3 = count_sketch(x3, output_size)
    #
    # pc1 = tf.complex(p1, tf.zeros_like(p1))
    # pc2 = tf.complex(p2, tf.zeros_like(p2))
    # pc3 = tf.complex(p3, tf.zeros_like(p3))
    #

    ps = []
    for v in x:
        p = count_sketch(v, output_size)
        ps.append(p)

    pcs = []

    for p in ps:
        pc = tf.complex(p, tf.zeros_like(p))
        pcs.append(pc)

    #this equals to : conved = tf.ifft(tf.fft(pc1) * tf.fft(pc2) * tf.fft(pc3))

    ffts = 1
    for pc in pcs:
        ffts *= tf.fft(pc)

    conved = tf.ifft(ffts)
    return tf.real(conved)
Exemple #23
0
def call_with_scalar_mass_matrix_evaluator(f, *args):
    """Returns f(evaluator, *args), with `evaluator` a mass matrix evaluator.

  Here, `evaluator` is a TensorFlow-based evaluator function
  that maps a 70-vector to a scalar mass matrix.

  We are then passing this on to a function that gets run in TensorFlow
  session context. This way, we can bulk-process solutions.
  """
    graph = tf.Graph()
    with graph.as_default():
        tf_scalar_evaluator = scalar_sector_tensorflow.get_tf_scalar_evaluator(
        )
        t_left_onb = tf.Variable(  # Uses orthonormal basis.
            initial_value=numpy.zeros([70]),
            trainable=False,
            dtype=tf.float64)
        t_input = tf.placeholder(tf.float64, shape=[70])
        t_v70 = tf.Variable(initial_value=numpy.zeros([70]),
                            trainable=False,
                            dtype=tf.float64)
        op_assign_input = tf.assign(t_v70, t_input)
        sinfo = tf_scalar_evaluator(
            tf.cast(t_v70, tf.complex128),
            t_left=tf.cast(
                tf.einsum('vV,V->v', tf.constant(algebra.e7.v70_from_v70o),
                          t_left_onb), tf.complex128))
        t_potential = sinfo.potential
        t_scalar_mass_matrix = (
            tf.real(tf.hessians([t_potential], [t_left_onb])[0]) *
            # Factor -3/8 (rather than -3/4) is due to normalization of
            # our orthonormal basis.
            (-3.0 / 8) / t_potential)
        with tf.Session() as sess:
            sess.run([tf.global_variables_initializer()])

            def evaluator(v70):
                sess.run([op_assign_input], feed_dict={t_input: v70})
                ret = sess.run([t_scalar_mass_matrix])[0]
                return ret

            return f(evaluator, *args)
def compute_fft(x, direction="C2C", inverse=False):

    if direction == 'C2R':
        inverse = True

    x_shape = x.get_shape().as_list()
    h, w = x_shape[-2], x_shape[-3]

    x_complex = tf.complex(x[..., 0], x[..., 1])

    if direction == 'C2R':
        out = tf.real(tf.ifft2d(x_complex)) * h * w
        return out

    else:
        if inverse:
            out = stack_real_imag(tf.ifft2d(x_complex)) * h * w
        else:
            out = stack_real_imag(tf.fft2d(x_complex))
        return out
Exemple #25
0
    def tensor_product(self, P, ch1, Q, ch2):
        P_hat = self.fft(tf.complex(P, tf.zeros(tf.shape(P), dtype=tf.float64)))
        Q_hat = self.fft(tf.complex(Q, tf.zeros(tf.shape(Q), dtype=tf.float64)))
        p_hat_list = [tf.squeeze(p) for p in tf.split(P_hat, self.k, axis=-1)]
        q_hat_list = [tf.squeeze(q) for q in tf.split(Q_hat, self.k, axis=-1)]

        if ch1 == 't' and ch2 == 't':
            S_hat = tf.concat([tf.expand_dims(tf.matmul(tf.transpose(p_hat), tf.transpose(q_hat)), axis=-1)
                               for (p_hat, q_hat) in zip(p_hat_list, q_hat_list)], axis=-1)
        elif ch1 == 't':
            S_hat = tf.concat([tf.expand_dims(tf.matmul(tf.transpose(p_hat), q_hat), axis=-1)
                               for (p_hat, q_hat) in zip(p_hat_list, q_hat_list)], axis=-1)
        elif ch2 == 't':
            S_hat = tf.concat([tf.expand_dims(tf.matmul(p_hat, tf.transpose(q_hat)), axis=-1)
                               for (p_hat, q_hat) in zip(p_hat_list, q_hat_list)], axis=-1)
        else:
            S_hat = tf.concat([tf.expand_dims(tf.matmul(p_hat, q_hat), axis=-1)
                               for (p_hat, q_hat) in zip(p_hat_list, q_hat_list)], axis=-1)

        return tf.real(self.ifft(S_hat))
Exemple #26
0
    def channel(x, snr):
        inter_shape = tf.shape(x)
        # reshape array to [-1, dim_z]args
        y = tf.layers.flatten(x)

        # convert from snr to std
        noise_stddev = np.sqrt(10**(-snr / 10))

        # Add channel noise
        dim_y = tf.shape(y)[1]
        # normalize latent vector so that the average power is 1
        y_in = (tf.sqrt(tf.cast(dim_y, dtype=tf.float32)) *
                tf.nn.l2_normalize(y, axis=1))
        y_out = real_awgn(y_in, noise_stddev)

        # convert signal back to intermediate shape
        y = tf.reshape(y_in, inter_shape)
        # compute average power
        avg_power = tf.reduce_mean(tf.real(y_in * tf.conj(y_in)))
        return y, avg_power
Exemple #27
0
    def _call(self, inputs):
        self_vecs, neigh_vecs = inputs

        neigh_vecs = tf.nn.dropout(neigh_vecs, 1-self.dropout)
        self_vecs = tf.nn.dropout(self_vecs, 1-self.dropout)
        
#         neigh_vecs = tf.py_func(assign_array, [neigh_vecs, self.adj, 0], tf.float32) 
        expanded_adj = tf.tile (tf.expand_dims(self.adj, -1) , [1, 1, self.input_dim])
        neigh_vecs = tf.complex(expanded_adj * tf.real(neigh_vecs),  expanded_adj * tf.imag(neigh_vecs))
        means = tf.reduce_sum(tf.concat([neigh_vecs, tf.expand_dims(self_vecs, axis=1)], axis=1), 
                                axis=1)/tf.reshape(tf.reduce_sum(self.adj, axis=1), (-1,1))
        
        # [nodes] x [out_dim]
        output = tf.matmul(means, self.vars['neigh_weights'])

        # bias
        if self.bias:
            output += self.vars['bias']
       
        return self.act(output)
Exemple #28
0
def one_hot_add(inputs, shift):
    """Performs (inputs + shift) % vocab_size in the one-hot space.

  Args:
    inputs: Tensor of shape `[..., vocab_size]`. Typically a soft/hard one-hot
      Tensor.
    shift: Tensor of shape `[..., vocab_size]`. Typically a soft/hard one-hot
      Tensor specifying how much to shift the corresponding one-hot vector in
      inputs. Soft values perform a "weighted shift": for example,
      shift=[0.2, 0.3, 0.5] performs a linear combination of 0.2 * shifting by
      zero; 0.3 * shifting by one; and 0.5 * shifting by two.

  Returns:
    Tensor of same shape and dtype as inputs.
  """
    # Compute circular 1-D convolution with shift as the kernel.
    inputs = tf.cast(inputs, tf.complex64)
    shift = tf.cast(shift, tf.complex64)
    return tf.real(tf.signal.ifft(
        tf.signal.fft(inputs) * tf.signal.fft(shift)))
Exemple #29
0
 def one_nn_iteration(self, xe_c2v_pre_iter, xe_0, alpha, beta):
     xe_c_sumv = self.staircase_quantizer(
         tf.add(tf.matmul(self.H_x_to_xe0, xe_0),
                alpha * tf.matmul(self.H_sumV_to_C, xe_c2v_pre_iter)), beta)
     xe_sign = tf.to_float(tf.sign(xe_c_sumv))
     xe_sum_log_img = tf.matmul(
         self.H_sumC_to_V,
         tf.multiply(tf.truediv((1 - xe_sign), [2.0]), [3.1415926]))
     xe_sum_log_complex = tf.complex(
         tf.zeros([self.num_all_edges, self.batch_size]), xe_sum_log_img)
     xe_product = tf.real(tf.exp(xe_sum_log_complex))
     xe_product_temp = tf.multiply(tf.sign(xe_product), -2e-7)
     xe_pd_modified = tf.add(xe_product, xe_product_temp)
     xe_v_minc = tf.segment_min(
         tf.abs(tf.gather(xe_c_sumv, self.h_minC_to_V)),
         self.h_min_segmentIDs)
     xe_v_sumc = tf.multiply(xe_pd_modified, xe_v_minc)
     bp_out_llr = tf.add(
         xe_0, alpha * tf.matmul(self.H_xe_v_sumc_to_y, xe_v_sumc))
     return bp_out_llr
def compute_fft(x, direction="C2C", inverse=False):

    if direction == 'C2R':
        inverse = True

    x_shape = x.get_shape().as_list()
    h, w = x_shape[-2], x_shape[-3]

    x_complex = tf.complex(x[..., 0], x[..., 1])

    if direction == 'C2R':
        out = tf.real(tf.ifft2d(x_complex)) * h * w
        return out

    else:
        if inverse:
            out = stack_real_imag(tf.ifft2d(x_complex)) * h * w
        else:
            out = stack_real_imag(tf.fft2d(x_complex))
        return out
def griffin_lim(spec, 
                frame_length,
                frame_step,
                num_fft,
                num_iters = 1):
                #num_iters = 20):
                #num_iters = 10):
  invert_spec = lambda spec : tf.contrib.signal.inverse_stft(spec, frame_length, frame_step, num_fft)

  spec_mag = tf.cast(tf.abs(spec), dtype=tf.complex64)
  best = tf.identity(spec)
  for i in range(num_iters):
    samples = invert_spec(best)
    est = tf.contrib.signal.stft(samples, frame_length, frame_step, num_fft, pad_end = False)  # (1, T, n_fft/2+1)
    phase = est / tf.cast(tf.maximum(1e-8, tf.abs(est)), tf.complex64) 
    best = spec_mag * phase
  X_t = invert_spec(best)
  y = tf.real(X_t)
  y = cast_float(y)
  return y
Exemple #32
0
def bilinear_pool(x1, x2, output_size):
    """ Computes approximation of bilinear pooling with respect to x1, x2.
    For detailed explaination, see the paper (https://arxiv.org/abs/1511.06062)

    Args:
      x1: A `Tensor` with shape (batch_size, x1_size).
      x2: A `Tensor` with shape ((batch_size, x2_size).
      output_size: Output projection size. (`int`)

    Returns:
       A Tensor with shape (batch_size, output_size).
    """

    p1 = count_sketch(x1, output_size)
    p2 = count_sketch(x2, output_size)
    pc1 = tf.complex(p1, tf.zeros_like(p1))
    pc2 = tf.complex(p2, tf.zeros_like(p2))

    conved = tf.batch_ifft(tf.batch_fft(pc1) * tf.batch_fft(pc2))
    return tf.real(conved)
Exemple #33
0
    def run(self, hr_img, lr_img):
        self.train_op = tf.train.AdamOptimizer(
            learning_rate=self.learning_rate).minimize(self.loss)
        self.sess.run(tf.global_variables_initializer())
        print('run: ->', hr_img.shape)
        # shape = np.zeros(hr_img.shape)
        # err_ = []
        # print(shape)
        for er in range(self.epoch):
            # image = tf.reshape(image,[image.shape[0],image.shape[1]])
            _, x = self.sess.run([self.train_op, self.loss],
                                 feed_dict={
                                     self.images: lr_img,
                                     self.label: hr_img
                                 })
            # source = self.sess.run([self.source_fft],feed_dict={self.images: lr_img, self.label:hr_img})
            # imshow_spectrum(np.squeeze(source))

            # _residual = self.sess.run([self.label_risidual],feed_dict={self.images: lr_img, self.label:hr_img})
            # _r = tf.abs(tf.ifft2d(np.squeeze(_residual)))
            # # imshow_spectrum(np.squeeze(_residual))
            # # print(np.abs(_residual))
            # plt_imshow(np.squeeze(self.sess.run(_r)))

            print(x)
        # w = self.sess.run([self.spectral_c1],feed_dict={self.images: lr_img, self.label:hr_img})
        # w = np.asarray(w)
        # # w =np.squeeze(w)
        # # w = w /(1e3*1e-5)
        # print(w)
        # print('----')
        # print(w[:,:,:,0])
        # # imshow_spectrum(w)

    # #
        result = self.pred.eval({self.images: lr_img, self.label: hr_img})
        result = np.squeeze(self.sess.run(tf.real(tf.ifft2d(result))))
        # result = result*255/(1e3*1e-5)
        # result = np.clip(result, 0.0, 255.0).astype(np.uint8)
        plt_imshow(((result)))
        print(np.abs(result))
Exemple #34
0
def inverse_filter(blurred, estimate, psf, gamma=None, otf=None, init_gamma=1.5):
    """Implements Weiner deconvolution in the frequency domain, with circular boundary conditions.
    Args:
        blurred: image with shape (batch_size, height, width, num_img_channels)
        estimate: image with shape (batch_size, height, width, num_img_channels)
        psf: filters with shape (kernel_height, kernel_width, num_img_channels, num_filters)
    TODO precompute OTF, adj_filt_img.
    """
    img_shape = blurred.shape.as_list()

    if gamma is None:
        gamma_initializer = tf.constant_initializer(init_gamma)
        gamma = tf.get_variable(name="wiener_gamma",
                                shape=(),
                                dtype=tf.float32,
                                trainable=True,
                                initializer=gamma_initializer)
        gamma = tf.square(gamma)
        tf.summary.scalar('gamma', gamma)

    a_tensor_transp = tf.transpose(blurred, [0, 3, 1, 2])
    estimate_transp = tf.transpose(estimate, [0, 3, 1, 2])
    # Everything has shape (batch_size, num_channels, height, width)
    img_fft = tf.fft2d(tf.complex(a_tensor_transp, 0.))
    if otf is None:
        otf = psf2otf(psf, output_size=img_shape[1:3])
        otf = tf.transpose(otf, [2, 3, 0, 1])
    # otf: [input_channels, output_channels, height, width]
    # img_fft: [batch_size, channels, height, width]
    adj_conv = img_fft * tf.conj(otf)
    numerator = adj_conv + tf.fft2d(tf.complex(gamma * estimate_transp, 0.))

    kernel_mags = tf.square(tf.abs(otf))

    denominator = tf.complex(kernel_mags + gamma, 0.0)
    filtered = tf.div(numerator, denominator)
    cplx_result = tf.ifft2d(filtered)
    real_result = tf.real(cplx_result)
    # Get back to (batch_size, num_channels, height, width)
    result = tf.transpose(real_result, [0, 2, 3, 1])
    return result
Exemple #35
0
    def call(self, inputs, state):
        """The most basic URNN cell.
        Args:
            inputs (Tensor - batch_sz x num_in): One batch of cell input.
            state (Tensor - batch_sz x num_units): Previous cell state: COMPLEX
        Returns:
        A tuple (outputs, state):
            outputs (Tensor - batch_sz x num_units*2): Cell outputs on the whole batch.
            state (Tensor - batch_sz x num_units): New state of the cell.
        """
        #print("cell.call inputs:", inputs.shape, inputs.dtype)
        #print("cell.call state:", state.shape, state.dtype)

        # prepare input linear combination
        inputs_mul = tf.matmul(inputs, tf.transpose(self.w_ih)) # [batch_sz, 2*num_units]
        inputs_mul_c = tf.complex( inputs_mul[:, :self._num_units],
                                   inputs_mul[:, self._num_units:] )
        # [batch_sz, num_units]

        with tf.name_scope("hidden"):
            # prepare state linear combination (always complex!)
            state_c = tf.complex( state[:, :self._num_units],
                                  state[:, self._num_units:] )

            state_mul = self.D.mul(state_c)
            state_mul = self.R.mul(state_mul)

            # [batch_sz, num_units]

            # calculate preactivation
            preact = inputs_mul_c + state_mul
            # [batch_sz, num_units]

        new_state_c = mat.modReLU(preact, self.b_h) # [batch_sz, num_units] C
        new_state = tf.concat([tf.real(new_state_c), tf.imag(new_state_c)], 1) # [batch_sz, 2*num_units] R
        # outside network (last dense layer) is ready for 2*num_units -> num_out
        output = new_state
        # print("cell.call output:", output.shape, output.dtype)
        # print("cell.call new_state:", new_state.shape, new_state.dtype)

        return output, new_state
Exemple #36
0
def casimir(wavefunc, bosonic, dbosonic, dfermionic):
    """Evaluates the casimir density diven dbosonic = dg bosonic, dfermionic = dg fermionic(bosonic).

    Arguments:
        wavefunc (Wavefunction): the wavefunction that gives the mapping from bosonic coordinates to fermionic states
        bosonic, dbosonic (tensor of shape (batch_size, num_bosonic_matrices, N, N)): the bosonic matrix coordinates
            and its infinitesimal change
        dfermionic (tensor of shape (batch_size, rank, num_fermions, num_femionic_matrices, N, N) or None):
            infinitesimal change of the fermionic state, and None if no fermions

    Returns:
        casimir (tensor of shape (batch_size,)): value of the casimir at bosonic 
    """
    # bosonic contribution
    s, log_norm, state = dstate(wavefunc, bosonic, dbosonic, dfermionic)
    casimir = tf.square(tf.gradients(log_norm, s)[0])
    # fermionic contribution
    if state is not None:
        s_, _, state_ = dstate(wavefunc, bosonic, dbosonic, dfermionic)
        casimir = casimir + tf.gradients(tf.gradients(tf.real(overlap(state, state_)), s), s_)[0]
    return casimir
def compute_tensorflow_spectrogram(waveform, window_size, hop_size):

    waveform_ = tf.placeholder(tf.float32)
    window_fn = functools.partial(tf.signal.hann_window, periodic=True)
    stft = tf.signal.stft(waveform_,
                          window_size,
                          hop_size,
                          window_fn=window_fn)
    gram = tf.real(stft * tf.conj(stft))

    with tf.Session() as sess:

        print('Computing TensorFlow spectrogram...')
        start_time = time.time()
        g = sess.run(gram, feed_dict={waveform_: waveform})
        end_time = time.time()
        print('Done.')

    report_performance(g, start_time, end_time)

    return g
Exemple #38
0
def FhRh(freq, mask, name='FhRh', is_normalized=False):
	with tf.variable_scope(name+'_scope'):
		# Convert from 2 channel to complex number
		freq = tf_complex(freq)
		mask = tf_complex(mask) 

		# Under sample
		condition = tf.cast(tf.real(mask)>0.9, tf.bool)
		freq_full = freq
		freq_zero = tf.zeros_like(freq_full)
		freq_dest = tf.where(condition, freq_full, freq_zero, name='RfFf')

		# Inverse Fourier Transform
		image 	  = tf.ifft2d(freq_dest, name='FtRt')
		
		if is_normalized:
			image = tf.div(image, ((DIMX-1)*(DIMY-1)))

		# Convert from complex number to 2 channel
		image = tf_channel(image)
	return tf.identity(image, name)
 def _compareGradient(self, x):
     # x[:, 0] is real, x[:, 1] is imag.  We combine real and imag into
     # complex numbers. Then, we extract real and imag parts and
     # computes the squared sum. This is obviously the same as sum(real
     # * real) + sum(imag * imag). We just want to make sure the
     # gradient function is checked.
     with self.test_session():
         inx = tf.convert_to_tensor(x)
         real, imag = tf.split(1, 2, inx)
         real, imag = tf.reshape(real, [-1]), tf.reshape(imag, [-1])
         cplx = tf.complex(real, imag)
         cplx = tf.conj(cplx)
         loss = tf.reduce_sum(tf.square(tf.real(cplx))) + tf.reduce_sum(
             tf.square(tf.imag(cplx)))
         epsilon = 1e-3
         jacob_t, jacob_n = tf.test.compute_gradient(inx,
                                                     list(x.shape),
                                                     loss, [1],
                                                     x_init_value=x,
                                                     delta=epsilon)
     self.assertAllClose(jacob_t, jacob_n, rtol=epsilon, atol=epsilon)
Exemple #40
0
def tf_reconstruct_diffraction_pattern(real_norm, imag_norm, propagateTF):

    # real_norm *= 2 # between 0 and 2
    # imag_norm *= 2 # between 0 and 2

    # real_norm -= 1 # between -1 and 1
    # imag_norm -= 1 # between -1 and 1

    # propagate through wavefront
    wavefront = tf.complex(real=real_norm, imag=imag_norm)
    through_wf = propagateTF.setup_graph_through_wfs(wavefront)

    complex_object_retrieved = tf.complex(real=tf.real(through_wf),
                                          imag=tf.imag(through_wf))
    diffraction_pattern = tf.abs(
        tf_fft2(complex_object_retrieved, dimmensions=[1, 2]))**2

    diffraction_pattern = diffraction_pattern / tf.reduce_max(
        diffraction_pattern, keepdims=True,
        axis=[1, 2])  # normalize the diffraction pattern
    return diffraction_pattern
 def __init__(self,
              param,
              nbqubitinput,
              nbqubitgatesize,
              posfirstqubit,
              learningrate=0,
              momentum=0):
     self.momentum = momentum
     self.learningrate = learningrate
     self.posfirstqubit = posfirstqubit
     self.nbqubitGatesize = nbqubitgatesize
     self.nbqubitinput = nbqubitinput
     self.param = tf.get_variable("w" + str(len(self.gatelist)),
                                  initializer=param)
     #self.param = tf.get_variable("w", initializer=param)
     self.prev = tf.zeros_like(self.param)
     self.nesterovprev = tf.identity(self.param)
     self.rmsprev = tf.zeros_like(self.param)
     self.gatelist.append(self)
     self.time = 1
     self.amsgradprev = tf.real(tf.zeros_like(self.param))
Exemple #42
0
def preprocess(audio,
               sequence_length,
               target_offset,
               max_sequence_length=80000,
               scale='audio',
               frame_length=2048,
               hop_length=512,
               fft_length=2048):
    audio = audio / tf.reduce_max(tf.abs(audio))
    length = tf.shape(audio)[0]
    if scale == 'audio':
        data = tf.expand_dims(audio, -1)
        n_frames = max_sequence_length
    else:
        stft = tf.contrib.signal.stft(audio,
                                      frame_length=frame_length,
                                      frame_step=hop_length,
                                      fft_length=fft_length,
                                      window_fn=partial(
                                          tf.contrib.signal.hann_window,
                                          periodic=True),
                                      pad_end=True)
        if scale == 'energy':
            data = tf.cast(tf.abs(stft), tf.float32)
        elif scale == 'power':
            data = tf.cast(
                tf.real(stft) * tf.real(stft) + tf.imag(stft) * tf.imag(stft),
                tf.float32)
        elif scale == 'log-power':
            data = 20.0 * tf.log(1e-10 + tf.cast(
                tf.real(stft) * tf.real(stft) +
                tf.imag(stft) * tf.imag(stft), tf.float32))
        elif scale == 'normalized-log-power':
            data = 20.0 * tf.log(1e-10 + tf.cast(
                tf.real(stft) * tf.real(stft) +
                tf.imag(stft) * tf.imag(stft), tf.float32))
            data = tf.minimum(1.0, tf.maximum(0.0, (data + 450.0) / 700.0))
        else:
            raise ValueError('scale: {} not supported'.format(scale))
        n_frames = max_sequence_length // hop_length - (frame_length //
                                                        hop_length)
    start = tf.random_uniform(
        (),
        maxval=n_frames - sequence_length - target_offset - 1,
        dtype=tf.int32)
    return data[start:start +
                sequence_length], data[start + target_offset:start +
                                       target_offset + sequence_length]
Exemple #43
0
    def logp(self, F, Y):
        """The Loss || V^_ijk - gik gjk* V^mod_ijk ||^2 / sigma_V^2 / 2
        F is"""

        #F is tec shape (Na,Nt,Nd)
        F = tf.reshape(F,(self.Na,self.Nt,self.Nd,1))

        #Na,Nt,Nd,Nf
        phi = self.c - 8.4480e9/self.freqs[None,None,None,:] * F

        #Nb,Nt,Nd,Nf
        phi_ij = tf.gather(phi,self.antenna2,axis=0) - tf.gather(phi,self.antenna1,axis=0)
        G = tf.exp(1j*tf.cast(phi_ij,tf.complex128))


        dY = tf.reduce_sum(self.Vmod * G, axis=2) - tf.reshape(Y,(self.Na,self.Nt,self.Nf))
        dY = tf.nn.dropout(dY,self.dropout)

        residuals_real = tf.sqrt(tf.square(tf.real(dY))/(self.var_scale * self.variance_V) + 1.) - 1.
        residuals_imag = tf.sqrt(tf.square(tf.imag(dY))/(self.var_scale * self.variance_V) + 1.) - 1.
        return tf.reduce_mean(residuals_real + residuals_imag)
Exemple #44
0
def complex_dropout(x, keep_prob, noise_shape=None, seed=None, name=None):
    '''
    Implementation of complex dropout based on tf.nn.dropout.
    '''
    with tf.name_scope(name, "complex_dropout", [x]) as name:
        # Early return if nothing needs to be dropped.
        if isinstance(keep_prob, float) and keep_prob == 1:
            return x

        noise_shape = _get_noise_shape(x, noise_shape)
        # uniform [keep_prob, 1.0 + keep_prob)
        random_tensor = keep_prob
        random_tensor += tf.random_uniform(noise_shape,
                                           seed=seed,
                                           dtype=tf.float32)
        # 0. if [keep_prob, 1.0) and 1. if [1.0, 1.0 + keep_prob)
        binary_tensor = tf.floor(random_tensor)
        ret = tf.complex(
            tf.div(tf.real(x), keep_prob) * binary_tensor,
            tf.div(tf.imag(x), keep_prob) * binary_tensor)
    return ret
Exemple #45
0
def loss_op(factors, energies):
    """
    Compute loss.

    Parameters
    ----------
    factors : Tensor of shape (N, system_size)
    energies : Tensor of shape (N,)

    Returns
    -------
    loss : tensor of shape (N,)
    """
    n = tf.cast(tf.shape(factors)[0], tf.complex64)
    energies = tf.cast(energies, tf.complex64)
    log_psi = tf.reduce_sum(factors, range(1, N_DIMS+1))
    log_psi_conj = tf.conj(log_psi)
    energy_avg = tf.reduce_sum(energies)/n
    loss = tf.reduce_sum(energies*log_psi_conj, 0)/n - \
        energy_avg * tf.reduce_sum(log_psi_conj, 0)/n
    return tf.real(loss)
Exemple #46
0
def LSEnet(model, Ip, u1p, u2p):
    # computation graph that defines least squared estimation of the electric field
    delta_Ep_pred = tf.cast(tf.tensordot(u1p, model.G1_real, axes=[[-1], [1]]) + tf.tensordot(u2p, model.G2_real, axes=[[-1], [1]]), tf.complex128) + \
      + 1j * tf.cast(tf.tensordot(u1p, model.G1_imag, axes=[[-1], [1]]) + tf.tensordot(u2p, model.G2_imag, axes=[[-1], [1]]), tf.complex128)
    delta_Ep_expand = tf.expand_dims(delta_Ep_pred, 2)
    delta_Ep_expand_diff = delta_Ep_expand[:, 1::
                                           2, :, :] - delta_Ep_expand[:, 2::
                                                                      2, :, :]
    y = tf.transpose(Ip[:, 1::2, :] - Ip[:, 2::2, :], [0, 2, 1])
    H = tf.concat(
        [2 * tf.real(delta_Ep_expand_diff), 2 * tf.imag(delta_Ep_expand_diff)],
        axis=2)
    H = tf.transpose(H, [0, 3, 1, 2])
    Ht_H = tf.matmul(tf.transpose(H, [0, 1, 3, 2]), H)
    Ht_H_inv_Ht = tf.matmul(
        tf.matrix_inverse(Ht_H + tf.eye(2, dtype=tf.float64) * 1e-12),
        tf.transpose(H, [0, 1, 3, 2]))
    x_new = tf.squeeze(tf.matmul(Ht_H_inv_Ht, tf.expand_dims(y, -1)), -1)

    n_observ = model.n_observ
    contrast_p = tf.reduce_mean(Ip, axis=2)

    d_contrast_p = tf.reduce_mean(tf.abs(delta_Ep_pred)**2, axis=2)

    Rp = tf.tensordot(
        tf.expand_dims(model.R0 + model.R1 * contrast_p + 4 *
                       (model.Q0 + model.Q1 * d_contrast_p) * contrast_p,
                       axis=-1),
        tf.ones((1, model.num_pix), dtype=tf.float64),
        axes=[[-1], [0]]) + 1e-24
    Rp = tf.transpose(Rp, [0, 2, 1])
    R_diff = Rp[:, :, 1::2] + Rp[:, :, 2::2]
    R = tf.matrix_set_diag(
        tf.concat([tf.expand_dims(tf.zeros_like(R_diff), -1)] *
                  (n_observ // 2), -1), R_diff)
    P_new = tf.matmul(tf.matmul(Ht_H_inv_Ht, R),
                      tf.transpose(Ht_H_inv_Ht, [0, 1, 3, 2]))
    Enp_pred_new = tf.cast(x_new[:, :, 0], dtype=tf.complex128) + 1j * tf.cast(
        x_new[:, :, 1], dtype=tf.complex128)
    return Enp_pred_new, P_new, H
Exemple #47
0
    def mfcc(self, signals):
        # `stfts` is a complex64 Tensor representing the Short-time Fourier Transform of
        # each signal in `signals`. Its shape is [batch_size, ?, fft_unique_bins]
        # where fft_unique_bins = fft_length // 2 + 1 = 513.
        stfts = tf.contrib.signal.stft(signals,
                                       frame_length=1024,
                                       frame_step=512,
                                       fft_length=1024)

        # A power spectrogram is the squared magnitude of the complex-valued STFT.
        # A float32 Tensor of shape [batch_size, ?, 513].
        power_spectrograms = tf.real(stfts * tf.conj(stfts))
        power_spectrum = tf.norm(power_spectrum, axis=2)

        # An energy spectrogram is the magnitude of the complex-valued STFT.
        # A float32 Tensor of shape [batch_size, ?, 513].
        magnitude_spectrograms = tf.abs(stfts)

        # Warp the linear-scale, magnitude spectrograms into the mel-scale.
        num_spectrogram_bins = magnitude_spectrograms.shape[-1].value
        lower_edge_hertz, upper_edge_hertz, num_mel_bins = 80.0, 7600.0, 64
        linear_to_mel_weight_matrix = tf.contrib.signal.linear_to_mel_weight_matrix(
            num_mel_bins, num_spectrogram_bins, sample_rate, lower_edge_hertz,
            upper_edge_hertz)
        mel_spectrograms = tf.tensordot(magnitude_spectrograms,
                                        linear_to_mel_weight_matrix, 1)
        # Note: Shape inference for `tf.tensordot` does not currently handle this case.
        mel_spectrograms.set_shape(
            magnitude_spectrograms.shape[:-1].concatenate(
                linear_to_mel_weight_matrix.shape[-1:]))

        log_offset = 1e-6
        log_mel_spectrograms = tf.log(mel_spectrograms + log_offset)

        num_mfccs = 13
        # Keep the first `num_mfccs` MFCCs.
        mfccs = tf.contrib.signal.mfccs_from_log_mel_spectrograms(
            log_mel_spectrograms)[..., :num_mfccs]

        return power_spectrum, mfcss
Exemple #48
0
    def mean_photon(self, mode, **kwargs):
        r"""
        Compute the mean photon number for the reduced state on the specified mode. May be numerical or symbolic.

        Args:
            mode (int): which subsystem to take the mean photon number of
            **kwargs: Optional keyword arguments.

                * If this contains the key
                  ``eval``, then the corresponding argument will be used to determine the return behaviour of this function.
                  When ``eval=True``, the return value is numerical; when ``eval=False``, it is symbolic.
                * If eval is not present in kwargs, then state falls back to the an internal evaluation behaviour,
                  which is specified at initialization.
                * A Tensorflow Session or feed_dict may also be passed via the keys ``session`` or ``feed_dict``, respectively.
                  These are ignored when ``eval=False``; when ``eval=True``, they are used when numerically evaluating the underlying quantity.
                  If session and/or feed_dict are not given, then a temporary session and/or empty feed_dict will be used.
        Returns:
            float/Tensor: the numerical value, or an unevaluated Tensor object, for the mean photon number.
        """
        with self.graph.as_default():
            rho = self.reduced_dm([mode])  # don't pass kwargs yet

            if not self.batched:
                rho = tf.expand_dims(rho, 0)  # add fake batch dimension

            n = np.diag(np.arange(self.cutoff_dim))
            flat_n = np.reshape(n, [1, self.cutoff_dim**2])

            flat_rho = tf.reshape(rho, [-1, self.cutoff_dim**2])

            nbar = tf.real(tf.reduce_sum(
                flat_rho * flat_n, axis=1))  # implements a batched tr(rho * n)

            if not self.batched:
                nbar = tf.squeeze(nbar, 0)  # drop fake batch dimension

            nbar = tf.identity(nbar, name="mean_photon")
            nbar = self._run(nbar, **kwargs)

            return nbar
Exemple #49
0
    def _getFullFieldProbeObjViews(self, obj_w_border_t: tf.Tensor,
                                   probe_cmplx_t: tf.Tensor,
                                   position_indices_t: tf.Tensor) -> tf.Tensor:
        """Precalculate the object positioning for each scan position.

        Assumes a small object that is translated within the dimensions of a full-field probe. For each scan
        position, we translate the object, then pad the object array to the size of the probe beam. For the padding,
        we assume free-space (transparent) propagation and use 1.0.

        In Tensorflow, performing the pad-and-stack procedure in the GPU for complex -valued arrays seems to be
        buggy. As a workaround, we separately pad-and-stack the real and imaginary parts of the object with 1.0 and
        0 respectively.

        Returns
        ----------
        obj_views : tensor(complex)
            Stack of tensors that correspond to the padded object at each object translation.
        """
        obj_real_pads = []
        obj_imag_pads = []

        ony, onx = obj_w_border_t.get_shape().as_list()
        pny, pnx = probe_cmplx_t.get_shape().as_list()

        padfn = lambda p, t, c: tf.pad(t, [[p[0], pny - (ony + p[0])],
                                           [p[1], pnx - (onx + p[1])]],
                                       constant_values=c)

        for i in range(position_indices_t.get_shape().as_list()[0]):
            p = self._scan_grid.positions_pix[i]
            padded_real = padfn(p, tf.real(obj_w_border_t), 1.0)
            padded_imag = padfn(p, tf.imag(obj_w_border_t), 0.)
            obj_real_pads.append(padded_real)
            obj_imag_pads.append(padded_imag)

        obj_real_pads_t = tf.stack(obj_real_pads)
        obj_imag_pads_t = tf.stack(obj_imag_pads)

        obj_views_t = tf.complex(obj_real_pads_t, obj_imag_pads_t)
        return obj_views_t
Exemple #50
0
def Circ_matmul(X, Wz, bsize):
	""" Compute matrix product Y = X*W
			Where X is IxM (stack of I 1xM vectors)
						W is MxN (single weight matrix)
						Y is IxN (stack of I 1xN vectors)

			W is composed of BxB circulant blocks, M and N divide B.
			The matmul is performed without constructing W explicitly

			X  = (I, 1, M)     -> (I, M/B,   1, B)
			Wz = (M/B, N/B, B) -> (1, M/B, N/B, B)
			Yz = (I, M/B, N/B, B) -> (I, N/B, B)
			Y  = (I, N)
	"""

	sx = X.get_shape().as_list()
	sW = Wz.get_shape().as_list()
	print(sx, sW)
	assert(len(sx) <= 2)
	assert(len(sW) == 3)
	M, N = sx[-1], sW[1]*bsize
	print("M, N", M, N)
	out_shape = [N] if len(sx) == 1 else [-1,N]

	Xz = tf.reshape(X,  [-1, M/bsize, 1, bsize])
	Wz = tf.reshape(Wz, [ 1, M/bsize, N/bsize, bsize])

	FXz = tf.ifft(tf.complex(Xz, tf.zeros_like(Xz)))
	FWz = tf.fft(tf.complex(Wz, tf.zeros_like(Wz)))
	FYz = tf.reduce_sum(FXz*FWz, axis=1)
	Yz = tf.real( tf.fft(FYz) )
	#Yz = tf.fft(FYz)
	#Cz = tf.imag(Yz)
	#Cz = tf.Print(Cz, [Cz], message="dense Cz", first_n=2, summarize=10)
	#Yz = tf.real(Yz)
	#Yz = tf.Print(Yz, [Yz], message="dense Yz", first_n=2, summarize=10)
	#Yz = Yz + Cz

	Y  = tf.reshape(Yz, out_shape)
	return Y
def sobolev_filter(x, c=5, s=1):
    """Apply sobolev filter.

    Parameters
    ----------
    x : tensorflow.Tensor of shape B W H C
        txt
    c : float
        Scaling of the cooridinate systems (1 / pixel size)
    s : float
        Order of the Sobolev norm
    """
    with tf.name_scope('sobolev'):
        # FFT is taken over the innermost axes, so move channel to beginning.
        x = tf.transpose(x, [0, 3, 1, 2])
        fft_x = tf.spectral.fft2d(tf.cast(x, 'complex64'))

        shape = tf.shape(fft_x)
        sx = shape[3]
        sy = shape[2]

        # Construct meshgrid for the scale
        x = tf.range(sx)
        x = tf.minimum(x, sx - x)
        x = tf.cast(x, dtype='complex64') / tf.cast(sx // 2, dtype='complex64')
        y = tf.range(sy)
        y = tf.minimum(y, sy - y)
        y = tf.cast(y, dtype='complex64') / tf.cast(sy // 2, dtype='complex64')
        X, Y = tf.meshgrid(x, y)
        X = X[None, None]
        Y = Y[None, None]

        scale = (1 + c * (X**2 + Y**2))**(s / 2)

        # Compute spatial gradient in fourier space
        fft_x = scale * fft_x

        result_x = tf.spectral.ifft2d(fft_x)
        result_x = tf.real(result_x)
        return tf.transpose(result_x, [0, 2, 3, 1])
Exemple #52
0
def Circ_conv2d(X, Wz, ksize, bsize, padding="SAME"):
	""" Compute Y = conv2d(X, W)
			X is (I, R, C, M)
			W is (K, K, M, N)
			Y is (I, R, C, N)

			W(i,j,:,:) is block-circulant

			Xi = (I, R, C, K*K*M)   -> (I*R*C, K*K*M/B,   1, B)
			Wz = (K*K, M/B, N/B, B) -> (    1, K*K*M/B, N/B, B)
			Yz = (I*R*C, K*K*M/B, N/B, B) -> (I*R*C, N/B, B)
	"""
	sX = X.get_shape().as_list()
	sW = Wz.get_shape().as_list()
	assert(len(sX) == 4)
	assert(len(sW) == 4)
	assert(len(ksize) == 2)
	R, C, M, N = sX[1], sX[2], sX[3], sW[2]*bsize
	K1, K2 = ksize[0], ksize[1]

	Xi = tf.extract_image_patches(X, [1,K1,K2,1], strides=[1,1,1,1],
															 rates=[1,1,1,1], padding=padding)
	sXi = Xi.get_shape().as_list()
	Rn, Cn = sXi[1], sXi[2]
	Xz = tf.reshape(Xi, [-1, K1*K2*M/bsize, 1, bsize])
	Wz = tf.reshape(Wz,  [1, K1*K2*M/bsize, N/bsize, bsize])

	FXz = tf.ifft(tf.complex(Xz, tf.zeros_like(Xz)))
	FWz = tf.fft (tf.complex(Wz, tf.zeros_like(Wz)))
	FYz = tf.einsum('abcd,ebfd->afd', FXz, FWz)
	Yz = tf.real( tf.fft(FYz) )
	#Yz = tf.fft(FYz)
	#Cz = tf.imag(Yz)
	#Cz = tf.Print(Cz, [Cz], message="conv Cz", first_n=2, summarize=10)
	#Yz = tf.real(Yz)
	#Yz = tf.Print(Yz, [Yz], message="conv Yz", first_n=2, summarize=10)
	#Yz = Yz + Cz

	Y = tf.reshape(Yz, [-1, Rn, Cn, N])
	return Y
Exemple #53
0
def restore_helper_power_method(tensors,
                                init=None,
                                precision=1E-12,
                                nmax=100000,
                                pinv=1E-30):

    """
    Helper function for putting InfiniteMPSCentralGauge into central form using
    the power method

    Parameters:
    ------------------------------
    init:          tf.tensor
                   initial guess for the eigenvector
    precision:     float
                   desired precision of the dominant eigenvalue
    nmax:          int
                   max number of iterations
    pinv:          float
                   pseudoinverse cutoff

    Returns:
    ----------------------------------
    (As,mat,connector,right_mat)

    As:         list of tf.Tensors
    mat:        tf.Tensor 
    connector:  tf.Tensor 
    right_mat:  tf.Tensor 
    """


    As=copy.copy(tensors)#[t for t in tensors] #won't compile without this
    newAs=[]
    if not np.all(As[0].dtype==t.dtype for t in As):
        raise TypeError('TMeigs_power_method: all As have to have the same dtype')
    dtype=As[0].dtype

    if init:
        x = init
    else:
        x = tf.diag(tf.ones(shape=[As[0].shape[0]],dtype=As[0].dtype))
    if not As[0].dtype==x.dtype:
        raise TypeError('TMeigs_power_method: `init` has other dtype than `As`')

    x/=tf.linalg.norm(x)
    dtype=x.dtype
    def do_step_left(n,eta,state,diff,):
        newstate=transfer_op(As, As, 'l', state)
        eta=tf.linalg.norm(newstate)
        newstate/=eta
        diff=tf.cast(tf.linalg.norm(state-newstate),dtype.real_dtype)
        return n+1,eta,newstate,diff

    def do_step_right(n,eta,state,diff,):
        newstate=transfer_op(As, As, 'r', state)
        eta=tf.linalg.norm(newstate)
        newstate/=eta
        diff=tf.cast(tf.linalg.norm(state-newstate),dtype.real_dtype)
        return n+1,eta,newstate,diff
    
    def stopping_criterion(n,eta,state,diff):
        return tf.less(tf.cast(precision,dtype.real_dtype),diff)

    def cond(n,eta,state,diff):
        return tf.cond(tf.less(0,n),lambda: tf.cond(tf.less(n,nmax),lambda: stopping_criterion(n,eta,state,diff),lambda:False),lambda:True)

    _,eta,l,_=tf.while_loop(cond,do_step_left,(0,tf.cast(0.0,dtype),x,tf.cast(1.0,dtype.real_dtype)))
    _,eta,r,_=tf.while_loop(cond,do_step_right,(0,tf.cast(0.0,dtype),x,tf.cast(1.0,dtype.real_dtype)))

    sqrteta = tf.cast(tf.sqrt(tf.real(eta)), dtype)
    As[0] /= sqrteta

    l = l / tf.trace(l)
    l = (l + tf.conj(tf.transpose(l))) / 2.0

    eigvals_left, u_left = tf.linalg.eigh(l)

    eigvals_left /= tf.reduce_sum(eigvals_left,axis=0)
    abseigvals_left = tf.abs(eigvals_left)
    mask = tf.greater(abseigvals_left, pinv)
    eigvals_left = tf.where(mask, eigvals_left,
                            tf.zeros(eigvals_left.shape, dtype=dtype))
    inveigvals_left = tf.where(
        mask, 1.0 / eigvals_left,
        tf.zeros(eigvals_left.shape, dtype=dtype))

    y = ncon([u_left, tf.diag(tf.sqrt(eigvals_left))],
                 [[-2, 1], [1, -1]])
    invy = ncon([tf.diag(tf.sqrt(inveigvals_left)),
                     tf.conj(u_left)], [[-2, 1], [-1, 1]])

    r = r / tf.trace(r)
    r = (r + tf.conj(tf.transpose(r))) / 2.0

    eigvals_right, u_right = tf.linalg.eigh(r)

    eigvals_right /= tf.reduce_sum(eigvals_right,axis=0)
    abseigvals_right = tf.abs(eigvals_right)
    mask = tf.greater(abseigvals_right, pinv)
    eigvals_right = tf.where(
        mask, eigvals_right, tf.zeros(
            eigvals_right.shape, dtype=dtype))
    inveigvals_right = tf.where(
        mask, 1.0 / eigvals_right,
        tf.zeros(eigvals_right.shape, dtype=dtype))

    x = ncon([u_right, tf.diag(tf.sqrt(eigvals_right))],
                 [[-1, 1], [1, -2]])
    invx = ncon([tf.diag(tf.sqrt(inveigvals_right)),
                     tf.conj(u_right)], [[-1, 1], [-2, 1]])
    lam, U, V = tf.linalg.svd(ncon([y, x], [[-1, 1], [1, -2]]))
    lam = tf.cast(lam, dtype)


    As[0] = ncon(#absorb everything on the left end 
        [tf.diag(lam), tf.conj(V), invx, As[0]],
        [[-1, 1], [2, 1], [2, 3],[3, -2, -3]])
    As[-1] = ncon(
        [As[-1], invy, U],
        [[-1, -2, 1], [1, 2], [2, -3]])

    for n in range(len(As)-1):
        tensor, mat, _ = prepare_tensor_QR(
            As[n], direction=1)
        As[n] = tensor
        As[n + 1] = ncon(
            [mat, As[n + 1]],
            [[-1, 1], [1, -2, -3]])


    Z = ncon(
        [As[-1], tf.conj(As[-1])],
        [[1, 2, 3], [1, 2, 3]]) / tf.cast(As[-1].shape[2], dtype)
    As[-1] /= tf.sqrt(Z)
    lam = lam / tf.linalg.norm(lam)
    mat = tf.diag(lam)
    connector = tf.diag(1.0 / lam)
    right_mat = tf.diag(lam)
    return As,mat,connector,right_mat  
Exemple #54
0
def restore_helper(tensors,
                   init=None,
                   precision=1E-12,
                   ncv=50,
                   nmax=100000,
                   numeig=1,
                   pinv=1E-30):
    
    """
    Helper function for putting InfiniteMPSCentralGauge into central form


    Parameters:
    ------------------------------
    init:          tf.tensor
                   initial guess for the eigenvector
    precision:     float
                   desired precision of the dominant eigenvalue
    ncv:           int
                   number of Krylov vectors
    nmax:          int
                   max number of iterations
    numeig:        int
                   hyperparameter, passed to scipy.sparse.linalg.eigs; number of eigenvectors 
                   to be returned by scipy.sparse.linalg.eigs; leave at 6 to avoid problems with arpack
    pinv:          float
                   pseudoinverse cutoff

    Returns:
    ----------------------------------
    (As,mat,connector,right_mat)

    As:         list of tf.Tensors
    mat:        tf.Tensor 
    connector:  tf.Tensor 
    right_mat:  tf.Tensor 
    """
    As=copy.copy(tensors)#[t for t in tensors] #won't compile without this
    
    if not np.all(As[0].dtype==t.dtype for t in As):
        raise TypeError('TMeigs_power_method: all As have to have the same dtype')
    dtype=As[0].dtype
    
    eta, l = TMeigs(tensors=As,
                    direction='left',
                    init=init,
                    nmax=nmax,
                    precision=precision,
                    ncv=ncv,
                    numeig=numeig)
        
    sqrteta = tf.cast(tf.sqrt(tf.real(eta)), dtype)
    As[0] /= sqrteta

    l = l / tf.trace(l)
    l = (l + tf.conj(tf.transpose(l))) / 2.0

    eigvals_left, u_left = tf.linalg.eigh(l)

    eigvals_left /= tf.reduce_sum(eigvals_left,axis=0)
    abseigvals_left = tf.abs(eigvals_left)
    mask = tf.greater(abseigvals_left, pinv)
    eigvals_left = tf.where(mask, eigvals_left,
                            tf.zeros(eigvals_left.shape, dtype=dtype))
    inveigvals_left = tf.where(
        mask, 1.0 / eigvals_left,
        tf.zeros(eigvals_left.shape, dtype=dtype))

    y = ncon([u_left, tf.diag(tf.sqrt(eigvals_left))],
                 [[-2, 1], [1, -1]])
    invy = ncon([tf.diag(tf.sqrt(inveigvals_left)),
                     tf.conj(u_left)], [[-2, 1], [-1, 1]])


    
    eta, r = TMeigs(tensors=As,
                    direction='right',
                    init=init,
                    nmax=nmax,
                    precision=precision,
                    ncv=ncv,
                    numeig=numeig)

    r = r / tf.trace(r)
    r = (r + tf.conj(tf.transpose(r))) / 2.0

    eigvals_right, u_right = tf.linalg.eigh(r)

    eigvals_right /= tf.reduce_sum(eigvals_right,axis=0)
    abseigvals_right = tf.abs(eigvals_right)
    mask = tf.greater(abseigvals_right, pinv)
    eigvals_right = tf.where(
        mask, eigvals_right, tf.zeros(
            eigvals_right.shape, dtype=dtype))
    inveigvals_right = tf.where(
        mask, 1.0 / eigvals_right,
        tf.zeros(eigvals_right.shape, dtype=dtype))

    x = ncon([u_right, tf.diag(tf.sqrt(eigvals_right))],
                 [[-1, 1], [1, -2]])
    invx = ncon([tf.diag(tf.sqrt(inveigvals_right)),
                     tf.conj(u_right)], [[-1, 1], [-2, 1]])
    lam, U, V = tf.linalg.svd(ncon([y, x], [[-1, 1], [1, -2]]))
    lam = tf.cast(lam, dtype)
    
    As[0] = ncon(#absorb everything on the left end 
        [tf.diag(lam), tf.conj(V), invx, As[0]],
        [[-1, 1], [2, 1], [2, 3],[3, -2, -3]])
    As[-1] = ncon(
        [As[-1], invy, U],
        [[-1, -2, 1], [1, 2], [2, -3]])


    for n in range(len(As)-1):
        tensor, mat, _ = prepare_tensor_QR(
            As[n], direction=1)
        As[n] = tensor
        As[n + 1] = ncon(
            [mat, As[n + 1]],
            [[-1, 1], [1, -2, -3]])


    Z = ncon(
        [As[-1], tf.conj(As[-1])],
        [[1, 2, 3], [1, 2, 3]]) / tf.cast(As[-1].shape[2], dtype)
    As[-1] /= tf.sqrt(Z)
    lam = lam / tf.linalg.norm(lam)
    mat = tf.diag(lam)
    connector = tf.diag(1.0 / lam)
    right_mat = tf.diag(lam)
    
    return As,mat,connector,right_mat
def build_func():
    slices_tensor = tf.placeholder(dtype=tf.complex64, shape=[None, None])
    S_tensor = tf.placeholder(dtype=tf.complex64, shape=[None, None])
    envelope_tensor = tf.placeholder(dtype=tf.float32, shape=[None])
    ctf_tensor = tf.placeholder(dtype=tf.float32, shape=[None, None])
    d_tensor = tf.placeholder(dtype=tf.complex64, shape=[None, None])
    logW_S_tensor = tf.placeholder(dtype=tf.float32, shape=[None])
    logW_I_tensor = tf.placeholder(dtype=tf.float32, shape=[None])
    logW_R_tensor = tf.placeholder(dtype=tf.float32, shape=[None])
    div_in_tensor = tf.placeholder(dtype=tf.float32, shape=[])
    sigma2_coloured_tensor = tf.placeholder(dtype=tf.float32, shape=[None])

    cproj = tf.expand_dims(slices_tensor, 1) * tf.complex(ctf_tensor, tf.zeros_like(ctf_tensor)) # r * i * t
    cim = tf.expand_dims(S_tensor, 1) * d_tensor  # s * i * t
    correlation_I = tf.real(tf.expand_dims(cproj, 1)) * tf.real(cim) \
                    + tf.imag(tf.expand_dims(cproj, 1)) * tf.imag(cim)  # r * s * i * t
    power_I = tf.real(cproj) ** 2 + tf.imag(cproj) ** 2  # r * i * t

    g_I =tf.complex(envelope_tensor, tf.zeros_like(envelope_tensor)) * tf.expand_dims(cproj, 1) - cim  # r * s * i * t

    sigma2_I = tf.real(g_I) ** 2 + tf.imag(g_I) ** 2  # r * s * i * t

    tmp = tf.reduce_sum(sigma2_I / sigma2_coloured_tensor, reduction_indices=3)  # r * s * i

    e_I = div_in_tensor * tmp + logW_I_tensor  # r * s * i

    g_I *= tf.complex(ctf_tensor, tf.zeros_like(ctf_tensor))  # r * s * i * t


    etmp = my_logsumexp_tensorflow(e_I)  # r * s
    e_S = etmp + logW_S_tensor  # r * s

    tmp = logW_S_tensor + tf.expand_dims(logW_R_tensor, 1)  # r * s
    phitmp = tf.exp(e_I - tf.expand_dims(etmp, 2))  # r * s * i
    I_tmp = tf.expand_dims(tmp, 2) + e_I

    correlation_S = tf.reduce_sum(tf.expand_dims(phitmp, 3) * correlation_I, reduction_indices=2)  # r * s * t
    power_S = tf.reduce_sum(tf.expand_dims(phitmp, 3) * tf.expand_dims(power_I, 1), reduction_indices=2)  # r * s * t
    sigma2_S = tf.reduce_sum(tf.expand_dims(phitmp, 3) * sigma2_I, reduction_indices=2)  # r * s * t
    g_S = tf.reduce_sum(tf.complex(tf.expand_dims(phitmp, 3), tf.zeros_like(tf.expand_dims(phitmp, 3)))
        * g_I, reduction_indices=2)  # r * s * t

    etmp = my_logsumexp_tensorflow(e_S)  # r
    e_R = etmp + logW_R_tensor  # r

    tmp = logW_R_tensor  # r
    phitmp = tf.exp(e_S - tf.expand_dims(etmp, 1))  # r * s
    S_tmp = tf.expand_dims(tmp, 1) + e_S
    correlation_R = tf.reduce_sum(tf.expand_dims(phitmp, 2) * correlation_S, reduction_indices=1)  # r * t
    power_R = tf.reduce_sum(tf.expand_dims(phitmp, 2) * power_S, reduction_indices=1)  # r * t
    sigma2_R = tf.reduce_sum(tf.expand_dims(phitmp, 2) * sigma2_S, reduction_indices=1)  # r * t

    g = tf.reduce_sum(tf.complex(tf.expand_dims(phitmp, 2), tf.zeros_like(tf.expand_dims(phitmp, 2)))
        * g_S, reduction_indices=1)  # r * t

    tmp = -2.0 * div_in_tensor
    nttmp = tmp * envelope_tensor / sigma2_coloured_tensor

    e = my_logsumexp_tensorflow(e_R)
    lse_in = -e

    # Noise estimate
    phitmp = e_R - e  # r
    R_tmp = phitmp
    phitmp = tf.exp(phitmp)

    sigma2_est = tf.squeeze(tf.matmul(tf.expand_dims(phitmp, 0), sigma2_R), squeeze_dims=[0])
    correlation = tf.squeeze(tf.matmul(tf.expand_dims(phitmp, 0), correlation_R), squeeze_dims=[0])
    power = tf.squeeze(tf.matmul(tf.expand_dims(phitmp, 0), power_R), squeeze_dims=[0])

    global func
    global inputs
    global objectives
    func = tf.Session()
    inputs = [slices_tensor,
              S_tensor,
              envelope_tensor,
              ctf_tensor,
              d_tensor,
              logW_S_tensor,
              logW_I_tensor,
              logW_R_tensor,
              div_in_tensor,
              sigma2_coloured_tensor]
    objectives = [g, I_tmp, S_tmp, R_tmp, sigma2_est, correlation, power, nttmp, lse_in, phitmp]
def complex_mul_real( z, r ):
    return tf.complex(tf.real(z)*r, tf.imag(z)*r)
def abs2_c(z):
    return tf.real(z)*tf.real(z)+tf.imag(z)*tf.imag(z)
Exemple #58
0
    def _optimize_2s_local(self,
                           thresh=1E-10,
                           D=None,
                           ncv=40,
                           Ndiag=10,
                           landelta=1E-5,
                           landeltaEta=1E-5,
                           verbose=0):
        raise NotImplementedError()
        mpol = self.mpo[self.mpo.pos - 1]
        mpor = self.mpo[self.mpo.pos]
        Ml, Mc, dl, dlp = mpol.shape
        Mc, Mr, dr, drp = mpor.shape
        mpo = tf.reshape(
            ncon.ncon([mpol, mpor], [[-1, 1, -3, -5], [1, -2, -4, -6]]),
            [Ml, Mr, dl * dr, dlp * drp])
        initial = ncon.ncon([
            self.mps[self.mps.pos-1],
            self.mps.mat,
            self.mps[self.mps.pos]],
                            [[-1,-2,1],[1,2],[2,-3,-4]]
        )
        Dl,dl,dr,Dr=initial.shape
        tf.reshape(initial,[Dl,dl*dr,Dr])
        if self.walltime_log:
            t1=time.time()
            
        nit, vecs, alpha, beta = LZ.do_lanczos(
            L=self.left_envs[self.mps.pos - 1],
            mpo=mpo,
            R=self.right_envs[self.mps.pos],
            initial_state=initial,
            ncv=ncv,
            delta=landelta
        )
        if self.walltime_log:
            self.walltime_log(lan=[(time.time()-t1)/float(nit)]*int(nit),QR=[],add_layer=[],num_lan=[int(nit)])                        
        
        temp = tf.reshape(
            tf.reshape(opt, [
                self.mps.D[self.mps.pos - 1], dlp, drp,
                self.mps.D[self.mps.pos + 1]
            ]), [])
        opt.split(mps_merge_data).transpose(0, 2, 3, 1).merge([[0, 1], [2, 3]])

        U, S, V = temp.svd(truncation_threshold=thresh, D=D)
        Dnew = S.shape[0]
        if verbose > 0:
            stdout.write(
                "\rTS-DMRG it=%i/%i, sites=(%i,%i)/%i: optimized E=%.16f+%.16f at D=%i"
                % (self._it, self.Nsweeps, self.mps.pos - 1, self.mps.pos,
                   len(self.mps), tf.real(e), tf.imag(e), Dnew))
            stdout.flush()
        if verbose > 1:
            print("")
        
        Z = np.sqrt(ncon.ncon([S, S], [[1], [1]]))
        self.mps.mat = S.diag() / Z

        self.mps[self.mps.pos - 1] = U.split([merge_data[0],
                                              [U.shape[1]]]).transpose(0, 2, 1)
        self.mps[self.mps.pos] = V.split([[V.shape[0]],
                                          merge_data[1]]).transpose(0, 2, 1)
        self.left_envs[self.mps.pos] = self.add_layer(
            B=self.left_envs[self.mps.pos - 1],
            mps_tensor=self.mps[self.mps.pos - 1],
            mpo_tensor=self.mpo[self.mps.pos - 1],
            conj_mps_tensor=self.mps[self.mps.pos - 1],
            direction=1
        )
        
        self.right_envs[self.mps.pos - 1] = self.add_layer(
            B=self.right_envs[self.mps.pos],
            mps_tensor=self.mps[self.mps.pos],
            mpo_tensor=self.mpo[self.mps.pos],
            conj_mps_tensor=self.mps[self.mps.pos],
            direction=-1
        )
        return e
Exemple #59
0
 def test_Real(self):
     t = tf.real(self.random(3, 4, complex=True))
     self.check(t)
Exemple #60
0
	def _ccorr(self, a, b):
		a = tf.cast(a, tf.complex64)
		b = tf.cast(b, tf.complex64)
		return tf.real(tf.ifft(tf.conj(tf.fft(a)) * tf.fft(b)))