Tensorflow报错Assign requires shapes of both tensors to match. lhs shape= [xxx] rhs shape= [xxx]的解决方法

开门见山,遇到这个问题,第一,确定修改后的网络结构和你输入的自定义数据是对应的,第二,删掉代码目录下logs(一般叫logs,也可能叫别的)文件夹,因为这里面记录了用别的数据集训练时候的网络结构,会对你的训练造成干扰。

···


具体问题:

最近在跑GAN程序的时候遇到的这种错误,注意,这个错误一般是你因为你修改了网络参数所导致的,例如修改卷积核尺寸,等等。
代码来自 https://github.com/hwalsuklee/tensorflow-generative-model-collections

这是一个GAN的tensorflow实现的集合,在其中的代码io部分核部分核心网络结构基本上是为MNIST数据集定制的,如下

            #第一部分
            parameters
            self.input_height = 28
            self.input_width = 28
            self.output_height = 28
            self.output_width = 28

            # 第二部分
                data = extract_data(data_dir + '/train-images-idx3-ubyte.gz', 60000, 16, 28 * 28)
                trX = data.reshape((60000, 28, 28, 1))

                data = extract_data(data_dir + '/train-labels-idx1-ubyte.gz', 60000, 8, 1)
                trY = data.reshape((60000))

                data = extract_data(data_dir + '/t10k-images-idx3-ubyte.gz', 10000, 16, 28 * 28)
                teX = data.reshape((10000, 28, 28, 1))

                data = extract_data(data_dir + '/t10k-labels-idx1-ubyte.gz', 10000, 8, 1)
                teY = data.reshape((10000))
                # 第三部分, 判别器网络


                    def generator(self, z, is_training=True, reuse=False):
                    #     # Network Architecture is exactly same as in infoGAN (https://arxiv.org/abs/1606.03657)
                    #     # Architecture : FC1024_BR-FC7x7x128_BR-(64)4dc2s_BR-(1)4dc2s_S
                    #     with tf.variable_scope("generator", reuse=reuse):
                            net = tf.nn.relu(bn(linear(z, 1024, scope='g_fc1'), is_training=is_training, scope='g_bn1'))
                            net = tf.nn.relu(bn(linear(net, 128 * 7 * 7, scope='g_fc2'), is_training=is_training, scope='g_bn2'))
                            net = tf.reshape(net, [self.batch_size, 7, 7, 128])
                            net = tf.nn.relu(
                                bn(deconv2d(net, [self.batch_size, 14, 14, 64], 4, 4, 2, 2, name='g_dc3'), is_training=is_training,
                                   scope='g_bn3'))

                            out = tf.nn.sigmoid(deconv2d(net, [self.batch_size, 28, 28, 1], 4, 4, 2, 2, name='g_dc4'))

                            return out

前两部分还好,更具特定数据集改就行了,可是第三个,特别市后面两个缩小1/2和1/4的反卷积不好看出来,而且改了之后还会报错Assign requires shapes of both tensors to match.

对于前面两个问题,建议新建一个类,存储当前输入数据训练集和测试机图片的长宽像素和个数。

对于第三个问题, 删掉目录中的logs文件夹,因为这里面记录了用别的数据集训练时候的网络结构,会对你的训练造成干扰。

猜你喜欢

转载自blog.csdn.net/github_34777264/article/details/80855705