Tensorflow 模型返回恒定预测 [关闭]

Posted

技术标签:

【中文标题】Tensorflow 模型返回恒定预测 [关闭]【英文标题】:Tensorflow model returns constant predictions [closed] 【发布时间】:2021-12-04 17:37:53 【问题描述】:

我的 Tensorflow python 代码有问题。我希望你能帮助我!

Python 代码:

import tensorflow as tf
import pandas as pd
from sklearn.model_selection import train_test_split


dataset = pd.read_csv("database2.csv")
x = dataset.drop(columns=["good/bad"])
y = dataset["good/bad"]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD

from sklearn.metrics import accuracy_score

optimizer = SGD(learning_rate=0.0001)
model = Sequential()
model.add(Dense(units=32, activation="relu", input_dim=len(x_train.columns)))
model.add(Dense(units=64, activation="relu"))
model.add(Dense(units=1, activation="sigmoid"))
model.compile(loss="binary_crossentropy", optimizer=optimizer, metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, batch_size=32)
y_hat = model.predict(x_test)
print('Predictions \n', y_hat)

print(y_hat)

y_hat = [0 if val < 0.5 else 1 for val in y_hat]
print(accuracy_score(y_test, y_hat))
model.save("tfmodel2.model")

database2.csv中的数据(只是一小块):

good/bad,version,ihl,len_,id_,frag,ttl,dport,seq,ack,dataofs,reserved,window,urgptr
1,4,5,40,27015,0,128,80,2984614429,3573894115,5,0,256,0
1,4,5,40,27040,0,128,80,2115958310,1919934020,5,0,256,0
1,4,5,40,50867,0,64,59477,1919934020,2115958311,5,0,501,0
1,4,5,40,27041,0,128,80,1166711346,1716471989,5,0,256,0
1,4,5,40,37473,0,64,59483,4228080830,3375657208,5,0,501,0
1,4,5,40,27058,0,128,80,1142889874,138479511,5,0,256,0
1,4,5,40,40979,0,64,59495,138479511,1142889875,5,0,501,0
1,4,5,40,27072,0,128,80,2005624071,3236453347,5,0,256,0
1,4,5,40,64294,0,64,59500,1133180730,1618819042,5,0,501,0
1,4,5,40,27079,0,128,80,3380492062,3355098367,5,0,256,0
1,4,5,40,27081,0,128,80,3347163551,1847047182,5,0,256,0
1,4,5,40,27111,0,128,80,181291518,3122049624,5,0,256,0
1,4,5,40,27112,0,128,80,1374363297,281264184,5,0,256,0
1,4,5,40,27113,0,128,80,1558367091,220950531,5,0,256,0
1,4,5,40,27121,0,128,80,1156537063,2055662768,5,0,256,0
1,4,5,40,27122,0,128,80,541593967,2755717529,5,0,256,0
1,4,5,40,27123,0,128,80,3032169689,1904875694,5,0,256,0
1,4,5,40,27124,0,128,80,1041875722,2901294249,5,0,256,0
1,4,5,40,27125,0,128,80,2552529427,814943902,5,0,256,0
1,4,5,40,27126,0,128,80,26367856,2266313616,5,0,256,0
1,4,5,40,27127,0,128,80,2115958311,1919934021,5,0,256,0
1,4,5,40,27128,0,128,80,1166711347,1716471990,5,0,256,0
1,4,5,40,27129,0,128,80,3061871610,3860343080,5,0,256,0
1,4,5,40,27145,0,128,80,1142889875,138479512,5,0,256,0
1,4,5,40,27146,0,128,80,1868008488,3876007865,5,0,256,0
0,4,5,64,0,0,64,80,2408682295,0,11,0,65535,0
0,4,5,60,0,0,64,53712,749003310,2408682296,10,0,65160,0
0,4,5,52,0,0,64,80,2408682296,749003311,8,0,4117,0
0,4,5,413,0,0,64,80,2408682296,749003311,8,0,4117,0
0,4,5,52,59466,0,64,53712,749003311,2408682657,8,0,507,0
0,4,5,1500,59467,0,64,53712,749003311,2408682657,8,0,507,0
0,4,5,1500,59468,0,64,53712,749004759,2408682657,8,0,507,0
0,4,5,1500,59469,0,64,53712,749006207,2408682657,8,0,507,0
0,4,5,1500,59470,0,64,53712,749007655,2408682657,8,0,507,0
0,4,5,1500,59471,0,64,53712,749009103,2408682657,8,0,507,0
0,4,5,1500,59472,0,64,53712,749010551,2408682657,8,0,507,0
0,4,5,642,59473,0,64,53712,749011999,2408682657,8,0,507,0
0,4,5,52,0,0,64,80,2408682657,749004759,8,0,4072,0
0,4,5,52,0,0,64,80,2408682657,749009103,8,0,3960,0
0,4,5,52,0,0,64,80,2408682657,749009103,8,0,4096,0
0,4,5,52,0,0,64,80,2408682657,749012589,8,0,3987,0
0,4,5,52,0,0,64,80,2408682657,749012589,8,0,4096,0
0,4,5,52,27166,0,128,80,4130826672,0,8,0,64240,0
0,4,5,52,0,0,64,50923,5098763,4130826673,8,0,64240,0
0,4,5,52,27167,0,128,80,517479657,0,8,0,64240,0
0,4,5,52,0,0,64,49532,4139062060,517479658,8,0,64240,0
0,4,5,40,27168,0,128,80,4130826673,5098764,5,0,256,0
0,4,5,40,27169,0,128,80,517479658,4139062061,5,0,256,0
0,4,5,596,27170,0,128,80,517479658,4139062061,5,0,256,0
0,4,5,40,22635,0,64,49532,4139062061,517480214,5,0,501,0
0,4,5,245,22636,0,64,49532,4139062061,517480214,5,0,501,0
0,4,5,40,27171,0,128,80,517480214,4139062266,5,0,255,0
0,4,5,596,27172,0,128,80,517480214,4139062266,5,0,255,0

