logit和标签必须具有相同的第一维CNN

时间:2018-04-12 08:41:49

标签: python tensorflow

我正在训练一个简单的CNN模型,这里是结构

图片的大小为64X64

第一层:卷积5X5X8平均合并5X5步幅2

第二层:卷积5X5X8平均合并5X5步幅2

第3层:卷积1X1X32平均汇总全球合并

第4层:完全连接的层32输入2输出

当我使用sparse_softmax_cross_entropy计算损失时,会引发如下错误:

enter image description here

我认为第四层的Tensor输出的形状应该是(?,2)而且它是,但我不明白为什么logits的形状是[ 1280 ,2]

def stg_model_fn(features, labels, mode):
    # Input Layer
    x = tf.reshape(features, [-1, 64, 64, 1])
    # print(x)
    x = layer_module.conv_group(
        inputs = x,
        activation = "tanh",
        filters = 8,
        kernel_size = [5, 5],
        pool_size = 5,
        strides = 2,
        abs_layer = True,
        pool_padding = "same")
    print(x)

    x = layer_module.conv_group(
        inputs = x,
        filters = 16,
        activation = "tanh",
        kernel_size = [5, 5],
        pool_size = 5,
        strides = 2,
        abs_layer = False,
        pool_padding = "same")
    print(x)

    x = layer_module.conv_group(
        inputs = x,
        filters = 32,
        activation = "relu",
        kernel_size = [1, 1],
        pool_size = 16,
        strides = 1,
        abs_layer = False,
        pool_padding = "valid")
    print(x)


    x = tf.reshape(x, [-1, 32])
    x = tf.layers.dense(inputs = x, units = 2)




    predictions = {
        # Generate predictions (for PREDICT and EVAL mode)
        "classes": tf.argmax(input=x, axis=1),
        # Add `softmax_tensor` to the graph. It is used for PREDICT and by the
        # `logging_hook`.
        "probabilities": tf.nn.softmax(x, 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.nn.sparse_softmax_cross_entropy_with_logits(labels = labels, logits = x)



if mode == tf.estimator.ModeKeys.TRAIN:
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=FLAGS.learning_rate)
    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)

1 个答案:

答案 0 :(得分:0)

我怀疑你搞砸了维度的计算。 在输出之前,您执行x = tf.reshape(x, [-1, 32])。此时,您假设x的形状为(?, 1, 1, 32)。由于你没有得到(批量大小,32),我预计情况并非如此。

仔细检查重塑前的x是否具有正确的形状并进行相应调整。