简体   繁体   English

TensorFlow 不兼容形状二进制分类

[英]TensorFlow incompatible shapes binary classification

I have a pandas dataframe of features and samples, and a single series with binary category (0 or 1) values.我有一个 pandas dataframe 的特征和样本,以及一个具有二进制类别(0 或 1)值的单个系列。 With that I'm trying to train a neural network, but I am getting the error:有了这个,我正在尝试训练一个神经网络,但我得到了错误:

TensorFlow incompatible shapes binary classification

Here is a summary of the code:以下是代码摘要:

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

Shouldn't the last dense layer have 2 units as there are two possible outcomes, so where is the shape (None, 1) coming from?最后一个密集层不应该有 2 个单位,因为有两种可能的结果,那么形状(None, 1)来自哪里?

The problem is related to the correct choice of an appropriate loss function according to the format of your labels.问题与根据您的标签格式正确选择适当的损失 function 有关。 you have 2 possibilities when using softmax in classification task:在分类任务中使用 softmax 时有两种可能性:

1 possibility: if you have 1D integer encoded target, you can use sparse_categorical_crossentropy as loss function ( this seems to be your case ) 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 possibility: if you have one-hot encoded your target in order to have 2D shape (n_samples, n_class), you can use categorical_crossentropy 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