You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow二元交叉熵模型始终输出相同预测值求助

TensorFlow模型对所有测试样本输出相同预测值的问题

我刚加入Stack Overflow社区,接触TensorFlow仅1周,肯定遗漏了某些要点,自身无法解决问题。

我已使用TensorFlow示例数据集完成若干练习,也学习了一些零散教程。尝试将其中一份教程的代码应用到自有CSV数据集,该模型需预测Last Result列的值,但很快遇到问题:predict函数对所有数据条目始终输出相同的预测值。

输出示例:

[[0.6335701]
[0.6335701]
...
[0.6335701]
[0.6335701]]

代码如下:

import pandas as pd
from sklearn.model_selection import train_test_split
from keras.models import Sequential, load_model
from keras.layers import Dense
from sklearn.metrics import accuracy_score

df = pd.read_csv('*.csv')
x = pd.get_dummies(df.drop(['Last Result'], axis=1))
y = df['Last Result'].apply(lambda X: 1 if X == 'Registered' else 0)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.2)
x_train.head()
print("x train: \n", x_train)
y_train.head()
print("y train: \n", y_train)

# Below is code to create a new TensorFlow Model. If you need to call a saved model, see 'load model' below.

model = Sequential()
model.add(Dense(units=32, activation='relu', input_dim=len(x_train.columns)))
model.add(Dense(units=32, activation='relu'))
model.add(Dense(units=1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=100, batch_size=128)

# model.save('*.keras')

# This is code to recall a previously saved TensorFlow Model

# model = load_model('*.keras')

y_hat = model.predict(x_test)
# y_hat = [0 if val < 0.5 else 1 for val in y_hat]
print(y_hat)

# print(accuracy_score(y_test, y_hat))

我已尝试同类帖子的建议但无效。我推测模型未进行有效学习,因目标类别中Registered的占比约60%,故预测值趋近该比例。

恳请帮助解决此问题,以加深我对TensorFlow库的理解,感谢您的时间与帮助。

内容的提问来源于stack exchange,提问作者JLadage

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 18:25:19