Keras 多分类器总是给出 0 输出

Keras multi classifier is always giving 0 output

我为多分类构建了这个 keras 模型

model = tf.keras.Sequential([
  tf.keras.layers.Dense(32, activation='relu'),
  tf.keras.layers.Dense(16, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(8, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam',
              loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True))

这是我的数据,已标准化

这些是我从 0 到 5 的目标值

我使用这段代码来准备数据集

train_dataset = tf.data.Dataset.from_tensor_slices((dff[:92000], target[:92000]))
test_dataset = tf.data.Dataset.from_tensor_slices((dff[500:520]))
# random slice of test dataset

train_dataset = train_dataset.batch(100)
test_dataset = test_dataset.batch(1)

然后我用这个训练和测试了我的模型

model.fit(train_dataset, epochs=20) 

predictions = model.predict(test_dataset)
classes = np.argmax(predictions, axis = 1)

类 输出始终为 0:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

我已经尝试过不规范化我的数据,但输出仍然相同。

来自评论

After changing it to Dense(5, activation='softmax') has resolved the issue (paraphrased from M.Innat)

工作代码如下所示

model = tf.keras.Sequential([
  tf.keras.layers.Dense(32, activation='relu'),
  tf.keras.layers.Dense(16, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(8, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(5, activation='softmax')
])

model.compile(optimizer='adam',
              loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True))

Keras multi classifier is always giving 0 output is due to your last > dense layer has only 1 unit which means your outputs has a size (1,) and you are applying argmax operation that results getting index 0 everytime (paraphrased from Frightera)