简体   繁体   English

如何将 dtype= complex64 的 3D 张量转换为 tensorflow 中 dtype = float32 的 3D 张量

[英]How to convert a 3D tensor of dtype= complex64 to 3D tensor of dtype = float32 in tensorflow

I have tensor of dimension (B,N,K) with complex values and I want to convert the tensor into dimension(B,N,2*K) in such a way that every real value is placed next to its complex value, something like this:我有具有复数值的维度张量(B,N,K),我想将张量转换为维度(B,N,2 * K),这样每个实数值都放在其复数值旁边,有些东西像这样:

[[[ 2.51-0.49j  0.80-0.74j]
  [-0.01+0.34j -2.04+0.70j]
  [ 0.02-1.85j -0.38+1.66j]]

 [[ 0.54+0.49j  0.28+1.75j]
  [-1.52-1.72j  0.68+0.17j]
  [-0.89+0.32j -1.88+0.15j]]] (2, 3, 2)

This complex tensor gets converted to:这个复数张量被转换为:

[[[ 2.51  -0.49  0.80  -0.74]
  [-0.01  0.34   -2.04  0.70]
  [ 0.02 -1.85   -0.38  1.66]]

 [[ 0.54  0.49   0.28  1.75]
  [-1.52  1.72   0.68  0.17]
  [-0.89  0.32  -1.88  0.15]]] (2, 3, 4)

I have reduced the number of decimal places for readability.为了便于阅读,我减少了小数位数。

Something like this should work fine:这样的事情应该可以正常工作:

inp = tf.convert_to_tensor([[[ 2.51-0.49j , 0.80-0.74j],
                   [-0.01+0.34j, -2.04+0.70j],
                   [ 0.02-1.85j, -0.38+1.66j]],

                  [[ 0.54+0.49j,  0.28+1.75j],
                   [-1.52-1.72j,  0.68+0.17j],
                   [-0.89+0.32j, -1.88+0.15j]]])
initial_shape = tf.shape(inp)

# convert to 1 number per "last dimension"
tmp = tf.reshape(inp, (initial_shape[0], -1, 1)) 

# get real part
real = tf.math.real(tmp) 
# get imaginary part
imag = tf.math.imag(tmp) 
# concatenate real with its corresponding imaginary 
to_reshape = tf.concat((real, imag), axis=-1) 

# reshape to initial shape, with the last *2
tf.reshape(to_reshape, (initial_shape[0], initial_shape[1], initial_shape[2]*2)) 

Output: Output:

<tf.Tensor: shape=(2, 3, 4), dtype=float64, numpy=
array([[[ 2.51, -0.49,  0.8 , -0.74],
        [-0.01,  0.34, -2.04,  0.7 ],
        [ 0.02, -1.85, -0.38,  1.66]],

       [[ 0.54,  0.49,  0.28,  1.75],
        [-1.52, -1.72,  0.68,  0.17],
        [-0.89,  0.32, -1.88,  0.15]]])>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TF 2.0:如何将 Tensor(&quot;mul_1:0&quot;, shape=(1000, 64), dtype=float32) 转换为 Numpy 数组 - TF 2.0: How to convert Tensor("mul_1:0", shape=(1000, 64), dtype=float32) to Numpy array Tensor转换请求dtype float32的dtype float64 - Tensor conversion requested dtype float64 for Tensor with dtype float32 Tensorflow 图像生成器通过 dtype=string 的张量而不是 dtype=float32 的张量来丢失 function - Tensorflow Image Generator passing Tensor with dtype=string instead of Tensor with dtype=float32 to loss function Tensorflow 2.0 警告-dense_features 正在将输入张量从 dtype float64 转换为 float32 层的 dtype - Tensorflow 2.0 Warnings - dense_features is casting an input tensor from dtype float64 to the layer's dtype of float32 图断开:无法获得张量 Tensor(&quot;conv2d_1_input:0&quot;, shape=(?, 128, 128, 1), dtype=float32) 的值 - Graph disconnected: cannot obtain value for tensor Tensor("conv2d_1_input:0", shape=(?, 128, 128, 1), dtype=float32) ValueError: Tensor 转换请求 dtype int32 for Tensor with dtype float32 - LSTM Implementation(tensorflow 2.0.0) - ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32 - LSTM Implementation( tensorflow 2.0.0) 警告:tensorflow:Layer my_model 正在将输入张量从 dtype float64 转换为 float32 层的 dtype,这是 TensorFlow 2 中的新行为 - WARNING:tensorflow:Layer my_model is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2 ValueError:张量转换为具有 dtype float32 的张量请求 dtype float32_ref - ValueError: Tensor conversion requested dtype float32_ref for Tensor with dtype float32 添加的层必须是 class 层的实例。 找到:张量(“conv1d_12/Relu:0”, shape=(?, 41, 64), dtype=float32) - The added layer must be an instance of class Layer. Found: Tensor(“conv1d_12/Relu:0”, shape=(?, 41, 64), dtype=float32) 使用dtype float32(lambda输入)的Tensor的Tensor转换请求dtype字符串 - Tensor conversion requested dtype string for Tensor with dtype float32 (lambda input)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM