当尝试使用密集层构建Keras顺序网络来进行多类分类时,我会收到“检查目标时出错”错误,请参阅下文。我可以对输出变量使用单热编码吗?所以我使用这个网络:
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
mean_len = 30
VEC_SIZE = 300
num_categories = 7
def baseline_model():
model = Sequential()
model.add(Dense(mean_len, activation='relu',input_shape=(mean_len, VEC_SIZE,)))
model.add(Dropout(0.2))
model.add(Dense(mean_len, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_categories, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy',
optimizer=RMSprop(),
metrics=['accuracy'])
return model
训练:
estimator = KerasClassifier(build_fn=baseline_model,
epochs=200, batch_size=100,
verbose=0, class_weight='balanced')
seed = 7
np.random.seed(seed)
kfold = KFold(n_splits=10, shuffle=True, random_state=seed)
results = cross_val_score(estimator,X_train, Y_train , cv=kfold)
print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
带输入向量:
X_train.shape
(6054, 30, 300)
和矢量看起来像这样:
X_train[0]
array([[ 0.01066018, 0.02115048, -0.0137242 , ..., -0.04065679,
0.0046372 , -0.01615561],
[-1.01996469, -0.5443387 , 2.59211802, ..., 0.85539412,
-0.34741816, -0.25221351],
[ 0.4434503 , 0.32438764, -0.26006699, ..., 0.27807152,
-0.2352812 , -0.6669544 ],
...,
[ 0.03539507, 0.00473431, 0.03123392, ..., -0.0063079 ,
0.03797895, 0.02079199],
[-0.03658897, 0.02584517, -0.01945854, ..., 0.0241626 ,
-0.03659866, -0.00615268],
[ 0.0182472 , -0.02080663, -0.02958447, ..., -0.04258969,
-0.04952002, 0.01375731]], dtype=float32)
输出矢量:
Y_train.shape
(6054, 7)
Y_train[0]
array([ 0., 0., 0., 0., 1., 0., 0.], dtype=float32)
错误输出:
Layer (type) Output Shape Param #
=================================================================
dense_23 (Dense) (None, 30, 30) 9030
_________________________________________________________________
dropout_18 (Dropout) (None, 30, 30) 0
_________________________________________________________________
dense_24 (Dense) (None, 30, 30) 930
_________________________________________________________________
dropout_19 (Dropout) (None, 30, 30) 0
_________________________________________________________________
dense_25 (Dense) (None, 30, 7) 217
=================================================================
Total params: 10,177
Trainable params: 10,177
Non-trainable params: 0
_________________________________________________________________
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-160-6a936b699825> in <module>()
----> 1 results = cross_val_score(estimator,X_train, Y_train , cv=kfold)
2 print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
...
...
...
D:\usr\anaconda\lib\site-packages\keras\engine\training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
126 ' to have ' + str(len(shapes[i])) +
127 ' dimensions, but got array with shape ' +
--> 128 str(array.shape))
129 for j, (dim, ref_dim) in enumerate(zip(array.shape, shapes[i])):
130 if not j and not check_batch_axis:
ValueError: Error when checking target: expected dense_25 to have 3 dimensions, but got array with shape (5448, 7)
有什么问题?