训练不同形状时的Tensorflow concat

时间:2020-03-20 07:14:29

标签: python tensorflow keras

伙计们,我将在训练时如何将张量对象与自己的定义张量对象连接起来,我正在训练自动编码器模型。

这是我的代码:

def IFFT(sig, name=None):

    nol = tf.zeros([1,12],tf.complex64)
    pv = tf.fill([1,8],3+3j)
    pv = tf.cast(pv,dtype=tf.complex64)
    nol = tf.expand_dims(pv,axis=1)
    pv = tf.expand_dims(pv,axis=1)
    return Lambda(lambda x : tf.ifft(tf.concat([nol,x,pv,nol],axis=-1)), name=name, output_shape=(1,64))(sig)

我运行该代码时出现错误

InvalidArgumentError: ConcatOp : Dimensions of inputs should match: shape[0] = [1,1,12] vs. shape[1] = [64,1,32]
     [[{{node 32-IFFT/concat}}]]
     [[{{node loss/add}}]]

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

处理批处理的一种方法是创建一个名为 BATCH_SIZE 的变量,然后手动输入所需的数据批大小。通常,如果您正在执行自定义操作,而批量大小可能不适合您,或者您需要对数据进行更多控制,则通常会这样做。

我复制了您的代码,并假定了您的X变量的形状 (1、32),因为它未包含在您的代码段中。

import tensorflow as tf
print('TensorFlow Version : {}'.format(tf.__version__)) # Using TensorFlow 2.1.0

BATCH_SIZE = 64

nol = tf.zeros([BATCH_SIZE,1,12],tf.complex64)
pv = tf.fill([BATCH_SIZE,1,8],3+3j)
pv = tf.cast(pv,dtype=tf.complex64)

#Assuming your data X based on your error
x = tf.zeros([BATCH_SIZE,1,32],tf.complex64)

# Verification 1
tf.signal.ifft(tf.concat([nol,x,pv,nol],axis=-1)) 

# Verification 2
tf.keras.layers.Lambda(lambda x : tf.signal.ifft(tf.concat([nol,x,pv,nol],axis=-1)), output_shape=(1,64))(x) 

在处理批处理数据时,这是一种常见的方法,因为它更容易被引用,并且在整个代码中更一致


修改后的代码:

BATCH_SIZE = 64

def IFFT(sig, name=None):

    nol = tf.zeros([BATCH_SIZE,1,12],tf.complex64)
    pv = tf.fill([BATCH_SIZE,1,8],3+3j)
    pv = tf.cast(pv,dtype=tf.complex64)

    return Lambda(lambda x : tf.ifft(tf.concat([nol,x,pv,nol],axis=-1)), name=name, output_shape=(1,64))(sig)
相关问题