Keras 函数[TimeDistributed]理解

本文深入解析了Keras中的TimeDistributed函数,通过实例展示了如何利用此函数进行批量降维操作,适用于处理时间序列数据,如将10个16维向量独立降至8维。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

例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官方文档

 

 

 

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值