Tensorflow - 从 complex64 转换为 2x float32

Tensorflow - Casting from complex64 to 2x float32

我正在尝试使用 Tensorflow LSTM RNN 进行一些音频处理。我使用 tf.contrib.signal.stft 希望神经网络更容易理解我的数据,但它 returns 是一个 complex64 类型的张量。如果我尝试将其输入 dynamic_rnn,我会收到以下错误:

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

所以我需要为 RNN 提供 float32 值。我可以将张量转换为 float32,但我认为虚部被丢弃了,我认为它可能很重要。我想改为将每个 complex64 转换为 2 个 float32 值,一个包含实数值,一个包含虚数值。

我的张量具有以下形状:[batch_size、chunks、channels、samples、bins] 和 dtype of complex64。

我想将其转换为形状 [batch_size、块、通道、样本、容器、2] 和 float32 数据类型。

我尝试了以下代码:

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)

但是运行起来真的很慢。

我相信有更好的方法来做到这一点。

怎么样

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

这首先添加新轴,然后我们分别提取 real/imaginary 部分。