gpt4 book ai didi

python - Keras:无法将输入数组从形状 (14,1) 广播到形状 (14)

转载 作者:太空宇宙 更新时间:2023-11-03 13:09:21 25 4
gpt4 key购买 nike

我在 Windows 10 上将 Keras 与 Tensorflow 结合使用。我正在尝试为成人数据集 (https://archive.ics.uci.edu/ml/datasets/Adult) 创建一个机器学习模型。

首先,我在进行一次热编码,然后尝试训练我的模型,但我得到:
“ValueError:无法将输入数组从形状 (14,1) 广播到形状 (14)”

即使我将 input_dim 更改为 14 以外的值,我仍然会在相同的形状下遇到相同的错误。

我做错了什么?

df = pd.read_csv("adult_data.csv",header=None)
X = df.iloc[:,0:14]
Y = df.iloc[:,14]

encoder = LabelEncoder()
#X
for i in [1,3,5,6,7,8,9,13]:
column = X[i]
encoder.fit(column)
encoded_C = encoder.transform(column)
X[i] = np_utils.to_categorical(encoded_C)

print(X.shape)
#Y
encoder.fit(Y)
en_Y = encoder.transform(Y)
Y = np_utils.to_categorical(en_Y)

#model
model = Sequential()
model.add(Dense(21, input_dim=14, activation="relu"))
model.add(Dense(2, activation="softmax"))
#compile
model.compile(loss="categorical_crossentropy",optimizer="adam",metrics=
["accuracy"])

#train
model.fit(X,Y, epochs=50, batch_size=100)
score = model.evaluate(X,Y)
print("Accuracy: {}%".format(score[0]))

完全错误:

Traceback (most recent call last):
File "main.py", line 36, in <module>
model.fit(X,Y, epochs=50, batch_size=100)
File "C:\Users\K\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\models.py", line 960, in fit
validation_steps=validation_steps)
File "C:\Users\K\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1574, in fit
batch_size=batch_size)
File "C:\Users\K\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1407, in _standardize_user_data
exception_prefix='input')
File "C:\Users\K\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 128, in _standardize_input_data
arrays[i] = array
ValueError: could not broadcast input array from shape (14,1) into shape (14)

最佳答案

通过更新到 Keras 版本 2.1.2 修复了错误。

关于python - Keras:无法将输入数组从形状 (14,1) 广播到形状 (14),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47724077/

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