python - Tensorflow - 从 Complex64 转换为 2x float32

标签 python tensorflow complex-numbers

我正在尝试使用 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/

相关文章:

python - TensorFlow 恢复抛出 "No Variable to save"错误

python - 如何抑制 Tensorflow (Python) 中的特定警告

c - 如何计算复数的模数

python - 如何从 Python 脚本管理多个 IPython/Jupyter 内核并与之通信?

python - 如何使用更少的内存完成文本分类任务

python - 如何将整个 css 添加到 django 表单中?

python - 无法在 Ubuntu 17.10 上安装 tensorflow

r - 在 R 中绘制史密斯圆图

Python:如何使用 xlwt 编写复数到 Excel?

python - AWS CDK : Error when deploying Redis ElastiCache: Subnet group belongs to a different VPC than CacheCluster