ValueError: cannot reshape array of size 235000 into shape (100,64,64,2350)

ValueError: cannot reshape array of size 235000 into shape (100,64,64,2350)

我正在尝试实现 cDCGAN。我的数据集有 2350 num_classes,batch_size 是 100,图像大小是 64 (rows=64, cols=64, channels=1),z_shape 是 100 我的值占位符如下.

    self.phX = tf.placeholder(tf.float32, [None, self.rows, self.cols, self.channels])
    self.phZ = tf.placeholder(tf.float32, [None, self.z_shape])
    self.phY_g = tf.placeholder(tf.float32, [None, self.num_classes])
    self.phY_d = tf.placeholder(tf.float32, shape=(None, self.rows, self.cols, self.num_classes))

我正在为训练循环中的 phY_g 和 phY_d 加载一批图像,noise_Z 和标签(一个热编码),如下所示。

# Get a random batch of images and labels. This gives 100 images of shape [100,4096] and 100 labels of shape [100,2350]
train_images, train_labels = self.sess.run([self.image_batch, self.label_batch])

# Real image input for Real Discriminator,
# Reshape images to pass to D
batch_X = train_images.reshape((self.batch_size, self.rows, self.cols, self.channels))
batch_X = batch_X * 2 - 1

# Z noise for Generator
batch_Z = np.random.uniform(-1, 1, (self.batch_size, self.z_shape)) # Shape is [?, 100]

# Label input for Generator
batch_Y_g = train_labels
batch_Y_g = batch_Y_g.reshape([self.batch_size, self.num_classes])

# Label input for Discriminator
batch_Y_d = train_labels
batch_Y_d = batch_Y_d.reshape([self.batch_size, self.rows, self.cols, self.num_classes])

一切正常,但 batch_Y_d 出现错误 "ValueError: cannot reshape array of size 235000 into shape (100,64,64,2350)"

如何根据我的占位符形状重塑它?

你不应该改变 self.phY_d 并且你需要改变 batch_Y_d 如下在 cDCGAN 中。

batch_Y_d = train_labels
batch_Y_d = batch_Y_d.reshape([self.batch_size,1,1,self.num_classes])
batch_Y_d = batch_Y_d * np.ones([batch_size, self.rows, self.cols, self.num_classes])
print(batch_Y_d.shape)

(100, 64, 64, 2350)