gpt4 book ai didi

python - Keras 由于形状原因不接受目标标签?

转载 作者:行者123 更新时间:2023-12-01 07:18:28 25 4
gpt4 key购买 nike

我正在尝试让 Keras 处理分类问题,该问题有五个分类目标标签(1、2、3、4、5)。由于某种原因,我在使用 StratifiedKFold 时无法使其工作。 X 和 y 分别是形状为 (500, 20) 和 (500, ) 的 NumPy 数组。

错误消息是“ValueError:检查目标时出错:预期dense_35具有形状(1,)但得到形状为(5,)的数组”,这使我认为错误肯定在于格式目标变量。还值得注意的是,每次尝试运行代码时,“dense_35”中的数字似乎都在变化。

random_state = 123
n_splits = 10
cv = StratifiedKFold(n_splits=n_splits,
random_state=random_state, shuffle=False)

def baseline_model():
nn_model = Sequential()
nn_model.add(Dense(units=50, input_dim=X.shape[1], init='normal',
activation= 'relu' ))
nn_model.add(Dense(30, init='normal', activation='relu'))
nn_model.add(Dense(10, init='normal', activation='relu'))
nn_model.add(Dense(1, init='normal', activation='softmax'))

nn_model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics = ['accuracy'])
return nn_model

for train, test in cv.split(X, y):
X_train, X_test = X[train], X[test]
y_train, y_test = y[train], y[test]

np_utils.to_categorical(y_train)
np_utils.to_categorical(y_test)

estimator = KerasClassifier(build_fn=baseline_model,
epochs=200, batch_size=5,
verbose=0)

estimator.fit(X_train, y_train)
y_pred = estimator.predict(X_test)

The numpy array (y), that I am trying to split:
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5]

最佳答案

random_state = 123
n_splits = 10
cv = StratifiedKFold(n_splits=n_splits, random_state=random_state, shuffle=False)
def baseline_model():
nn_model = Sequential(name='model_name')
nn_model.add(Dense(units=50, input_dim=X.shape[1], init='normal',
activation= 'relu', name='dense1'))
nn_model.add(Dense(30, init='normal', activation='relu', name='dense2'))
nn_model.add(Dense(10, init='normal', activation='relu', name='dense3'))
# code changed here
nn_model.add(Dense(5, init='normal', activation='softmax', name='dense4'))

nn_model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics = ['accuracy'])
return nn_model

for train, test in cv.split(X, y):
X_train, X_test = X[train], X[test]
y_train, y_test = y[train], y[test]

# the error is due to this step
# you have specified only one output in the last dense layer (dense4)
# but you are giving input of length 5
np_utils.to_categorical(y_train)
np_utils.to_categorical(y_test)

estimator = KerasClassifier(build_fn=baseline_model,
epochs=200, batch_size=5,
verbose=0)
estimator.fit(X_train, y_train)
y_pred = estimator.predict(X_test)
  • 通过在图层中指定名称参数,您可以命名图层。通过这样做,您将获得图层的明确名称,以防每次出错。
  • model.summary() 是另一个有用的函数,您可以使用它检查每层的输出形状。

关于python - Keras 由于形状原因不接受目标标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57835678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com