使用哪个轴来对三维数据进行归一化?

huangapple go评论87阅读模式
英文:

Which axis to use to normalise 3 dimensional data?

问题

我想知道在三维数据上进行归一化时,最好使用哪个轴(默认是最后一个轴)。例如,我有一个形状为 3 x 4 x 5批量大小 x 元素数量 x 特征数量)的张量。在这种情况下,我应该使用轴 1 来对每个特征进行归一化,还是使用默认的轴 -1(2)?:

  1. data = tf.constant(np.arange(3 * 4 * 5).reshape(3, 4, 5) * 10, dtype=tf.float32)
  2. print(data)
  3. tf.Tensor(
  4. [[[ 0. 10. 20. 30. 40.]
  5. [ 50. 60. 70. 80. 90.]
  6. [100. 110. 120. 130. 140.]
  7. [150. 160. 170. 180. 190.]]
  8. [[200. 210. 220. 230. 240.]
  9. [250. 260. 270. 280. 290.]
  10. [300. 310. 320. 330. 340.]
  11. [350. 360. 370. 380. 390.]]
  12. [[400. 410. 420. 430. 440.]
  13. [450. 460. 470. 480. 490.]
  14. [500. 510. 520. 530. 540.]
  15. [550. 560. 570. 580. 590.]]], shape=(3, 4, 5), dtype=float32)
  16. layer = tf.keras.layers.LayerNormalization(axis=1)
  17. output = layer(data)
  18. print(output)
  19. tf.Tensor(
  20. [[[-1.3416405 -1.3416405 -1.3416406 -1.3416405 -1.3416405 ]
  21. [-0.44721347 -0.44721353 -0.44721353 -0.44721353 -0.4472134 ]
  22. [ 0.44721353 0.44721353 0.4472134 0.44721353 0.44721365]
  23. [ 1.3416405 1.3416405 1.3416405 1.3416406 1.3416407 ]]
  24. [[-1.3416407 -1.3416407 -1.3416407 -1.3416405 -1.3416405 ]
  25. [-0.44721365 -0.44721365 -0.44721365 -0.44721317 -0.44721317]
  26. [ 0.44721317 0.44721317 0.44721317 0.44721365 0.44721365]
  27. [ 1.3416405 1.3416405 1.3416405 1.341641 1.3416405 ]]
  28. [[-1.341641 -1.341641 -1.341641 -1.341641 -1.34164 ]
  29. [-0.44721413 -0.44721413 -0.44721413 -0.44721413 -0.44721317]
  30. [ 0.44721317 0.44721317 0.44721317 0.44721317 0.44721413]
  31. [ 1.3416405 1.3416405 1.3416405 1.3416405 1.3416414 ]]], shape=(3, 4, 5), dtype=float32)
英文:

I want to know which axis is better to use for normalisation for a 3 dimensional data (it is the last axis by default). For example, I have this tensor with the shape 3 x 4 x 5 (batch_size x number_of_elements x number_of_features). Should I use axis 1 in this case so that I normalise each feature or the default axis -1 (2)?:

  1. data = tf.constant(np.arange(3 * 4 * 5).reshape(3, 4, 5) * 10, dtype=tf.float32)
  2. print(data)
  3. tf.Tensor(
  4. [[[ 0. 10. 20. 30. 40.]
  5. [ 50. 60. 70. 80. 90.]
  6. [100. 110. 120. 130. 140.]
  7. [150. 160. 170. 180. 190.]]
  8. [[200. 210. 220. 230. 240.]
  9. [250. 260. 270. 280. 290.]
  10. [300. 310. 320. 330. 340.]
  11. [350. 360. 370. 380. 390.]]
  12. [[400. 410. 420. 430. 440.]
  13. [450. 460. 470. 480. 490.]
  14. [500. 510. 520. 530. 540.]
  15. [550. 560. 570. 580. 590.]]], shape=(3, 4, 5), dtype=float32)
  16. layer = tf.keras.layers.LayerNormalization(axis=1)
  17. output = layer(data)
  18. print(output)
  19. tf.Tensor(
  20. [[[-1.3416405 -1.3416405 -1.3416406 -1.3416405 -1.3416405 ]
  21. [-0.44721347 -0.44721353 -0.44721353 -0.44721353 -0.4472134 ]
  22. [ 0.44721353 0.44721353 0.4472134 0.44721353 0.44721365]
  23. [ 1.3416405 1.3416405 1.3416405 1.3416406 1.3416407 ]]
  24. [[-1.3416407 -1.3416407 -1.3416407 -1.3416405 -1.3416405 ]
  25. [-0.44721365 -0.44721365 -0.44721365 -0.44721317 -0.44721317]
  26. [ 0.44721317 0.44721317 0.44721317 0.44721365 0.44721365]
  27. [ 1.3416405 1.3416405 1.3416405 1.341641 1.3416405 ]]
  28. [[-1.341641 -1.341641 -1.341641 -1.341641 -1.34164 ]
  29. [-0.44721413 -0.44721413 -0.44721413 -0.44721413 -0.44721317]
  30. [ 0.44721317 0.44721317 0.44721317 0.44721317 0.44721413]
  31. [ 1.3416405 1.3416405 1.3416405 1.3416405 1.3416414 ]]], shape=(3, 4, 5), dtype=float32)

答案1

得分: 2

层归一化是在特征轴/轴上执行的。我不确定你的 axis=1 是什么意思,但由于你有多维数据,我假设这是一个(空间?)特征。所以在你的情况下,你应该使用将列表传递给函数的选项

  1. tf.keras.layers.LayerNormalization(axis=[1, 2])

一些背景信息可以在这里找到,其中提到:

> [...] 假设我们有一个表示(B,H,W,C)的4D张量,表示图像的批次、高度、宽度和通道。

> 批次归一化 → 对通道(1,1,1,c)求均值和方差

> 层归一化 → 对批次(b,1,1,1)求均值和方差

> 实例归一化 → 对批次/通道(b,1,1,c)求均值和方差

英文:

Layer Normalization is performed across feature axis/axes. I am not sure what your axis=1 is, but, since you have multidimensional data, I assume this is a (spatial?) feature. So in your case you should use the option to pass a list to the function

  1. tf.keras.layers.LayerNormalization(axis=[1, 2])

some background can be found here where it says:

> [...] Assuming we have 4D tensor that represents (B,H,W,C), which
> stands for Batch, Height, Width, and Channel of image.

> Batch Norm → Take mean and variance respect to channel (1,1,1,c)

> Layer Norm → Take mean and variance respect to batch (b,1,1,1)

> Instance Norm → Take mean and variance respect to batch/channel (b,1,1,c)

huangapple
  • 本文由 发表于 2023年7月12日 23:29:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定