ValueError:Dimension(-1)必须在[0,2}

时间:2017-09-13 15:56:46

标签: python-3.x tensorflow keras valueerror

我的python版本是3.5.2。

我已经安装了keras和tensorflow,我尝试了官方的一些例子。

示例链接: Example title: Multilayer Perceptron (MLP) for multi-class softmax classification:

我在我的python IDEL下复制示例并显示代码:

import kerasfrom keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD


import numpy as np
x_train = np.random.random((1000, 20))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
x_test = np.random.random((100, 20))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)

model = Sequential()
model.add(Dense(64, activation='relu', input_dim=20))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy'])
model.fit(x_train, y_train,epochs=20,batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)

显示错误信息:

Using TensorFlow backend.
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 670, in _call_cpp_shape_fn_impl
    status)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\contextlib.py", line 66, in __exit__
    next(self.gen)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 469, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: 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: [?,?], [].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/keras/practice.py", line 25, in <module>
    model.compile(loss='mean_squared_error', optimizer=sgd, metrics=['accuracy'])
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\models.py", line 784, in compile
    **kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py", line 924, in compile
    handle_metrics(output_metrics)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py", line 921, in handle_metrics
    mask=masks[i])
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py", line 450, in weighted
    score_array = fn(y_true, y_pred)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\metrics.py", line 25, in categorical_accuracy
    return K.cast(K.equal(K.argmax(y_true, axis=-1),
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\backend\tensorflow_backend.py", line 1333, in argmax
    return tf.argmax(x, axis)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\math_ops.py", line 249, in argmax
    return gen_math_ops.arg_max(input, axis, name)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 168, in arg_max
    name=name)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 759, in apply_op
    op_def=op_def)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2242, in create_op
    set_shapes_for_outputs(ret)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1617, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1568, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 610, in call_cpp_shape_fn
    debug_python_shape_fn, require_shape_fn)
  File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\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: [?,?], [].

我试图在谷歌上找到答案......但没有与我相同的问题。

需要帮助......我很感激......

1 个答案:

答案 0 :(得分:2)

我保存了我的问题......

我升级了我的tensorflow版本,程序可以正常工作。

我尝试使用此命令进行升级。

pip3 install --upgrade tensorflow

我可以跑了之后。另一个问题是示例精度是如此之低?

结果显示:

Using TensorFlow backend.
Epoch 1/20

 128/1000 [==>...........................] - ETA: 1s - loss: 0.7514 - acc: 0.4297
1000/1000 [==============================] - 0s - loss: 0.7193 - acc: 0.4690     
Epoch 2/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.7264 - acc: 0.4141
1000/1000 [==============================] - 0s - loss: 0.7019 - acc: 0.5090     
Epoch 3/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.7056 - acc: 0.5234
1000/1000 [==============================] - 0s - loss: 0.7063 - acc: 0.4920     
Epoch 4/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6822 - acc: 0.5625
1000/1000 [==============================] - 0s - loss: 0.6994 - acc: 0.5180     
Epoch 5/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6946 - acc: 0.5000
1000/1000 [==============================] - 0s - loss: 0.7004 - acc: 0.4980     
Epoch 6/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6901 - acc: 0.5547
1000/1000 [==============================] - 0s - loss: 0.6978 - acc: 0.5130     
Epoch 7/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6946 - acc: 0.5156
1000/1000 [==============================] - 0s - loss: 0.7027 - acc: 0.4910     
Epoch 8/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.7035 - acc: 0.4922
1000/1000 [==============================] - 0s - loss: 0.6960 - acc: 0.5240     
Epoch 9/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6975 - acc: 0.4844
1000/1000 [==============================] - 0s - loss: 0.6959 - acc: 0.4990     
Epoch 10/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.7127 - acc: 0.4453
1000/1000 [==============================] - 0s - loss: 0.6989 - acc: 0.4980     
Epoch 11/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6862 - acc: 0.5312
1000/1000 [==============================] - 0s - loss: 0.6867 - acc: 0.5240     
Epoch 12/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6815 - acc: 0.5469
1000/1000 [==============================] - 0s - loss: 0.6913 - acc: 0.5190     
Epoch 13/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6991 - acc: 0.5156
1000/1000 [==============================] - 0s - loss: 0.6931 - acc: 0.5340     
Epoch 14/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6834 - acc: 0.5391
1000/1000 [==============================] - 0s - loss: 0.6951 - acc: 0.5000     
Epoch 15/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6900 - acc: 0.5547
1000/1000 [==============================] - 0s - loss: 0.6926 - acc: 0.5310     
Epoch 16/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6945 - acc: 0.5469
1000/1000 [==============================] - 0s - loss: 0.6896 - acc: 0.5320     
Epoch 17/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6995 - acc: 0.4688
1000/1000 [==============================] - 0s - loss: 0.6902 - acc: 0.5530     
Epoch 18/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6788 - acc: 0.6016
1000/1000 [==============================] - 0s - loss: 0.6927 - acc: 0.5180     
Epoch 19/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.7072 - acc: 0.5234
1000/1000 [==============================] - 0s - loss: 0.6960 - acc: 0.5230     
Epoch 20/20

 128/1000 [==>...........................] - ETA: 0s - loss: 0.6884 - acc: 0.5625
1000/1000 [==============================] - 0s - loss: 0.6933 - acc: 0.5180     

100/100 [==============================] - 0s

我想再次感谢大家。

尽管我花了3个小时来解决我的错误问题,但它非常有趣。