Python ValueError:无法挤压尺寸[1],预期尺寸为1,对于'得到3;稀疏最大交叉熵损失

Python ValueError:无法挤压尺寸[1],预期尺寸为1,对于'得到3;稀疏最大交叉熵损失,python,tensorflow,Python,Tensorflow,我试图用本地图像替换培训和验证数据。但在运行培训代码时,出现了错误: ValueError:无法压缩尺寸[1],预期尺寸为1,输入形状为[100,3]的“稀疏”\u softmax\u交叉”\u熵\u损失/移除\u可压缩的\u尺寸/压缩”(op:“压缩”)得到3 我不知道如何修理它。模型定义代码中没有可见变量。代码是根据TensorFlow教程修改的。这些图像是JPG 以下是详细的错误消息: INFO:tensorflow:Using default config. INFO:tensorflo

我试图用本地图像替换培训和验证数据。但在运行培训代码时,出现了错误:

ValueError:无法压缩尺寸[1],预期尺寸为1,输入形状为[100,3]的“稀疏”\u softmax\u交叉”\u熵\u损失/移除\u可压缩的\u尺寸/压缩”(op:“压缩”)得到3

我不知道如何修理它。模型定义代码中没有可见变量。代码是根据TensorFlow教程修改的。这些图像是JPG

以下是详细的错误消息:

INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_log_step_count_steps': 100, '_is_chief': True, '_model_dir': '/tmp/mnist_convnet_model', '_tf_random_seed': None, '_session_config': None, '_save_checkpoints_secs': 600, '_num_worker_replicas': 1, '_save_checkpoints_steps': None, '_service': None, '_keep_checkpoint_max': 5, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x00000288088D50F0>, '_keep_checkpoint_every_n_hours': 10000, '_task_type': 'worker', '_master': '', '_save_summary_steps': 100, '_num_ps_replicas': 0, '_task_id': 0}
Traceback (most recent call last):
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 686, in _call_cpp_shape_fn_impl
    input_tensors_as_shapes, status)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Can not squeeze dim[1], expected a dimension of 1, got 3 for 'sparse_softmax_cross_entropy_loss/remove_squeezable_dimensions/Squeeze' (op: 'Squeeze') with input shapes: [100,3].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\tf_exe_5_make_image_lables\cnn_mnist.py", line 214, in <module>
    tf.app.run()
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\platform\app.py", line 124, in run
    _sys.exit(main(argv))
  File "D:\tf_exe_5_make_image_lables\cnn_mnist.py", line 203, in main
    hooks=[logging_hook])
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\estimator\estimator.py", line 314, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\estimator\estimator.py", line 743, in _train_model
    features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\estimator\estimator.py", line 725, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "D:\tf_exe_5_make_image_lables\cnn_mnist.py", line 67, in cnn_model_fn
    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\losses\losses_impl.py", line 790, in sparse_softmax_cross_entropy
    labels, logits, weights, expected_rank_diff=1)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\losses\losses_impl.py", line 720, in _remove_squeezable_dimensions
    labels, predictions, expected_rank_diff=expected_rank_diff)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\confusion_matrix.py", line 76, in remove_squeezable_dimensions
    labels = array_ops.squeeze(labels, [-1])
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\array_ops.py", line 2490, in squeeze
    return gen_array_ops._squeeze(input, axis, name)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 7049, in _squeeze
    "Squeeze", input=input, squeeze_dims=axis, name=name)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3162, in create_op
    compute_device=compute_device)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3208, in _create_op_helper
    set_shapes_for_outputs(op)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2427, in set_shapes_for_outputs
    return _set_shapes_for_outputs(op)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2400, in _set_shapes_for_outputs
    shapes = shape_func(op)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2330, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 627, in call_cpp_shape_fn
    require_shape_fn)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 691, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Can not squeeze dim[1], expected a dimension of 1, got 3 for 'sparse_softmax_cross_entropy_loss/remove_squeezable_dimensions/Squeeze' (op: 'Squeeze') with input shapes: [100,3].
>>> 

这里的错误来自tf.loss.sparse\u softmax\u cross\u entropy(labels=labels,logits=logits)

TensorFlow文档明确指出“标签向量必须为每行logit的真实类提供一个特定的索引”。因此,标签向量必须只包含类索引,如0,1,2,而不是它们各自的热编码,如[1,0,0],[0,1,0],[0,0,1]

复制错误以进一步解释:

import numpy as np
import tensorflow as tf

# Create random-array and assign as logits tensor
np.random.seed(12345)
logits = tf.convert_to_tensor(np.random.sample((4,4)))
print logits.get_shape() #[4,4]

# Create random-labels (Assuming only 4 classes)
labels = tf.convert_to_tensor(np.array([2, 2, 0, 1]))

loss_1 = tf.losses.sparse_softmax_cross_entropy(labels, logits)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

print 'Loss: {}'.format(sess.run(loss_1)) # 1.44836854

# Now giving one-hot-encodings in place of class-indices for labels
wrong_labels = tf.convert_to_tensor(np.array([[0,0,1,0], [0,0,1,0], [1,0,0,0],[0,1,0,0]]))
loss_2 = tf.losses.sparse_softmax_cross_entropy(wrong_labels, logits)

# This should give you a similar error as soon as you define it
因此,尝试在Y_标签向量中给出类索引,而不是一个热编码。
希望这能消除你的疑虑

我已经解决了这个错误。标签采用了
onehot
编码,因此它的维度是
[,10]
,而不是
[,1]
。因此,我使用了
tf.argmax()
如果您使用Keras'
ImageDataGenerator
,您可以添加以获得正确的级别:

train_datagen = keras.preprocessing.image.ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
train_generator = train_datagen.flow_from_directory(
        'data/train',
        target_size=(150, 150),
        batch_size=32, 
        class_mode="sparse")

或者,您可以使用,这似乎对标签使用了onehot编码。

我编写的代码将[1,0,0]、[0,1,0]、[0,0,1]更改为0,1,2

将numpy导入为np
导入tensorflow作为tf
def将_更改为_right(错误的_标签):
右_标签=[]
对于错误的_标签中的x:
对于范围内的i(0,len(错误的_标签[0]):
如果x[i]==1:
右键\u标签。附加(i)
返回右箭头标签
错误的标签=np.数组([[0,0,1,0],[0,0,1,0],[1,0,0],[0,1,0])
右\u标签=tf.将\u转换为\u张量(np.数组(将\u更改为\u右(错误的\u标签)))
正在更改

loss='sparse_categorical_crossentropy'


为我工作。

简单地说,如果您对测试数据应用了labelbinarizer(用于热编码),那么损失函数应该是分类的。如果您尚未对测试数据进行热编码,则应使用“稀疏分类交叉熵”。

对于一种热编码或前面提到的其他选项,您可以更改为loss='classifical\u交叉熵'
tf.loss.sparse\u softmax\u cross\u entropy(标签、登录),

如果其他人试图运行tensorflow MNIST教程,我已经创建了一个。派林认证。天哪,谢谢!事实上,我认为文档不够清晰,但您的答案非常清楚。对于那些寻找从分类版本中轻松获取类索引的简单方法的人:使用numpy.argmax()检查以实现相同的功能。
loss='sparse_categorical_crossentropy'
loss='categorical_crossentropy'