程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了keras SpatialDropout2D 在 TimeDistributed 层中的正确使用 - CNN LSTM 网络大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决keras SpatialDropout2D 在 TimeDiStributed 层中的正确使用 - CNN LSTM 网络?

开发过程中遇到keras SpatialDropout2D 在 TimeDiStributed 层中的正确使用 - CNN LSTM 网络的问题如何解决?下面主要结合日常开发的经验,给出你关于keras SpatialDropout2D 在 TimeDiStributed 层中的正确使用 - CNN LSTM 网络的解决方法建议,希望对你解决keras SpatialDropout2D 在 TimeDiStributed 层中的正确使用 - CNN LSTM 网络有所启发或帮助;

我在为时间序列样本中的所有时间步长应用相同的 dropout 掩码时遇到了一个棘手的问题,以便 LSTM 层在一次前向传递中看到相同的输入。我阅读了多篇文章,但没有找到解决方案。以下 implementation 是否支持这一点?或者这会在每个时间步随机丢弃不同的特征图?

dim = (420,48,1) # grayscale images of size 48x48
inputShape = (dim)
input_words = input(shape=inputShape,name='input_vID')
x = TimediStributed(Conv2D(filters=50,kernel_size=(8,8),padding='same',activation='relu'))(input_words)
x = TimediStributed(MaxPooling2D(pool_size=(2,2)))(X)
x = TimediStributed(SpatialDropout2D(0.2))(X)
x = TimediStributed(Batchnormalization())(X)
x = TimediStributed(Flatten())(X)
x = LSTM(200,dropout=0.2,recurrent_dropout=0.2)(X)
out = Dense(5,activation='softmax')(X)
model = Model(inputs=input_words,outputs=[out])
opt = Adam(lr=1e-3,decay=1e-3 / 200)
model.compile(loss = 'categorical_crossentropy',optimizer=opt,metrics = ['accuracy'])

如果不是,在 keras 上有什么好的解决方案?我可以使用 Dropout with noise_shape 来解决我的问题吗?

解决方法

您可以简单地自行测试所有可能性...

我们生成一个形状样本(1、n_frame、H、W、n_chAnnel)并可视化不同丢弃策略的影响:

inputShape = (100,8,1) # frames of 100 grayscale images of size 8x8 
X = np.random.uniform(1,2,(1,)+inputShapE).astype('float32') # generate 1 sample

layer = Dropout(0.4,seed=0)
d = layer(X,Training=TruE).numpy()

layer = Dropout(0.4,seed=0,noise_shape=(X.shape[0],1,X.shape[2],X.shape[3],X.shape[4]))
d1d = layer(X,Training=TruE).numpy()

layer = TimeDiStributed(SpatialDropout2D(0.4,seed=0))
tsd2d = layer(X,Training=TruE).numpy()

layer = SpatialDropout3D(0.4,seed=0)
# the same as:
# layer = Dropout(0.4,X.shape[4]))
sd3d = layer(X,Training=TruE).numpy()

来自Dropout的结果:

plt.figure(figsize=(15,12))

for i,f_map in enumerate(d[0]):
    
    if i == 12:
        break
    
    plt.subplot(3,4,i+1)
    plt.imshow(np.squeeze(f_map>0,-1),vmin=0,vmax=1)
    plt.title(f"frame {i}")

keras SpatialDropout2D 在 TimeDistributed 层中的正确使用 - CNN LSTM 网络

来自 Dropoutnoise_shape 的结果:

plt.figure(figsize=(15,f_map in enumerate(d1d[0]):
    
    if i == 12:
        break
    
    plt.subplot(3,vmax=1)
    plt.title(f"frame {i}")

keras SpatialDropout2D 在 TimeDistributed 层中的正确使用 - CNN LSTM 网络

来自 TimeDiStributedSpatialDropout2D 的结果

plt.figure(figsize=(15,f_map in enumerate(tsd2d[0]):
    
    if i == 12:
        break
    
    plt.subplot(3,vmax=1)
    plt.title(f"frame {i}")

keras SpatialDropout2D 在 TimeDistributed 层中的正确使用 - CNN LSTM 网络

来自SpatialDropout3D的结果:

plt.figure(figsize=(15,12))

for i,f_map in enumerate(sd3d[0]):
    
    if i == 12:
        break
    
    plt.subplot(3,vmax=1)
    plt.title(f"frame {i}")

keras SpatialDropout2D 在 TimeDistributed 层中的正确使用 - CNN LSTM 网络

结论

  • 简单的 Dropout 在没有规则的情况下随机丢弃每一帧中的像素
  • Dropout with noise_shape 随机丢弃每帧中始终在相同位置的像素
  • TimeDiStributedSpatialDropout2D 随机丢弃整帧
  • SpatialDropout3D 丢弃随机通道中的所有帧

大佬总结

以上是大佬教程为你收集整理的keras SpatialDropout2D 在 TimeDistributed 层中的正确使用 - CNN LSTM 网络全部内容,希望文章能够帮你解决keras SpatialDropout2D 在 TimeDistributed 层中的正确使用 - CNN LSTM 网络所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。