[英]Prediction gives me the same value
我试图根据我的 cnn+lstm 模型的前一个值预测下一个值,但我得到了每个预测的总体平均值。 我的数据包括一个街区(特征)的热图图像,其中包含每周的犯罪数量(标签)和 20 周。 我尝试更改时代数、更改批次大小和更改模型中的参数数量。 下面是我的模型。
# 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]
我不知道为什么当市中心的预测给我不同的值时,它会为每个预测提供相同的值,即使我对两个预测使用相同的模型。 我的预测值是否完全相同是否取决于 MSE?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.