首页 文章

Tensorflow:logits和标签必须具有相同的第一个维度

提问于
浏览
0

我是tensoflow的新手,我想用我自己的数据(40x40的图像)调整MNIST教程https://www.tensorflow.org/tutorials/layers . 这是我的模特功能:

def cnn_model_fn(features, labels, mode):
        # Input Layer
        input_layer = tf.reshape(features, [-1, 40, 40, 1])

        # Convolutional Layer #1
        conv1 = tf.layers.conv2d(
                inputs=input_layer,
                filters=32,
                kernel_size=[5, 5],
                #  To specify that the output tensor should have the same width and height values as the input tensor
                # value can be "same" ou "valid"
                padding="same",
                activation=tf.nn.relu)

        # Pooling Layer #1
        pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)

        # Convolutional Layer #2 and Pooling Layer #2
        conv2 = tf.layers.conv2d(
                inputs=pool1,
                filters=64,
                kernel_size=[5, 5],
                padding="same",
                activation=tf.nn.relu)
        pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)

        # Dense Layer
        pool2_flat = tf.reshape(pool2, [-1, 10 * 10 * 64])
        dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
        dropout = tf.layers.dropout(
                inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)

        # Logits Layer
        logits = tf.layers.dense(inputs=dropout, units=2)

        predictions = {
            # Generate predictions (for PREDICT and EVAL mode)
            "classes":       tf.argmax(input=logits, axis=1),
            # Add `softmax_tensor` to the graph. It is used for PREDICT and by the
            # `logging_hook`.
            "probabilities": tf.nn.softmax(logits, name="softmax_tensor")
        }

        if mode == tf.estimator.ModeKeys.PREDICT:
            return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

        # Calculate Loss (for both TRAIN and EVAL modes)
        loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)

        # Configure the Training Op (for TRAIN mode)
        if mode == tf.estimator.ModeKeys.TRAIN:
            optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
            train_op = optimizer.minimize(
                    loss=loss,
                    global_step=tf.train.get_global_step())
            return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)

        # Add evaluation metrics (for EVAL mode)
        eval_metric_ops = {
            "accuracy": tf.metrics.accuracy(
                    labels=labels, predictions=predictions["classes"])}
        return tf.estimator.EstimatorSpec(
                mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)

我在标签和logits之间有一个形状大小错误:

InvalidArgumentError (see above for traceback): logits and labels must have the same first dimension, got logits shape [3,2] and labels shape [1]

filenames_array是一个16字符串的数组

["file1.png", "file2.png", "file3.png", ...]

和labels_array是一个16整数的数组

[0,0,1,1,0,1,0,0,0,...]

主要功能是:

# Create the Estimator
mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn, model_dir="/tmp/test_convnet_model")

# Train the model
cust_train_input_fn = lambda: train_input_fn_custom(
        filenames_array=filenames, labels_array=labels, batch_size=1)

mnist_classifier.train(
        input_fn=cust_train_input_fn,
        steps=20000,
        hooks=[logging_hook])

我试图重塑logits但没有成功:

logits = tf.reshape(logits,[1,2])

我需要你的帮助,谢谢


EDIT

经过更长时间的搜索,在我的模型功能的第一行

input_layer = tf.reshape(features, [-1, 40, 40, 1])

"-1"表示将动态计算batch_size维度,此处的值为"3" . 与我的错误相同"3": logits and labels must have the same first dimension, got logits shape [3,2] and labels shape [1]

如果我强制该值为“1”我有这个新错误:

Input to reshape is a tensor with 4800 values, but the requested shape has 1600

也许我的功能有问题?


EDIT2 :

完整的代码在这里:https://gist.github.com/geoffreyp/cc8e97aab1bff4d39e10001118c6322e


EDIT3

我更新了要点

logits = tf.layers.dense(inputs=dropout, units=1)

https://gist.github.com/geoffreyp/cc8e97aab1bff4d39e10001118c6322e

但我不完全理解你对批量大小的答案,这里的批量大小是3,而我选择1的批量大小?

如果我选择batch_size = 3我有这个错误: logits and labels must have the same first dimension, got logits shape [9,1] and labels shape [3]

我试图重塑标签:

labels = tf.reshape(labels, [3, 1])

我更新了功能和标签结构:

filenames_train = [['blackcorner-data/1.png', 'blackcorner-data/2.png', 'blackcorner-data/3.png',
                   'blackcorner-data/4.png', 'blackcorner-data/n1.png'],
                   ['blackcorner-data/n2.png',
                   'blackcorner-data/n3.png', 'blackcorner-data/n4.png',
                   'blackcorner-data/11.png', 'blackcorner-data/21.png'],
                   ['blackcorner-data/31.png',
                   'blackcorner-data/41.png', 'blackcorner-data/n11.png', 'blackcorner-data/n21.png',
                   'blackcorner-data/n31.png']
                   ]

labels = [[0, 0, 0, 0, 1], [1, 1, 1, 0, 0], [0, 0, 1, 1, 1]]

但没有成功......

2 回答

  • 0

    我有一个类似的问题,结果是一个池图层没有正确重新整形 . 我错误地使用了我的情况 tf.reshape(pool, shape=[-1, 64 * 7 * 7]) 而不是 tf.reshape(pool, shape=[-1, 64 * 14 * 14]) ,这导致了关于logits和标签的类似错误按摩 . 改变因素,例如 tf.reshape(pool, shape=[-1, 64 * 12 * 12]) 导致完全不同,误导性较差的错误消息 .

    也许这也是这种情况 . 我建议通过代码检查节点的形状,以防万一 .

  • 1

    您的logits形状看起来正确,批量大小为3,输出层大小为2,这是您定义的输出层 . 你的标签也应该是形状[3,2] . 批次3,每批有2 [1,0]或[0,1] .

    还要注意,当你有一个布尔分类输出时,输出/ logits层上不应该有2个神经元 . 您只需输出一个取值为0或1的值,您可以看到[1,0]和[0,1]的2个输出是多余的,可以表示为简单的[0 | 1]值 . 当你这样做时,你往往会得到更好的结果 .

    因此,您的logits应该最终为[3,1],并且您的标签应该是一个包含3个值的数组,每个值对应一批中的每个样本 .

相关问题