我在Windows 10上使用Keras和Tensorflow。我正在尝试为Adult数据集(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)
答案 0 :(得分:7)
通过更新到Keras版本2.1.2修复了错误。