当我运行此代码时,我收到一个错误,我无法识别并获得解决方案。如果有人能给我一个错误的解决方案,对我来说会很有帮助。我使用python 3.5.2,tensorflow版本1.4.0,keras 2.1.2,pandas版本0.21.1和scikit-learn版本0.19.1。我在IDLE ide中运行它。代码是:
import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline
seed = 7
numpy.random.seed(seed)
dataframe = pandas.read_csv("iris.csv", header=None)
dataset = dataframe.values
X = dataset[:,0:4].astype(float)
Y = dataset[:,4]
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
dummy_y = np_utils.to_categorical(encoded_Y)
def baseline_model():
model = Sequential()
model.add(Dense(8, input_dim=4, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
estimator = KerasClassifier(build_fn=baseline_model, epochs=200, batch_size=5, verbose=0)
kfold = KFold(n_splits=10, shuffle=True, random_state=seed)
results = cross_val_score(estimator, X, dummy_y, cv=kfold)
print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
错误是:
Using TensorFlow backend.
Traceback (most recent call last):
File "F:/7th semester/machine language/thesis work/python/iris2.py", line 36, in <module>
results = cross_val_score(estimator, X, dummy_y, cv=kfold)
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\sklearn\model_selection\_validation.py", line 342, in cross_val_score
pre_dispatch=pre_dispatch)
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\sklearn\model_selection\_validation.py", line 206, in cross_validate
for train, test in cv.split(X, y, groups))
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\sklearn\externals\joblib\parallel.py", line 779, in __call__
while self.dispatch_one_batch(iterator):
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\sklearn\externals\joblib\parallel.py", line 625, in dispatch_one_batch
self._dispatch(tasks)
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\sklearn\externals\joblib\parallel.py", line 588, in _dispatch
job = self._backend.apply_async(batch, callback=cb)
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 111, in apply_async
result = ImmediateResult(func)
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py", line 332, in __init__
self.results = batch()
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in __call__
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\sklearn\externals\joblib\parallel.py", line 131, in <listcomp>
return [func(*args, **kwargs) for func, args, kwargs in self.items]
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\sklearn\model_selection\_validation.py", line 458, in _fit_and_score
estimator.fit(X_train, y_train, **fit_params)
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\wrappers\scikit_learn.py", line 203, in fit
return super(KerasClassifier, self).fit(x, y, **kwargs)
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\wrappers\scikit_learn.py", line 147, in fit
history = self.model.fit(x, y, **fit_args)
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\models.py", line 960, in fit
validation_steps=validation_steps)
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py", line 1581, in fit
batch_size=batch_size)
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py", line 1418, in _standardize_user_data
exception_prefix='target')
File "C:\Users\maisha\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\engine\training.py", line 153, in _standardize_input_data
str(array.shape))
ValueError: Error when checking target: expected dense_2 to have shape (None, 3) but got array with shape (90, 40)
答案 0 :(得分:0)
您可以考虑更改代码行:
model.add(Dense(8, input_dim=4, activation='relu'))
到
model.add(Dense(4, input_dim=4, activation='relu'))
由于网络的拓扑是4个输入- 4个隐藏节点- 3个输出。