机器学习/pytorch笔记:time2vec

1 概念部分

对于给定的标量时间概念 t,Time2Vec 的表示 t2v(t)是一个大小为 k+1的向量,定义如下:

  • 其中,t2v(t)[i]是 t2v(t)的第 i 个元素,F是一个周期性激活函数,ω和 ϕ是可学习的参数。

  • 以下是个人理解:

    • t是时间序列中的一个时间点,而不是时间序列的数值。
    • 具体来说,t 代表时间序列中的一个特定时刻,例如某一天、某一小时或某一秒等。Time2Vec 的目标是将每一个时间点 t 转换为一个具有特定特征的向量表示,以便更好地捕捉时间相关的特性和模式。

2 pytorch实现

2.1 函数t2v

python 复制代码
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)
  • t2v 负责将输入的时间 tau 通过两个不同的线性变换和激活函数转换成特征向量,并将这两个特征向量连接起来
    • tau 是输入的时间张量。
    • f 是激活函数(例如 torch.sin 或 torch.cos)。
    • out_features 是输出特征的维度。
    • w 和 b 是用于第一个变换的权重和偏置。
    • w0 和 b0 是用于第二个变换的权重和偏置。

2.2 SineActivation

python 复制代码
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)
  • 实现了使用正弦函数作为激活函数的时间嵌入
  • cos同理,把torch.sin换成torch.cos即可
  • 输入特征的维度(in_features)取决于提供的时间特征的数量
    • 如果你只有一个时间特征(例如,仅一天中的时间),那么输入特征的维度是 1。
    • 如果你有两个时间特征(例如,一天中的时间和一周中的某一天),那么输入特征的维度是 2。
    • 依此类推,输入特征的维度是你提供的时间特征的数量。
相关推荐
盼小辉丶4 分钟前
PyTorch实战——pix2pix详解与实现
pytorch·深度学习·生成模型
咚咚王者6 分钟前
人工智能之核心基础 机器学习 第七章 监督学习总结
人工智能·学习·机器学习
武子康16 分钟前
大数据-203 scikit-learn 决策树剪枝参数:max_depth/min_samples_leaf 到 min_impurity_decrease
大数据·后端·机器学习
斯外戈的小白19 分钟前
【NLP】Transformer在pytorch 的实现+情感分析案例+生成式任务案例
pytorch·自然语言处理·transformer
W|J24 分钟前
ES 学习笔记
笔记·学习·elasticsearch
张人玉25 分钟前
西门子 S7 PLC 通信 WPF 应用分析笔记
笔记·c#·wpf·plc
山土成旧客30 分钟前
【Python学习打卡-Day35】从黑盒到“玻璃盒”:掌握PyTorch模型可视化、进度条与推理
pytorch·python·学习
@zulnger31 分钟前
python 学习笔记(循环)
笔记·python·学习
Master_oid35 分钟前
机器学习28:增强式学习(Deep Reinforcement Learn)③
人工智能·学习·机器学习
Hammer_Hans37 分钟前
DFT笔记16
笔记