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.compat.v1.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 sinc_impulse_response(cutoff_frequency, window_size=512, sample_rate=None): """Get a sinc impulse response for a set of low-pass cutoff frequencies. Args: cutoff_frequency: Frequency cutoff for low-pass sinc filter. If the sample_rate is given, cutoff_frequency is in Hertz. If sample_rate is None, cutoff_frequency is normalized ratio (frequency/nyquist) in the range [0, 1.0]. Shape [batch_size, n_time, 1]. window_size: Size of the Hamming window to apply to the impulse. sample_rate: Optionally provide the sample rate. Returns: impulse_response: A series of impulse responses. Shape [batch_size, n_time, (window_size // 2) * 2 + 1]. """ # Convert frequency to samples/sample_rate [0, Nyquist] -> [0, 1]. if sample_rate is not None: cutoff_frequency *= 2.0 / float(sample_rate) # Create impulse response axis. half_size = window_size // 2 full_size = half_size * 2 + 1 idx = tf.range(-half_size, half_size + 1.0, dtype=tf.float32) idx = idx[tf.newaxis, tf.newaxis, :] # Compute impulse response. impulse_response = sinc(cutoff_frequency * idx) # Window the impulse response. window = tf.signal.hamming_window(full_size) window = tf.broadcast_to(window, impulse_response.shape) impulse_response = window * tf.real(impulse_response) # Normalize for unity gain. impulse_response /= tf.reduce_sum(impulse_response, axis=-1, keepdims=True) return impulse_response
mean=0.0, stddev=np.sqrt(2 / (num_filter_1))), name='wf4'), } biases_decoder1 = { 'bf1': tf.Variable(tf.zeros([num_filter_3]), name='bf1'), 'bf2': tf.Variable(tf.zeros([num_filter_2]), name='bf2'), 'bf3': tf.Variable(tf.zeros([num_filter_1]), name='bf3'), 'bf4': tf.Variable(tf.zeros([dim_feature * n_user]), name='bf4'), } # Construct model for u_ii in range(n_user): x_reshape = tf.reshape(x[:, :, :, u_ii], [-1, L_ * n_rx]) x_user = tf.concat([tf.real(x_reshape), tf.imag(x_reshape)], axis=1) Out_encoder_low, _ = Dnn_Encoder(x_user, weights_encoder1, biases_encoder1, train_flag) if u_ii == 0: Out_encoder_low_cat = Out_encoder_low else: Out_encoder_low_cat = tf.concat([Out_encoder_low_cat, Out_encoder_low], 1) # Transmitter DNN with quantization Out_decoder_low = Dnn_Decoder_low(Out_encoder_low_cat, weights_decoder1, biases_decoder1, train_flag) w_esti_low = tf.reshape(Out_decoder_low, [-1, n_tx, n_rx, n_user])
def build(self): self.audios = tf.placeholder(tf.float32, [self.batch_size, self.n_speaker, None], name='input_signals') self.mix_input = tf.reduce_sum(self.audios, axis=1) with tf.variable_scope("encoder"): # [batch, encode_len, channels] encoded_input = tf.layers.Conv1D( filters=self.config["model"]["filters"]["ae"], kernel_size=self.fft_len, strides=self.fft_hop, activation=tf.nn.relu, name="conv1d_relu")(tf.expand_dims(self.mix_input, -1)) stfts_mix = tf.signal.stft(self.mix_input, frame_length=self.fft_len, frame_step=self.fft_hop, fft_length=self.fft_len, window_fn=self.fft_wnd) magni_mix = tf.abs(stfts_mix) phase_mix = tf.atan2(tf.imag(stfts_mix), tf.real(stfts_mix)) with tf.variable_scope("bottle_start"): norm_input = self.cLN( tf.concat([encoded_input, tf.log1p(magni_mix)], axis=-1), "layer_norm") block_input = tf.layers.Conv1D( filters=self.config["model"]["filters"]["1*1-conv"], kernel_size=1)(norm_input) for stack_i in range(self.num_stacks): for dilation in self.dilations: with tf.variable_scope("conv_block_{}_{}".format( stack_i, dilation)): block_output = tf.layers.Conv1D( filters=self.config["model"]["filters"]["d-conv"], kernel_size=1)(block_input) block_output = self.prelu(block_output, name='1st-prelu', shared_axes=[1]) block_output = self.gLN(block_output, "first") block_output = self._depthwise_conv1d( block_output, dilation) block_output = self.prelu(block_output, name='2nd-prelu', shared_axes=[1]) block_output = self.gLN(block_output, "second") block_output = tf.layers.Conv1D( filters=self.config["model"]["filters"]["1*1-conv"], kernel_size=1)(block_output) block_input += block_output if self.output_ratio == 1: embed_channel = self.config["model"]["filters"]["ae"] feature_map = encoded_input elif self.output_ratio == 0: embed_channel = self.stft_ch feature_map = magni_mix else: embed_channel = self.concat_channels feature_map = tf.concat([encoded_input, magni_mix], axis=-1) with tf.variable_scope('separator'): s_embed = tf.layers.Dense( embed_channel * self.config["model"]["embed_size"])(block_input) s_embed = tf.reshape(s_embed, [ self.batch_size, -1, embed_channel, self.config["model"]["embed_size"] ]) # Estimate attractor from best combination from anchors v_anchors = tf.get_variable( 'anchors', [self.n_anchor, self.config["model"]["embed_size"]], dtype=tf.float32) c_combs = tf.constant(list( itertools.combinations(range(self.n_anchor), self.n_speaker)), name='combs') s_anchor_sets = tf.gather(v_anchors, c_combs) s_anchor_assignment = tf.einsum('btfe,pce->bptfc', s_embed, s_anchor_sets) s_anchor_assignment = tf.nn.softmax(s_anchor_assignment) s_attractor_sets = tf.einsum('bptfc,btfe->bpce', s_anchor_assignment, s_embed) s_attractor_sets /= tf.expand_dims( tf.reduce_sum(s_anchor_assignment, axis=(2, 3)), -1) sp = tf.matmul(s_attractor_sets, tf.transpose(s_attractor_sets, [0, 1, 3, 2])) diag = tf.fill(sp.shape[:-1], float("-inf")) sp = tf.linalg.set_diag(sp, diag) s_in_set_similarities = tf.reduce_max(sp, axis=(-1, -2)) s_subset_choice = tf.argmin(s_in_set_similarities, axis=1) s_subset_choice_nd = tf.transpose( tf.stack([ tf.range(self.batch_size, dtype=tf.int64), s_subset_choice ])) s_attractors = tf.gather_nd(s_attractor_sets, s_subset_choice_nd) s_logits = tf.einsum('btfe,bce->bctf', s_embed, s_attractors) output_code = s_logits * tf.expand_dims(feature_map, 1) with tf.variable_scope("decoder"): conv_out = pred_istfts = 0 if self.output_ratio != 0: output_frame = tf.layers.Dense( self.config["model"]["kernel_size"]["ae"])(output_code[ ..., :self.config["model"]["filters"]["ae"]]) conv_out = tf.signal.overlap_and_add(signal=output_frame, frame_step=self.fft_hop) if self.output_ratio != 1: phase_mix_expand = tf.expand_dims(phase_mix, 1) pred_stfts = tf.complex( tf.cos(phase_mix_expand) * output_code[..., -self.stft_ch:], tf.sin(phase_mix_expand) * output_code[..., -self.stft_ch:]) pred_istfts = tf.signal.inverse_stft( pred_stfts, frame_length=self.fft_len, frame_step=self.fft_hop, fft_length=self.fft_len, window_fn=tf.signal.inverse_stft_window_fn( self.fft_hop, forward_window_fn=self.fft_wnd)) self.data_out = conv_out * self.output_ratio + pred_istfts * ( 1 - self.output_ratio) self.loss, self.pred_output, self.sdr, self.perm_idxs = loss.pit_loss( self.audios, self.data_out, self.config, self.batch_size, self.n_speaker, self.n_output) ### fixed loss not implemented yet !!!!!! ### self.loss_fix, self.pred_output_fix, self.sdr_fix, self.perm_idxs_fix = loss.pit_loss( self.audios, self.data_out, self.config, self.batch_size, self.n_speaker, self.n_output)
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)))