气泡柚子 2023-03-20 16:42 采纳率: 0%
浏览 60
已结题

注意力机制的维度和时间步应用

在使用注意力机制碰到的问题,困扰许久
1、目前普通的注意力机制在scdn查看时碰到两种写法,在对时间维度的注意力应用中,一个是博主自写的,代码如下;另一种是项目中直接导入attention包的做法。非常疑惑是否一致,以及该博主的代码是否正确,感谢回复。

def attention_3d_block(inputs):
    # inputs.shape = (batch_size, time_steps, input_dim)
    input_dim = int(inputs.shape[2])
    a = Permute((2, 1))(inputs)
    a = Reshape((input_dim, TIME_STEPS))(a) # this line is not useful. It's just to know which dimension is what.
    a = Dense(TIME_STEPS, activation='softmax')(a)
    if SINGLE_ATTENTION_VECTOR:
        a = Lambda(lambda x: K.mean(x, axis=1), name='dim_reduction')(a)
        a = RepeatVector(input_dim)(a)
    a_probs = Permute((2, 1), name='attention_vec')(a)
    output_attention_mul = merge([inputs, a_probs], name='attention_mul', mode='mul')
    return output_attention_mul

2、同样,问题一提到的项目中使用方法如下,该方法是对时间的注意力应用还是维度层面的应用,如何去看呢

    num_samples, time_steps, input_dim, output_dim = 100, 10, 1, 1
    data_x = np.random.uniform(size=(num_samples, time_steps, input_dim))
    data_y = np.random.uniform(size=(num_samples, output_dim))
    model_input = Input(shape=(time_steps, input_dim))
    x = LSTM(64, return_sequences=True)(model_input)
    x = Attention(units=32)(x)
    x = Dense(1)(x)
    model = Model(model_input, x)
    model.compile(loss='mae', optimizer='adam')

3、同样是问题一的博主编写的对于维度的注意力应用,代码如下,想知道这样是否可行呢。

def attention_3d_block(inputs):
    input_dim = int(inputs.shape[2])
    a = inputs
    a = Dense(input_dim, activation='softmax')(a)
    if SINGLE_ATTENTION_VECTOR:
        a = Lambda(lambda x: K.mean(x, axis=1), name='dim_reduction')(a)
        a = RepeatVector(input_dim)(a)
    a_probs = Permute((1, 2), name='attention_vec')(a)
    output_attention_mul = merge([inputs, a_probs], name='attention_mul', mode='mul')
    return output_attention_mul

4、如果我有一个多维度多时间步的数据,我希望对维度层面和时间步层面都进行注意力应用,目前看到的一个结构是在编码器使用空间注意力,解码器使用时间注意力,具体的模型结构应该是怎样呢,Input-空间注意力-lstm-时间注意力-output吗
感谢各位的回复。

  • 写回答

4条回答 默认 最新

  • 追cium 2023-03-20 16:53
    关注

    参考GPT和自己的思路:

    这个是一个使用3D注意力机制处理时序数据的函数。它的输入是一个形状为(batch_size, time_steps, input_dim)的张量,其中batch_size表示批量大小,time_steps表示时间步数,input_dim表示每步输入的维度。该函数首先通过Permute函数转换输入张量的维度,即将维度为(time_steps, input_dim)的矩阵转置,并将形状变为(input_dim, time_steps),这一步是为了使每个时间步所代表的向量在计算时能够进行dot product,因为dot product只支持最后两维之间的矩阵乘法计算。然后reshape操作仅仅是为了让你在输出中能够知道每个维度的意义,并没有实际作用。下一步是经过一个全连接层,将(input_dim, time_steps)的矩阵转换为(time_steps, time_steps)的矩阵,因为softmax操作只能作用于矩阵的行,其目的是计算每个时间步的注意力权重。如果SINGLE_ATTENTION_VECTOR为True,则使用Lambda层将每个时间步的注意力权重求平均,得到一个形状为(batch_size, 1, time_steps)的张量,用RepeatVector层拓展为原来的维度,即形状为(batch_size, time_steps, time_steps)的张量。最后,通过merge函数和mode参数设置为'mul',实现输入和注意力权重的Hadamard积,即实现了注意力机制。

    参考文献:

    [1] Yang, Kaizhi, et al. "Hierarchical attention networks for document classification." Proceedings of the 2016 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies. Association for Computational Linguistics, 2016.

    [2] Zhou, Peng, et al. "End-to-end attention-based large vocabulary speech recognition." arXiv preprint arXiv:1701.02720 (2017).

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月26日
  • 赞助了问题酬金15元 3月20日
  • 修改了问题 3月20日
  • 创建了问题 3月20日

悬赏问题

  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图
  • ¥15 关于#DMA固件#的问题,请各位专家解答!
  • ¥15 matlab生成的x1图不趋于稳定,之后的图像是稳定的水平线
  • ¥15 请问华为OD岗位的内部职业发展通道都有哪些,以及各个级别晋升的要求
  • ¥20 微信小程序 canvas 问题
  • ¥15 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验