当我试图打印出y_hat 时,我只得到一个内容相同的数组。

[[0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]]
[[0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]
 [0.50000346]]

我的代码有什么问题?我希望你能帮助我!

【问题讨论】:

【参考方案1】:

如果您缩放特征矩阵,您的模型将按预期返回非常量预测:

import pandas as pd
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler

# import the data
dataset = pd.read_csv('database2.csv')
x = dataset.drop(columns=['good/bad']).values
y = dataset['good/bad'].values

# split the data
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)

# scale the data
scaler = StandardScaler().fit(x_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)

# fit the model
model = Sequential()
model.add(Dense(units=32, activation='relu', input_dim=(x_train.shape[1])))
model.add(Dense(units=64, activation='relu'))
model.add(Dense(units=1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=SGD(learning_rate=0.01), metrics=['accuracy'])
model.fit(x_train, y_train, epochs=100, batch_size=32, verbose=0)

# generate the model predictions
y_hat = model.predict(x_test)
print(y_hat)
# [0.6584388017654419, 0.6620162725448608, 0.32905155420303345, 0.2320745885372162, 0.5126726627349854, 0.7077205181121826, 0.46010926365852356, 0.6682235598564148, 0.6515036821365356, 0.22272580862045288, 0.6382402777671814]

# calculate the accuracy of the model predictions
print(accuracy_score(y_test, np.where(y_hat > 0.5, 1, 0)))
# 0.9090909090909091

【讨论】:

以上是关于Tensorflow 模型返回恒定预测 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

tensorflow 的数据集 API 返回的大小不是恒定的

Tensorflow 2 图像批量预测返回结果

TensorFlow:在一个新模型的培训中,将一个模型的预测结合起来的最简单方法是什么?

keras sequential 预测值为啥输出后半部分为恒定值

多输出回归模型始终为 Tensorflow 中的批次返回相同的值

Tensorflow.js 模型仅预测相同的值