【问题标题】:Understanding the 'axis' parameter in the softmax activation function理解 softmax 激活函数中的 'axis' 参数
【发布时间】:2021-03-06 22:46:02
【问题描述】:

假设我有一个输入张量,每个时间步带有一个嵌入词,例如,对于 5 的时间窗口和 64 的词嵌入向量宽度,我得到了形状:

(None, 5, 64, 1)

我应用 4 个内核形状为 (1, 64) 的过滤器在每个时间步寻找特定的词,每个过滤器在每个时间步产生 1 个值,表示“词/含义存在”或“词/含义不存在” .它产生一个形状的输出张量:

(None, 5, 1, 4)

我如何定义 softmax 层的“轴”参数,以使 每个时间步 的所有卷积的输出都被归一化,就像在分类任务中一样?

更具体地说,我希望输出如下所示(高度是时间,宽度是通道):

[[[.1, .4, .4, .1]]
 [[.9,  0,  0, .1]]
 [[.8,  0, .1, .1]]
 [[ 0,  1,  0,  0]]
 [[.6,. 1, .1, .2]]]

即每行/时间步长的分量加起来为一,softmax 应该只对行进行归一化。

代码sn-p:

model.add(layers.Conv2D(
    filters=words_of_interest,
    kernel_size=(1, embedding_length),
    strides=(1, embedding_length),
    padding="same")
)
model.add(layers.Softmax(axis=3)) # <- is this correct for what i described above?

【问题讨论】:

    标签: python tensorflow keras tensor activation


    【解决方案1】:

    对于轴的所有值都可以。 Tensorflow 以相同的方式对数组的值进行归一化。在这里,您可以使用以下代码检查 tensornormalized 之间的区别。

    import numpy as np
    import tensorflow as tf
    
    array = np.random.random((5, 4))
    tensor = tf.convert_to_tensor(array)
    
    norm0 = tf.keras.activations.softmax(tensor, axis=0)
    norm1 = tf.keras.activations.softmax(tensor, axis=1)
    norm2 = tf.keras.activations.softmax(tensor, axis=2)
    norm3 = tf.keras.activations.softmax(tensor, axis=3)
    
    print(sum(tensor[0]))
    print(sum[norm0[0]])
    print(sum[norm1[0]])
    print(sum[norm2[0]])
    print(sum[norm3[0]])
    

    【讨论】:

      猜你喜欢
      • 2018-06-27
      • 2021-03-23
      • 1970-01-01
      • 2021-03-19
      • 2012-04-11
      • 2023-01-27
      • 1970-01-01
      • 2018-03-24
      • 2020-09-14
      相关资源
      最近更新 更多