首页 文章

Tensorflow - 从complex64转换为2x float32

提问于
浏览
2

我正在尝试使用Tensorflow LSTM RNN进行一些音频处理 . 我正在使用tf.contrib.signal.stft来帮助神经网络更容易理解我的数据,但它返回了类型为complex64的Tensor . 如果我尝试将其提供给dynamic_rnn,我会收到以下错误:

ValueError:需要变量rnn / basic_lstm_cell / kernel的初始值设定项

所以我需要为RNN提供float32值 . 我可以将张量转换为float32,但是我认为假想的组件被丢弃了,我认为它可能很重要 . 我想将每个complex64转换为2个float32值,一个包含实数值,另一个包含虚数值 .

我的张量具有以下形状:[batch_size,chunk,channels,samples,bin]和complex64的dtype .

我想将它转换为具有形状[batch_size,chunk,channel,samples,bin,2]和float32的dtype .

我尝试了以下代码:

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)

但它运行得很慢 .

我确信有更好的方法可以做到这一点 .

1 回答

  • 5

    怎么样

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

    首先添加新轴,然后分别提取实部/虚部 .

相关问题