gpt4 book ai didi

python - TensorFlow 索引无效(越界)

转载 作者:太空宇宙 更新时间:2023-11-03 17:12:43 25 4
gpt4 key购买 nike

您好,我目前正在尝试使用自己的图像数据运行 TensorFlow。但是当我尝试运行这些函数时它崩溃了:它来自 mnist.py

def loss_fn(logits, labels):
batch_size = tf.size(labels)
labels = tf.expand_dims(labels, 1)
indices = tf.expand_dims(tf.range(0, batch_size, 1), 1)
concated = tf.concat(1, [indices, labels])
onehot_labels = tf.sparse_to_dense(
concated, tf.pack([batch_size, NUM_CLASSES]), 1.0, 0.0)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits,
onehot_labels,name='xentropy')
loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')
return loss

出现此错误:

Compute status: Invalid argument: Indices are not valid (out of bounds).  Shape: dim { size: 100 } dim { size: 447 }

数字 100 是我的批量大小,447 是我的类数。

我也尝试像这里一样解决这个问题 https://github.com/tensorflow/tensorflow/issues/194将 indeces 行更改为以下行:

indices = tf.expand_dims(tf.range(0, batch_size, 1), 1)

没有解决我的问题。有人有想法吗?

最佳答案

我也遇到了这个错误。我意识到了我的错误。如果您有 10 个类别,则标签值的范围应在 0 到 9 之间(含 0 和 9)。该错误在与 SVHN 一起使用的 TensorFlow CIFAR10 示例上重现。数据集。请参阅下面的问题和解答。

TensorFlow CIFAR10 Example

关于python - TensorFlow 索引无效(越界),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33933846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com