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}")

enter image description here

来自 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}")

enter image description here

来自 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}")

enter image description here

来自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}")

enter image description here

结论

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?