在张量流中为浮点变量计算 fft 时出错

error in computing fft in tensorflow for float variables

我已将 x 声明为浮点变量,但在计算变量的 fft 时出现错误。我的输入不是复数,它将是正浮点值和负浮点值。那么我应该将变量声明为什么呢? 我的代码

x=tf.Variable([1.0,2.3,3.99,4,5.1],dtype=tf.int32)
model = tf.initialize_all_variables()
sess= tf.Session()
sess.run(model)
print(sess.run(tf.fft(x))

错误

TypeError: Input 'input' of 'FFT' Op has float32 that doesnot match expected type of complex64

根据给出的评论更新代码

我修改了变量

a = tf.Variable([1.3, -2, 3.6, 4.1, 5.9],dtype=tf.float32)
b = tf.Variable([0, 0, 0, 0, 0], dtype=tf.float32)
c = tf.complex(a, b)
d = tf.fft(c)
model = tf.initialize_all_variables()
sess=tf.Session()
sess.run(model)
print(sess.run(d))

我收到另一个错误

InvalidArgumentError (see above for traceback): No OpKernel was registered to support Op 'FFT' with these attrs.  Registered kernels:<no registered kernels> [[Node: FFT = FFT[](Complex)]]

我可以轻松地用scipy

表演
from scipy.fftpack import fft as scifft
b = [1.3, -2, 3.6, 4.1, 5.9]
print(scifft(b))  

并得到结果

[ 12.90000000+0.j   -3.72426458+7.8072391j   0.52426458+4.16797523j   0.52426458-4.16797523j   -3.72426458-7.8072391j  ]

如何使用张量流计算得到相同的值?

您使用的是哪个版本的tensorflow?你需要安装用于计算 FFT 的 GPU 版本。 我有类似的问题,首先我很难理解这一点。 您可以使用 tf.cast 来更改变量类型 示例:tf.cast(tf.Varaible(x,dtype=tf.int32),dtype=tf.complex64)