【问题标题】:Prediction gives me the same value预测给了我相同的价值
【发布时间】:2020-11-23 05:00:51
【问题描述】:

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

 # 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] 

【问题讨论】:

  • 我认为它来自您使用TimeDistributed 层的方式。这会将包裹层应用到使用相同权重的每个时间步。因此,在每个时间步都限制相同权重的同时实现最小 MSE 损失的唯一方法是针对每个时间步的总体平均值。
  • 你可以阅读这篇文章来帮助你相应地修改你的架构:machinelearningmastery.com/…

标签: python lstm conv-neural-network


【解决方案1】:

我查看了我的模型并进行了一些更改。

# 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?

【讨论】:

    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 2017-04-25
    • 2019-02-21
    • 2021-06-13
    • 2014-04-03
    相关资源
    最近更新 更多