我正在尝试使用 Tensorflow LSTM RNN 进行一些音频处理。我使用 tf.contrib.signal.stft 希望使神经网络更容易理解我的数据,但它返回一个复杂64类型的张量。如果我尝试将其输入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]和complex64的dtype。
我想将其转换为具有形状 [batch_size, chunks, Channels, Samples, bins, 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)
这首先添加新轴,然后分别提取实部/虚部。
关于python - Tensorflow - 从 Complex64 转换为 2x float32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48842263/