gpt4 book ai didi

python - 预测给了我同样的值(value)

转载 作者:行者123 更新时间:2023-12-05 06:57:08 25 4
gpt4 key购买 nike

我正在尝试根据我的 cnn+lstm 模型的先前值预测下一个值,但我得到了每个预测的总体平均值。我的数据包含二十周内邻里(特征)的热图图像以及每周的犯罪数量(标签)。我尝试更改模型中的迭代次数、批量大小和参数数量。下面是我的模型。

 # MODEL
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense


def baseline_model():
#create model
model = Sequential()
model.add(
TimeDistributed(
Conv2D(16, (3, 3), strides=(2,2), data_format='channels_last', activation='relu'),
input_shape=(1,256, 256,3)# looking back 1 image

)
)

model.add(
TimeDistributed(
MaxPooling2D(pool_size=(2, 2))
)
)

model.add(
TimeDistributed(
Conv2D(16, (3, 3), activation='relu'),
)
)

model.add(
TimeDistributed(
MaxPooling2D(pool_size=(2, 2))
)
)


model.add(
TimeDistributed(
Conv2D(32, (3, 3),activation='relu'),
)
)

model.add(
TimeDistributed(
MaxPooling2D(pool_size=(2, 2))
)
)

model.add(
TimeDistributed(
Flatten()
)
)

model.add(
LSTM(4, return_sequences=True)
)

model.add(Dense(2, activation='relu'))
model.add(Flatten())
model.add(Dense((1), activation='linear'))


#Compile model
model.compile(loss='mean_squared_error', optimizer='adam')
return model




# evaluate model
estimator = KerasRegressor(build_fn=baseline_model, epochs=500, batch_size=1,verbose=0)
kfold = KFold(n_splits=10)
results = cross_val_score(estimator, X, y, cv=kfold)
print("Baseline: %.2f (%.2f) MSE" % (results.mean(), results.std()))
Baseline: -16.57 (19.04) MSE


estimator.fit(X, y)
prediction = estimator.predict(X)

print(y)
print(prediction)
[[ 4]
[ 7]
[ 7]
[ 6]
[13]
[11]
[10]
[ 4]
[11]
[10]
[ 6]
[ 7]
[ 2]
[17]
[14]
[ 9]
[ 8]
[ 8]
[ 4]
[ 8]]
[8.324332 8.324332 8.324332 8.324332 8.324332 8.324332 8.324332 8.324332
8.324332 8.324332 8.324332 8.324332 8.324332 8.324332 8.324332 8.324332
8.324332 8.324332 8.324332 8.324332]

最佳答案

我检查了我的模型并做了一些更改。

# MODEL
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense

def ReshapeLayer(x):
shape = x.shape

reshape = Reshape((shape[2],shape[3]*shape[4]))(x)

return reshape



def baseline_model():
#create model
model = Sequential()
model.add(
TimeDistributed(
Conv2D(16, (3, 3), strides=(2,2), data_format='channels_last', activation='relu')
,input_shape=(1,256, 256,3)
)

)

model.add(
TimeDistributed(
MaxPooling2D(pool_size=(2, 2))
)
)

model.add(
TimeDistributed(
Conv2D(16, (3, 3), activation='relu'),
)
)

model.add(
TimeDistributed(
MaxPooling2D(pool_size=(2, 2))
)
)


model.add(
TimeDistributed(
Conv2D(32, (3, 3),activation='relu'),
)
)

model.add(
TimeDistributed(
MaxPooling2D(pool_size=(2, 2))
)
)

model.add(
Lambda(ReshapeLayer)
)


model.add(
LSTM(20, activation='relu', return_sequences=True)
)

model.add(
Dense((2), activation='relu')
)
model.add(
Flatten()

)

model.add(

Dense((1))

)




#Compile model
model.compile(loss='mean_squared_error', optimizer='adam')
return model

模型总结:

Model: "sequential_58"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
time_distributed_343 (TimeDi (None, 1, 127, 127, 16) 448
_________________________________________________________________
time_distributed_344 (TimeDi (None, 1, 63, 63, 16) 0
_________________________________________________________________
time_distributed_345 (TimeDi (None, 1, 61, 61, 16) 2320
_________________________________________________________________
time_distributed_346 (TimeDi (None, 1, 30, 30, 16) 0
_________________________________________________________________
time_distributed_347 (TimeDi (None, 1, 28, 28, 32) 4640
_________________________________________________________________
time_distributed_348 (TimeDi (None, 1, 14, 14, 32) 0
_________________________________________________________________
lambda_58 (Lambda) (None, 14, 448) 0
_________________________________________________________________
lstm_58 (LSTM) (None, 14, 20) 37520
_________________________________________________________________
dense_115 (Dense) (None, 14, 2) 42
_________________________________________________________________
flatten_58 (Flatten) (None, 28) 0
_________________________________________________________________
dense_116 (Dense) (None, 1) 29
=================================================================
Total params: 44,999
Trainable params: 44,999
Non-trainable params: 0
_________________________________________________________________

当我试图预测温哥华市中心的犯罪数量时,我能够得到我想要的结果。

 # evaluate model
estimator KerasRegressor(build_fn=baseline_model,epochs=500,batch_size=1,verbose=0)
kfold = KFold(n_splits=10)
results = cross_val_score(estimator, X, y, cv=kfold)
print("Baseline: %.2f (%.2f) MSE" % (results.mean(), results.std()))

基线:-3838.29 (10400.71) MSE

# evaluate
estimator.fit(X, y)
prediction = estimator.predict(X)

print(y)
print(prediction)
[[200]
[189]
[224]
[170]
[195]
[197]
[236]
[156]
[203]
[218]
[215]
[240]
[175]
[223]
[239]
[222]
[174]
[207]
[201]
[200]]
[199.85223 188.7917 223.93802 169.9083 194.99187 196.86598 235.94765
155.94873 202.9606 217.96512 214.86911 240.00726 175.0241 223.01225
238.89543 221.8833 173.89732 206.95938 200.80322 199.88109]

但是,每当我尝试预测 Fairview Vancouver 的犯罪率时,每次预测都会得到相同的值。

# evaluate model
estimator = KerasRegressor(build_fn=baseline_model, epochs=500, batch_size=1,verbose=0)
kfold = KFold(n_splits=10)
results = cross_val_score(estimator, X, y, cv=kfold)
print("Baseline: %.2f (%.2f) MSE" % (results.mean(), results.std()))

基线:-782.18 (735.71) MSE

# evaluate
estimator.fit(X, y)
prediction = estimator.predict(X)

print(y)
print(prediction)

[[39]
[40]
[36]
[29]
[44]
[49]
[35]
[29]
[49]
[55]
[40]
[57]
[38]
[39]
[38]
[37]
[24]
[53]
[32]
[43]]
[9.494502 9.494502 9.494502 9.494502 9.494502 9.494502 9.494502 9.494502
9.494502 9.494502 9.494502 9.494502 9.494502 9.494502 9.494502 9.494502
9.494502 9.494502 9.494502 9.494502]

我不知道为什么它会为每个预测提供相同的值,而当 Downtown 的预测为我提供不同的值时,即使我对两个预测使用相同的模型。我的预测值是否完全相同是否取决于MSE?

关于python - 预测给了我同样的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64962783/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com