前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Time2Vec 的理解与简单实现

Time2Vec 的理解与简单实现

作者头像
EmoryHuang
发布2022-10-31 17:41:36
9230
发布2022-10-31 17:41:36
举报
文章被收录于专栏:EmoryHuang's BlogEmoryHuang's Blog

Time2Vec 的理解与简单实现

前言

Time2Vec 从其名字就可以看出其功能,将时间进行 Embedding,并且能够应用于不同的模型。

2019 年的一篇论文:Time2Vec: Learning a Vector Representation of Time

Time2Vec

Time2Vec 的设计主要基于以下几个方面:

  1. 捕获周期性和非周期性模式
  2. 对时间缩放不变
  3. 易于与其他模型融合

Time2Vec 的公式并不复杂:

其中kkk为 time2vec 的维度,

为周期激活函数,

为可学习参数。为了使算法可以捕获周期性,所以F\mathcal{F}F选用sin⁡\sinsin函数(cos⁡\coscos函数同样效果)捕获周期性。

PyTorch 实现

代码语言:javascript
复制
def t2v(tau, f, out_features, w, b, w0, b0, arg=None):
    if arg:
        v1 = f(torch.matmul(tau, w) + b, arg)
    else:
        v1 = f(torch.matmul(tau, w) + b)
    v2 = torch.matmul(tau, w0) + b0
    return torch.cat([v1, v2], 1)


class SineActivation(nn.Module):
    def __init__(self, in_features, out_features):
        super(SineActivation, self).__init__()
        self.out_features = out_features
        self.w0 = nn.parameter.Parameter(torch.randn(in_features, 1))
        self.b0 = nn.parameter.Parameter(torch.randn(in_features, 1))
        self.w = nn.parameter.Parameter(torch.randn(in_features, out_features - 1))
        self.b = nn.parameter.Parameter(torch.randn(in_features, out_features - 1))
        self.f = torch.sin

    def forward(self, tau):
        return t2v(tau, self.f, self.out_features, self.w, self.b, self.w0, self.b0)


class CosineActivation(nn.Module):
    def __init__(self, in_features, out_features):
        super(CosineActivation, self).__init__()
        self.out_features = out_features
        self.w0 = nn.parameter.Parameter(torch.randn(in_features, 1))
        self.b0 = nn.parameter.Parameter(torch.randn(in_features, 1))
        self.w = nn.parameter.Parameter(torch.randn(in_features, out_features - 1))
        self.b = nn.parameter.Parameter(torch.randn(in_features, out_features - 1))
        self.f = torch.cos

    def forward(self, tau):
        return t2v(tau, self.f, self.out_features, self.w, self.b, self.w0, self.b0)


class Time2Vec(nn.Module):
    def __init__(self, activation, hiddem_dim):
        super(Time2Vec, self).__init__()
        if activation == "sin":
            self.l1 = SineActivation(1, hiddem_dim)
        elif activation == "cos":
            self.l1 = CosineActivation(1, hiddem_dim)

        self.fc1 = nn.Linear(hiddem_dim, 2)

    def forward(self, x):
        x = self.l1(x)
        x = self.fc1(x)
        return x

总结

对于时间的 Embedding 怎么说呢,个人感觉其实有必要又没必要,可有可无,当然不是说时间信息不重要。论文没有仔细看,当然主要是内容也比较少,感觉对于时间、位置这些东西的处理,到底还是 sin、cos 效果会好一点?看代码的时候又看见了作者的Date2Vec,模型没怎么看懂,具体也没解释原理,有兴趣的可以看看。

参考资料

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-07-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Time2Vec 的理解与简单实现
    • 前言
      • Time2Vec
        • PyTorch 实现
          • 总结
            • 参考资料
            相关产品与服务
            对象存储
            对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档