【发布时间】:2021-06-12 19:29:56
【问题描述】:
我为多分类构建了这个 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]
我已经尝试不规范化我的数据,但输出仍然相同。
【问题讨论】:
-
如你所说的多类并设置这样的损失函数,那么
Dense(1, activation='sigmoid')是什么? -
你应该把它改成
Dense(5, activation='softmax') -
此外,您的最后一个密集层只有 1 单位,这意味着您的输出大小为
(1,),并且您正在应用 argmax 操作,结果每次都获得索引 0。跨度> -
@Jatin,你按照上面的建议试过了吗?
-
@M.Innat 为这么长时间的延迟道歉。非常感谢它工作得很好!我忙于解决其他错误,以至于我完全忘记了这个线程。
标签: python tensorflow keras text-classification multilabel-classification