作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对深度学习非常陌生:我正在 Udemy 上学习深度学习类(class)。一旦我执行我的代码,它会说:
ValueError:模型未配置为计算准确性。您应该将 metrics=["accuracy"]
传递给 model.compile()
方法。
我尝试将“accuracy”更改为“acc”,它不再给我带来错误,但是代码非常快,它只显示了 10 个 epoch,而不是 100。此外,我预计准确度约为 83%,但我是大约 79%。
有人可以帮我吗?
''''
import keras
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import cross_val_score
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
def build_classifier():
classifier = Sequential()
classifier.add(Dense(6, kernel_initializer='uniform', activation='relu', input_dim= 11))
classifier.add(Dense(6, kernel_initializer='uniform', activation='relu'))
classifier.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
return classifier
classifier = KerasClassifier(build_fn= build_classifier, batch_size=10 , nb_epoch=100)
accuracies = cross_val_score(estimator = classifier, X = x_train, y = y_train, cv=10, n_jobs=-1)
''''这是我的完整回溯:
''''
accuracies = cross_val_score(estimator = classifier, X = x_train, y = y_train, cv=10, n_jobs=-1, verbose=0)
Traceback (most recent call last):
File "<ipython-input-138-36c63e93365e>", line 1, in <module>
accuracies = cross_val_score(estimator = classifier, X = x_train, y = y_train, cv=10, n_jobs=-1, verbose=0)
File "C:\ProgramData\Anaconda3\envs\deeplearning\lib\site-packages\sklearn\model_selection\_validation.py", line 389, in cross_val_score
error_score=error_score)
File "C:\ProgramData\Anaconda3\envs\deeplearning\lib\site-packages\sklearn\model_selection\_validation.py", line 235, in cross_validate
for train, test in cv.split(X, y, groups))
File "C:\ProgramData\Anaconda3\envs\deeplearning\lib\site-packages\joblib\parallel.py", line 1017, in __call__
self.retrieve()
File "C:\ProgramData\Anaconda3\envs\deeplearning\lib\site-packages\joblib\parallel.py", line 909, in retrieve
self._output.extend(job.get(timeout=self.timeout))
File "C:\ProgramData\Anaconda3\envs\deeplearning\lib\site-packages\joblib\_parallel_backends.py", line 562, in wrap_future_result
return future.result(timeout=timeout)
File "C:\ProgramData\Anaconda3\envs\deeplearning\lib\concurrent\futures\_base.py", line 435, in result
return self.__get_result()
File "C:\ProgramData\Anaconda3\envs\deeplearning\lib\concurrent\futures\_base.py", line 384, in __get_result
raise self._exception
ValueError: The model is not configured to compute accuracy. You should pass `metrics=["accuracy"]` to the `model.compile()` method.
''''
我也尝试过这个:'
try:
accuracies = cross_val_score(estimator = classifier, X = x_train, y = y_train, cv=10, n_jobs=-1, verbose=0)
except ValueError as e:
print ("ValueError:", e)
ValueError: The model is not configured to compute accuracy. You should pass `metrics=["accuracy"]` to the `model.compile()` method.
'
最佳答案
在导入中,您在 keras
和 tf.keras
包之间混合导入,这实际上是不支持的,您应该选择一个并从所选包中进行所有导入。
关于python - ValueError : The model is not configured to compute accuracy. 您应该传递 `metrics=["准确度“]` to the ` model.compile()` 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59556100/
我是一名优秀的程序员,十分优秀!