ValueError:维度 (-1) 必须在 [0, 2) 范围内
Posted
技术标签:
【中文标题】ValueError:维度 (-1) 必须在 [0, 2) 范围内【英文标题】:ValueError: Dimension (-1) must be in the range [0, 2) 【发布时间】:2018-02-22 10:19:57 【问题描述】:我的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: [?,?], [].
我尝试在谷歌上找到答案...但没有与我相同的问题。
需要帮助...我很感激...
【问题讨论】:
第 25 行是哪一行?我只能看到 22 个帖子。 对不起。我放错线了。第 25 行被更正为第 20 行。这里显示第 20 行的代码。 [model.compile(loss='mean_squared_error', optimizer=sgd, metrics=['accuracy'])] 你能打印x_train, y_train, x_test, y_test
的形状吗?
好的。等一下。
【参考方案1】:
我保存我的问题...
我升级了我的 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 个小时来解决我的错误问题,但这很有趣。
【讨论】:
你确定它在升级命令之后使用 gpu 吗?我做了同样的事情,它开始使用 cpu 版本的张量流。以上是关于ValueError:维度 (-1) 必须在 [0, 2) 范围内的主要内容,如果未能解决你的问题,请参考以下文章
Keras ValueError: 维度必须相等,但对于 'node Equal 是 6 和 9
sklearn管道ValueError:除连接轴外的所有输入数组维度必须完全匹配
ValueError:matmul:输入操作数 1 在其核心维度 0 中不匹配
ValueError:无法将大小为0的序列复制到维度为56的数组轴
ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到的数组具有形状(无,1)
Tensorflow 维度问题:ValueError: Shapes (3, 1) and (None, 3) is incompatible