Yoshi Walsh
Yoshi Walsh

Reputation: 2077

Tensorflow - Casting from complex64 to 2x float32

I'm trying to do some audio processing using a Tensorflow LSTM RNN. I'm using tf.contrib.signal.stft to hopefully make it easier for the neural-net to understand my data, but it returns a Tensor of type complex64. If I try to feed this into a dynamic_rnn I get the following error:

ValueError: An initializer for variable rnn/basic_lstm_cell/kernel of is required

So I need to provide the RNN with float32 values. I can cast the tensor to float32, but then I think the imaginary component is discarded, and I think it might be significant. I'd like to instead convert each complex64 into 2 float32 values, one containing the real value and one containing the imaginary value.

My tensor has the following shape: [batch_size, chunks, channels, samples, bins] and dtype of complex64.

I'd like to convert it to have shape [batch_size, chunks, channels, samples, bins, 2] and dtype of float32.

I tried the following code:

realFourierTransformed = tf.map_fn(lambda batch: tf.map_fn(lambda chunk: tf.map_fn(lambda channel: tf.map_fn(lambda sample: tf.map_fn(lambda bin: tf.convert_to_tensor([tf.real(bin), tf.imag(bin)]), sample, dtype=tf.float32), channel, dtype=tf.float32), chunk, dtype=tf.float32), batch, dtype=tf.float32), fourierTransformed, dtype=tf.float32)

but it runs really slowly.

I'm sure there's a much better way of doing this.

Upvotes: 2

Views: 1879

Answers (1)

Jonas Adler
Jonas Adler

Reputation: 10759

How about

extended_bin = bin[..., None]
tf.concat([tf.real(extended_bin), tf.imag(extended_bin)], axis=-1)

This first adds the new axis and we then extract the real/imaginary parts respectively.

Upvotes: 5

Related Questions