当前位置:网站首页>Time2Vec 的理解与简单实现
Time2Vec 的理解与简单实现
2022-07-23 16:38:00 【EmoryHuang】
Time2Vec 的理解与简单实现
前言
Time2Vec 从其名字就可以看出其功能,将时间进行 Embedding,并且能够应用于不同的模型。
2019 年的一篇论文:Time2Vec: Learning a Vector Representation of Time
Time2Vec
Time2Vec 的设计主要基于以下几个方面:
- 捕获周期性和非周期性模式
- 对时间缩放不变
- 易于与其他模型融合
Time2Vec 的公式并不复杂:
t 2 v ( τ ) [ i ] = { ω i τ + φ i , if i = 0. F ( ω i τ + φ i ) , if 1 ≤ i ≤ k . \mathbf{t2v}(\tau)[i]=\begin{cases}\omega_i\tau+\varphi_i, &\text{if }i=0. \\ \mathcal{F}(\omega_i\tau+\varphi_i), &\text{if }1\leq i\leq k. \end{cases} t2v(τ)[i]={ ωiτ+φi,F(ωiτ+φi),if i=0.if 1≤i≤k.
其中 k k k为 time2vec 的维度, F \mathcal{F} F为周期激活函数, ω i , φ i \omega_i,\varphi_i ωi,φi为可学习参数。为了使算法可以捕获周期性,所以 F \mathcal{F} F选用 sin \sin sin函数( cos \cos cos函数同样效果)捕获周期性。
PyTorch 实现
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,模型没怎么看懂,具体也没解释原理,有兴趣的可以看看。
参考资料
边栏推荐
- MySQL performance tuning
- 【游戏建模模型制作全流程】ZBrush武器模型制作:弩
- jumpserver管理员账号被锁定
- Alliance DAO创始人:100+Web3基础设施及Dapp创业清单
- Handwriting bind, call, apply is actually very simple
- Is 3D modeling promising? Is employment guaranteed with high salary or is it more profitable to take orders in sideline industry
- DB9 serial port and RJ45 serial port
- My creation anniversary
- [2020] [paper notes] new terahertz detection - Introduction to terahertz characteristics, various terahertz detectors
- How to evaluate the accuracy of stock analysts' prediction?
猜你喜欢

As a senior 3D modeler, I give some suggestions to novice learning partners to use

日志框架【详解学习】

【2022】【论文笔记】太赫兹量子阱——

【2020】【论文笔记】基于二维光子晶体的光控分光比可调Y——

Building virtual private network based on softther

Have a safe summer vacation, no holidays! Please keep these summer safety tips
![[2018] [paper notes] graphene FET and [1] - Types and principles of gfets, characteristics of gfets, applications and principles of gfets in terahertz](/img/df/bc757c7f6f6b801fafdd5a99352ddd.png)
[2018] [paper notes] graphene FET and [1] - Types and principles of gfets, characteristics of gfets, applications and principles of gfets in terahertz

VS2010一个解决方案下新建多个项目出现的问题和方法

1259. Disjoint handshake dynamic programming

What is the current situation of the next generation industry? 90% of career changing modelers are learning this process
随机推荐
基于 softether 搭建虚拟私有网络
?前台传参的问题待确认
398. Random number index hash table method
PCL:多直线拟合(RANSAC)
Modeling at the beginning of learning is very confused, how to learn next generation role modeling?
并非原创的原文路径【如有侵权 请原博主联系删除】
How to become a modeler? Which is more popular, industrial modeling or game modeling?
As a senior 3D modeler, I give some suggestions to novice learning partners to use
Detailed explanation: tmp1750 chip three channel linear LED driver
[heavyweight] focusing on the terminal business of securities companies, Borui data released a new generation of observable platform for the core business experience of securities companies' terminals
Integer and = = compare
What problems do you usually encounter when learning 3D modeling? It's too much
Redis [super superfine introductory tutorial]
[the whole process of Game Modeling and model production] create the game soldier character with ZBrush
Flutter 运行模式
ros(27):rosparam简单使用与一种通过launch传递参数不成功与解决
Google is improving the skin color performance in all products and practicing the concept of "image fairness"
Learn about spark project on nebulagraph
What is the current situation of the next generation industry? 90% of career changing modelers are learning this process
How does Apache, the world's largest open source foundation, work?