【问题标题】:Logits and labels have different first dimensionsLogits 和标签具有不同的第一维
【发布时间】:2019-07-22 13:14:41
【问题描述】:

这是我得到的错误:InvalidArgumentError(参见上面的回溯):logits 和 labels 必须具有相同的第一维,得到 logits 形状 [30,5] 和标签形状 [50]

我使用的批量大小为 50。我的分类问题的输出数量为 5。

我不知道 logits 形状中的 30 是从哪里来的。这是我的架构:

with tf.name_scope("pool3"):

pool3 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID")

pool3_flat = tf.reshape(pool3, shape=[-1, 24000]) # must be a multiple of the input

pool3_flat_drop = tf.layers.dropout(pool3_flat, conv2_dropout_rate, training=training)



with tf.name_scope("fc1"):

flattened = tf.layers.flatten(pool3_flat_drop)

fc1 = tf.layers.dense(flattened , n_fc1, activation=tf.nn.relu, name="fc1")

fc1_drop = tf.layers.dropout(fc1, fc1_dropout_rate, training=training)



with tf.name_scope("output"):

# n_outputs = number of possible classes

logits = tf.layers.dense(fc1_drop, n_outputs, name="output")

Y_proba = tf.nn.softmax(logits, name="Y_proba")



with tf.name_scope("train"):

xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y)

这也是我声明占位符的方式

with tf.name_scope('inputs'):

X = tf.placeholder(tf.float32, shape=[None, n_inputs], name='X')

X_reshaped = tf.reshape(X, shape=[-1, height, width, channels]) # make applicable to convolutional

y = tf.placeholder(tf.int32, shape=[None], name='y')

training = tf.placeholder_with_default(False, shape=[], name='training')

【问题讨论】:

    标签: python tensorflow machine-learning


    【解决方案1】:

    我认为是pool3_flat = tf.reshape(pool3, shape=[-1, 24000])引起的

    你应该检查24000是否正确。

    【讨论】:

    • 所以我应该用池化层替换 24000 的输出数量来展平它?
    • 如果你想保留原来的拱门。你发布,你应该用大小[批量大小,-1]使它变平。其实很奇怪,你可以简单地使用 tf.layers.flatten,它会保持批量大小不变。
    猜你喜欢
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 2021-07-19
    • 2021-07-03
    • 1970-01-01
    • 2022-07-06
    相关资源
    最近更新 更多