【发布时间】: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的形状吗? -
好的。等一下。
标签: python-3.x tensorflow keras valueerror