原始資料框有 20,000 行,對于演示文稿,我準備了一個 df 有 20 行。我的模型給出了 0.03 級別的 acc(對于 df 20k 行),并且在不同時期之間沒有變化。對于我犯錯誤的建議,我將不勝感激。我的代碼:
import keras
from keras.models import Sequential
from keras.layers import Dense
from tensorflow.keras.optimizers import Adam
import pandas as pd
df = pd.DataFrame({'lettr': ['T','I','D','N','G','S','B','A','J','M','X','O','G','M','R','F','O','C','T', 'J'],
'x-box': [2, 5, 4, 7, 2, 4, 4, 1, 2, 11, 3, 6, 4, 6, 5, 6, 3, 7, 6, 2],
'y-box': [8, 12, 11, 11, 1, 11, 2, 1, 2, 15, 9, 13, 9, 9, 9, 9, 4, 10, 11, 2],
'width': [3, 3, 6, 6, 3, 5, 5, 3, 4, 13, 5, 4, 6, 8, 5, 5, 4, 5, 6, 3],
'high': [5, 7, 8, 6, 1, 8, 4, 2, 4, 9, 7, 7, 7, 6, 7, 4, 3, 5, 8, 3],
'onpix':[1, 2, 6, 3, 1, 3, 4, 1, 2, 7, 4, 4, 6, 9, 6, 3, 2, 2, 5, 1],
'x-bar':[8, 10, 10, 5, 8, 8, 8, 8, 10, 13, 8, 6, 7, 7, 6, 10, 8, 6, 6, 10],
'y-bar':[13, 5, 6, 9, 6, 8, 7, 2, 6, 2, 7, 7, 8, 8, 11, 6, 7, 8, 11, 6],
'x2bar':[0, 5, 2, 4, 6, 6, 6, 2, 2, 6, 3, 6, 6, 6, 7, 3, 7, 6, 5, 3],
'y2bar':[6, 4, 6, 6, 6, 9, 6, 2, 6, 2, 8, 3, 2, 5, 3, 5, 5, 8, 6, 6],
'xybar':[6, 13, 10, 4, 6, 5, 7, 8, 12, 12, 5, 10, 6, 7, 7, 10, 7, 11, 11, 12],
'x2ybr':[10, 3, 3, 4, 5, 6, 6, 2, 4, 1, 6, 7, 5, 5, 3, 5, 6, 7, 9, 4],
'xy2br':[8, 9, 7, 10, 9, 6, 6, 8, 8, 9, 8, 9, 11, 8, 9, 7, 8, 11, 4, 9],
'x-ege':[0, 2, 3, 6, 1, 0, 2, 1, 1, 8, 2, 5, 4, 8, 2, 3, 2, 2, 3, 0],
'xegvy':[8, 8, 7, 10, 7, 8, 8, 6, 6, 1, 8, 9, 8, 9, 7, 9, 8, 8, 12, 7],
'y-ege':[0, 4, 3, 2, 5, 9, 7, 2, 1, 1, 6, 5, 7, 8, 5, 6, 3, 5, 2, 1],
'yegvx':[8, 10, 9, 8, 10, 7, 10, 7, 7, 8, 7, 8, 8, 6, 11, 9, 8, 9, 4, 7],
})
def naiveEncode(col):
values = list(col.unique())
return col.apply(lambda x: values.index(x))
df["lettr"] = naiveEncode(df["lettr"])
X = df.iloc[:,1:].values
y = df.iloc[:, 0].values
from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()
y1 = encoder.fit_transform(y)
Y = pd.get_dummies(y1).values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(128, activation="relu", input_shape=(16,)),
tf.keras.layers.Dense(64, activation="relu",),
tf.keras.layers.Dense(32, activation="relu",),
tf.keras.layers.Dense(26, activation="softmax",)
])
model.compile(Adam(lr=0.04),'categorical_crossentropy',metrics=['accuracy'])
model.summary()
model.fit(X_train,y_train,epochs=10)
uj5u.com熱心網友回復:
抱歉我的第一個答案,我完全錯過了您在錯誤情況下使用categorical_crossentropy損失的事實。
在您的情況下,您應該使用sparse_categorical_crossentropy而不是categorical_crossentropy。您應該檢查: https ://stats.stackexchange.com/questions/326065/cross-entropy-vs-sparse-cross-entropy-when-to-use-one-over-the-other
此外,您應該使用 softmax 激活函式將輸出層更新為適合您擁有的類數(如果您擁有所有字母表,則為 26 個)的 Dense 層。
順便說一句,我建議您添加測驗資料作為模型訓練的驗證指標。
修改后的代碼:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(128, activation="relu", input_shape=(16,)),
tf.keras.layers.Dense(64, activation="relu",),
tf.keras.layers.Dense(32, activation="relu",),
tf.keras.layers.Dense(26, activation="softmax",)
])
model.compile(Adam(lr=0.04),'sparse_categorical_crossentropy',metrics=['accuracy'])
model.summary()
model.fit(X_train,y_train,
validation_data= (X_test, y_test),
epochs=10)
uj5u.com熱心網友回復:
問題來自您的 y 變數不包含您想要的目標列的事實。
y = df.iloc[:, 0].values將您的第一列作為目標,而您希望 df["lettr"] 作為目標。您應該將其替換為:
y = df["lettr"].values
然后你需要以這種方式調整你的輸入 X :
X = df.loc[:, df.columns != "lettr"].values
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/495334.html
