Keras 函数[TimeDistributed]理解

4 篇文章 0 订阅
1 篇文章 0 订阅

这个函数刚接触时不太好理解,其实结合几个例子来理解就容易多了.

例1:

Keras 官网文档关于该函数的例子,该文档说:

This wrapper applies a layer to every temporal slice of an input.

The input should be at least 3D, and the dimension of index one will be considered to be the temporal dimension.

You can then use TimeDistributed to apply a Dense layer to each of the 10 timesteps, independently:

The output will then have shape (32, 10, 8).

# as the first layer in a model
model = Sequential()
model.add(TimeDistributed(Dense(8), input_shape=(10, 16)))
# now model.output_shape == (None, 10, 8)

其如上所示,一个(32, 10, 16)的张量,不考虑第一个批次的话,也就是从第一个维度开始,就是(10,16),好比于10个16维的向量,现在要将这10个向量从16维同时降维到8维,这时候在Keras中就可以使用[TimeDistributed]函数,如上述代码,其参数就是Dense(8),input_shape. 这样就实现了张量的批量将维.

例2:

当然,除了Dense,也可以是其他的操作,比如Flatten:

x = TimeDistributed(Flatten(), name='flatten')(x)

则,如下图所示,输入为(None,None,4,192),由于以第一个维度(从零开始数)的None为时间片,批量地将2,3维排成一维,即变成(None, None,768):

 

参考:

Keras官方文档

 

 

 

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值