gpt4 book ai didi

machine-learning - pytorch中的层标准化?

转载 作者:行者123 更新时间:2023-11-30 09:58:58 30 4
gpt4 key购买 nike

x = torch.tensor([[1.5,0,0,0,0]]) 的层归一化不应该是 [[1.5,-0.5,-0.5 ,-0.5]] ?根据本文paper以及 pytorch doc 中的方程。但 torch.nn.LayerNorm 给出 [[ 1.7320, -0.5773, -0.5773, -0.5773]]

这里是示例代码:

x = torch.tensor([[1.5,.0,.0,.0]])
layerNorm = torch.nn.LayerNorm(4, elementwise_affine = False)

y1 = layerNorm(x)

mean = x.mean(-1, keepdim = True)
var = x.var(-1, keepdim = True)
y2 = (x-mean)/torch.sqrt(var+layerNorm.eps)

地点:

y1 == tensor([[ 1.7320, -0.5773, -0.5773, -0.5773]])
y2 == tensor([[ 1.5000, -0.5000, -0.5000, -0.5000]])

最佳答案

使用裸 PyTorch 的 Layer Norm 层的另一个简化实现。

from typing import Tuple
import torch


def layer_norm(
x: torch.Tensor, dim: Tuple[int], eps: float = 0.00001
) -> torch.Tensor:
mean = torch.mean(x, dim=dim, keepdim=True)
var = torch.square(x - mean).mean(dim=dim, keepdim=True)
return (x - mean) / torch.sqrt(var + eps)


def test_that_results_match() -> None:
dims = (1, 2)
X = torch.normal(0, 1, size=(3, 3, 3))

indices = torch.tensor(dims)
normalized_shape = torch.tensor(X.size()).index_select(0, indices)
orig_layer_norm = torch.nn.LayerNorm(normalized_shape)

y = orig_layer_norm(X)
y_hat = layer_norm(X, dim=dims)

assert torch.allclose(y, y_hat)

请注意,原始实现还具有可训练参数 𝛄β(请参阅 docs )。

关于machine-learning - pytorch中的层标准化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59830168/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com