我突然发现这个错误与带有tensorflow后端的kears(python2.7),每个代码都有相同的错误。我认为它的keras 1和2不兼容,但它不是
Dimension (-1) must be in the range [0, 2), where 2 is the number of dimensions in the input. for 'metrics/acc/ArgMax' (op: 'ArgMax') with input shapes: [?,?], [].
'我更新张力流和keras类似的问题(链接↓↓),但仍然相同的错误 ValueError: Dimension (-1) must be in the range [0, 2) 完整代码(示例)
**Code updated the whole code**
using TensorFlow backend.
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcublas.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcudnn.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcufft.so locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcurand.so locally
60000 train samples
10000 test samples
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 512) 401920
_________________________________________________________________
dropout_1 (Dropout) (None, 512) 0
_________________________________________________________________
dense_2 (Dense) (None, 512) 262656
_________________________________________________________________
dropout_2 (Dropout) (None, 512) 0
_________________________________________________________________
dense_3 (Dense) (None, 10) 5130
=================================================================
Total params: 669,706
Trainable params: 669,706
Non-trainable params: 0
_________________________________________________________________
Traceback (most recent call last):
File "mnist_mlp.py", line 48, in <module>
metrics=['accuracy'])
File "/home/usr/miniconda2/lib/python2.7/site-packages/keras/models.py", line 784, in compile
**kwargs)
File "/home/usr/miniconda2/lib/python2.7/site-packages/keras/engine/training.py", line 924, in compile
handle_metrics(output_metrics)
File "/home/usr/miniconda2/lib/python2.7/site-packages/keras/engine/training.py", line 921, in handle_metrics
mask=masks[i])
File "/home/usr/miniconda2/lib/python2.7/site-packages/keras/engine/training.py", line 450, in weighted
score_array = fn(y_true, y_pred)
File "/home/usr/miniconda2/lib/python2.7/site-packages/keras/metrics.py", line 25, in categorical_accuracy
return K.cast(K.equal(K.argmax(y_true, axis=-1),
File "/home/usr/miniconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 1333, in argmax
return tf.argmax(x, axis)
File "/home/usr/.local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 249, in argmax
return gen_math_ops.arg_max(input, axis, name)
File "/home/usr/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 168, in arg_max
name=name)
File "/home/usr/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
op_def=op_def)
File "/home/usr/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2242, in create_op
set_shapes_for_outputs(ret)
File "/home/usr/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1617, in set_shapes_for_outputs
shapes = shape_func(op)
File "/home/usr/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1568, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/home/usr/.local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
debug_python_shape_fn, require_shape_fn)
File "/home/usr/.local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 675, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Dimension (-1) must be in the range [0, 2), where 2 is the number of dimensions in the input. for 'metrics/acc/ArgMax' (op: 'ArgMax') with input shapes: [?,?], [].'
答案 0 :(得分:2)
我刚刚开始和Keras一起玩,我遇到了同样的问题。我遵循了在不同论坛上提出的不同解决方法 - 包括运行tensorflow / keras本身的升级 - 但这似乎对我没有用。
问题似乎是默认情况下调用Keras.backend中的argmax函数,其中轴= -1,超出范围,因为只有[0,2}是合法的。
我的解决方案一直在重写分类准确度函数:
import keras.backend as K
def get_categorical_accuracy_keras(y_true, y_pred):
return K.mean(K.equal(K.argmax(y_true, axis=1), K.argmax(y_pred, axis=1)))
(我在this thread中找到了公式)
应该等同于以下函数,该函数利用numpy库:
import numpy as np
def get_categorical_accuracy(y_true, y_pred):
return (np.argmax(y_true, axis=1) == np.argmax(y_pred, axis=1)).mean()
在模型编译中使用 get_categorical_accuracy_keras 函数:
model.compile(loss=losses.categorical_crossentropy, optimizer='adam', metrics=[get_categorical_accuracy_keras])
似乎解决了这个问题。
当然,我想自己使用已经定义的准确性,所以欢迎任何建议
答案 1 :(得分:1)
当我尝试将已保存的模型从我的mac加载到DigOcean时,我遇到了相同的错误消息(由于默认的Digital Ocean应用程序)。使用以下方法更新了tensorflow:
pip3 install --upgrade tensorflow
和1.3.0,当我重新启动jupyter内核时问题得到了解决。