Python Tensorflow错误:ValueError:形状必须具有相等的秩,但如果将形状1与其他形状合并,则为2和1

Python Tensorflow错误:ValueError:形状必须具有相等的秩,但如果将形状1与其他形状合并,则为2和1,python,tensorflow,artificial-intelligence,Python,Tensorflow,Artificial Intelligence,我试图使用tensorflow实现dcgan,但遇到以下错误: ValueError: Shapes must be equal rank, but are 2 and 1 From merging shape 1 with other shapes. for 'generator/Reshape/packed' (op: 'Pack') with input shapes: [?,2048], [100,2048], [2048]. 据我所知,这表明我的张量形状不同,但我看不出我需要改变什么

我试图使用tensorflow实现dcgan,但遇到以下错误:

ValueError: Shapes must be equal rank, but are 2 and 1
From merging shape 1 with other shapes. for 'generator/Reshape/packed' (op: 'Pack') with input shapes: [?,2048], [100,2048], [2048].
据我所知,这表明我的张量形状不同,但我看不出我需要改变什么来修正这个错误。我相信错误就在这两种方法之间:

首先,我使用以下方法在方法中创建占位符:

self.z = tf.placeholder(tf.float32, [None,self.z_dimension], name='z')
self.z_sum = tf.histogram_summary("z", self.z)

self.G = self.generator(self.z)
最后一条语句调用generator方法,此方法使用Reformate通过以下方式更改张量:

 self.z_ = linear(z,self.gen_dimension * 8 * sample_H16 * sample_W16, 'gen_h0_lin', with_w=True)

 self.h0 = tf.reshape(self.z_,[-1, sample_H16, sample_W16,self.gen_dimension * 8])

 h0 = tf.nn.relu(self.gen_batchnorm1(self.h0))
如果有帮助,这里是我的线性方法:

def linear(input_, output_size, scope=None, stddev=0.02, bias_start=0.0, with_w=False):
shape = input_.get_shape().as_list()

with tf.variable_scope(scope or "Linear"):
  matrix = tf.get_variable("Matrix", [shape[1], output_size], tf.float32,tf.random_normal_initializer(stddev=stddev))
  bias = tf.get_variable("bias", [output_size],initializer=tf.constant_initializer(bias_start))
  if with_w:
    return tf.matmul(input_, matrix) + bias, matrix, bias
  else:
    return tf.matmul(input_, matrix) + bias
编辑:

我还使用以下占位符:

    self.inputs = tf.placeholder(tf.float32, shape=[self.batch_size] + image_dimension, name='real_images')
    self.gen_inputs = tf.placeholder(tf.float32, shape=[self.sample_size] + image_dimension, name='sample_inputs')
    inputs = self.inputs
    sample_inputs = self.gen_inputs
线性(z,self.gen\u维度*8*样本\u H16*样本\u W16,'gen\u h0\u lin',且\u w=True)
将返回元组
(tf.matmul(输入,矩阵)+偏差,矩阵,偏差)

因此,
self.z
由元组分配,而不是唯一的tf张量

只需将
线性(z,self.gen_维度*8*样本*H16*样本*W16,'gen_h0_lin',w=True)
更改为
线性(z,self.gen_维度*8*样本*W16,'gen_h0_lin',w=False)