我们可以在keras中使用tf.spectral傅立叶函数吗? [英] Can we use tf.spectral fourier functions in keras?

查看:431
本文介绍了我们可以在keras中使用tf.spectral傅立叶函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们从一个简单的时间序列输入开始,并尝试构建一个自动编码器,该编码器只需进行傅立叶变换,然后在keras中对数据进行非变换.

Let us start with an input that is a simple time series and try to build an autoencoder that simply fourier transforms then untransforms our data in keras.

如果我们尝试这样做:

inputs = Input(shape=(MAXLEN,1), name='main_input')
x = tf.spectral.rfft(inputs)
decoded = Lambda(tf.spectral.irfft)(x)

然后,第三行在输入时引发错误:

Then the third line throws an error when entered:

>> ValueError: Tensor conversion requested dtype complex64 for Tensor with dtype float32

您看到的tf.spectral.irfft的输出是float32,但看起来Lambda认为它是complex64? (Complex64是上一步的输入x)

You see, the output of tf.spectral.irfft is float32 but it looks like Lambda thinks it is complex64?? (Complex64 is the input x from the previous step)

我们可以在模型输入时使用以下方法修复该错误:

We can fix that error at model entry time with:

inputs = Input(shape=(MAXLEN,1), name='main_input')
x = tf.spectral.rfft(inputs)
decoded = Lambda(tf.cast(tf.spectral.irfft(x),dtype=tf.float32)))

这是在输入时接受的,但是当我们尝试构建模型时:

This is accepted at input time but then when we try to build the model:

autoencoder = Model(inputs, decoded)

它生成错误:

TypeError: Output tensors to a Model must be Keras tensors. Found: <keras.layers.core.Lambda object at 0x7f24f0f7bbe0>

我认为这是合理的,也是我不想首先将其投放的原因.

Which I guess is reasonable and was the reason I didn't want to cast it in the first place.

主要问题:如何成功包装输出float32的tf.spectral.irfft函数?

Main question: how do I successfully wrap the tf.spectral.irfft function which outputs float32 ?

更一般的学习问题: 假设我实际上想在rfft和irfft之间做些什么,如何将这些虚数转换为绝对值而不破坏keras,以便我可以应用各种卷积等?

More general question for learning: Let's assume I actually want to do something between the rfft and the irfft, how can I cast those imaginary numbers into absolute values without breaking keras so I can apply various convolutions and the like?

推荐答案

我认为您只需要进行更多Lambda包装(使用tf.keras即可,因为这就是我已经安装的包装):

I think you just need more Lambda wrapping (using tf.keras since that's what I have installed):

import numpy
import tensorflow as tf
K = tf.keras

inputs = K.Input(shape=(10, 8), name='main_input')
x = K.layers.Lambda(tf.spectral.rfft)(inputs)
decoded = K.layers.Lambda(tf.spectral.irfft)(x)
model = K.Model(inputs, decoded)
output = model(tf.ones([10, 8]))
with tf.Session():
  print(output.eval())

irfft的输出应该是真实的,因此可能不需要强制转换.但是,如果确实需要强制转换(或通常在Lambda层中合并操作),则可以将其包装在Python lambda中:K.layers.Lambda(lambda v: tf.cast(tf.spectral.whatever(v), tf.float32))

The output of irfft should be real, so probably no need to cast it. But if you do need to cast it (or in general combine operations in a Lambda layer), I'd wrap that in a Python lambda: K.layers.Lambda(lambda v: tf.cast(tf.spectral.whatever(v), tf.float32))

例如,如果您知道中间值(在rfftirfft之间)的虚部为零,则可以将其截断:

For example if you know your intermediate values (between rfft and irfft) will have an imaginary component of zero, you can truncate that off:

import numpy
import tensorflow as tf
K = tf.keras

inputs = K.Input(shape=(10, 8), name='main_input')
x = K.layers.Lambda(lambda v: tf.real(tf.spectral.rfft(v)))(inputs)
decoded = K.layers.Lambda(
    lambda v: tf.spectral.irfft(tf.complex(real=v, imag=tf.zeros_like(v))))(x)
model = K.Model(inputs, decoded)
output = model(tf.reshape(tf.range(80, dtype=tf.float32), [10, 8]))
with tf.Session():
  print(output.eval())

请注意,对于一般序列而言并非如此,因为一旦转换,即使是实值输入也可能具有虚部.它适用于上面的tf.ones输入,但是tf.range输入被弄乱了:

Note that this isn't true for general sequences, since even real-valued inputs can have imaginary components once transformed. It works for the tf.ones input above, but the tf.range input gets mangled:

[[ 0.  4.  4.  4.  4.  4.  4.  4.]
 [ 8. 12. 12. 12. 12. 12. 12. 12.]
 [16. 20. 20. 20. 20. 20. 20. 20.]
 [24. 28. 28. 28. 28. 28. 28. 28.]
 [32. 36. 36. 36. 36. 36. 36. 36.]
 [40. 44. 44. 44. 44. 44. 44. 44.]
 [48. 52. 52. 52. 52. 52. 52. 52.]
 [56. 60. 60. 60. 60. 60. 60. 60.]
 [64. 68. 68. 68. 68. 68. 68. 68.]
 [72. 76. 76. 76. 76. 76. 76. 76.]]

(不进行强制转换,则得到0.到79.完美重建)

(Without the casting we get 0. through 79. reconstructed perfectly)

这篇关于我们可以在keras中使用tf.spectral傅立叶函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