使用 None 为批量大小重塑张量

mna*_*nal 0 python tensorflow tensor

基本上这个问题TensorFlow: Is there a way to convert a list with None type to a Tensor?

答案说明了为什么这是不可能的,但没有解决方法

我正在尝试制作一个自动编码器,它具有一些卷积层,这些层被展平为一些完全连接的层并重新扩展到原始维度。但是,当我尝试将展平层的输出扩展为张量时,我遇到了问题Tried to convert 'shape' to a tensor and failed. Error: Cannot convert a partially known TensorShape to a Tensor: (?, 14, 32, 128)

这基本上就是网络的样子

X=tf.placeholder(tf.float32,shape=[None, height, width, channel])
conv = Conv2D(32, (3, 3), activation='relu', padding='same')(X)
h1=actf(tf.matmul(conv,w)+b) 
output = Conv2D(32, (3, 3), activation='relu', padding='same')(tf.reshape(h1, conv.shape))
Run Code Online (Sandbox Code Playgroud)

如何在不指定批量大小的情况下重塑中间层的输出?

我也试过

output = Conv2D(32, (3, 3), activation='relu', padding='same')(tf.reshape(h1, [None, 14, 32, 128]))
Run Code Online (Sandbox Code Playgroud)

这给出了以下错误 Failed to convert object of type <class 'list'> to Tensor. Contents: [None, 14, 32, 128]

小智 5

您应该使用-1代替None来指定应该自动计算的维度。试试tf.reshape(h1, [-1] + conv.shape.as_list()[1:])