簡體   English   中英

TensorFlow 不兼容形狀二進制分類

[英]TensorFlow incompatible shapes binary classification

我有一個 pandas dataframe 的特征和樣本,以及一個具有二進制類別(0 或 1)值的單個系列。 有了這個,我正在嘗試訓練一個神經網絡,但我得到了錯誤:

TensorFlow incompatible shapes binary classification

以下是代碼摘要:

X_train, X_test, y_train, y_test = train_test_split(df_x, series_y, random_state=1, test_size=0.25)

best_weight_path = 'best_weights.hdf5'

x = df_x.to_numpy()
y = series_y.to_numpy()

numpy_x_train = X_train.to_numpy()
numpy_y_train = y_train.to_numpy()
numpy_x_test = X_test.to_numpy()
numpy_y_test = y_test.to_numpy()
model = Sequential()
model.add(Dense(20, input_dim=x.shape[1], activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=5, verbose=1, mode='auto')
checkpointer = ModelCheckpoint(filepath=best_weight_path, verbose=0, save_best_only=True)
model.fit(x, y, validation_data=(numpy_x_test, numpy_y_test), callbacks=[monitor, checkpointer], verbose=0, epochs=1000)

ValueError: Shapes (None, 1) and (None, 2) are incompatible

最后一個密集層不應該有 2 個單位,因為有兩種可能的結果,那么形狀(None, 1)來自哪里?

問題與根據您的標簽格式正確選擇適當的損失 function 有關。 在分類任務中使用 softmax 時有兩種可能性:

1 種可能性:如果您有 1D integer 編碼目標,則可以使用sparse_categorical_crossentropy作為損失 function (這似乎是您的情況

n_class = 2
n_features = 100
n_sample = 1000

X = np.random.randint(0,10, (n_sample,n_features))
y = np.random.randint(0,n_class, n_sample)

inp = Input((n_features,))
x = Dense(128, activation='relu')(inp)
out = Dense(n_class, activation='softmax')(x)

model = Model(inp, out)
model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
history = model.fit(X, y, epochs=3)

2 種可能性:如果您對目標進行一次熱編碼以獲得 2D 形狀(n_samples,n_class),則可以使用categorical_crossentropy

n_class = 2
n_features = 100
n_sample = 1000

X = np.random.randint(0,10, (n_sample,n_features))
y = pd.get_dummies(np.random.randint(0,n_class, n_sample)).values

inp = Input((n_features,))
x = Dense(128, activation='relu')(inp)
out = Dense(n_class, activation='softmax')(x)

model = Model(inp, out)
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
history = model.fit(X, y, epochs=3)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM