深度学习笔记016:BatchNorm批量归一化+nn.LayerNorm暂记

本文详细介绍了如何在PyTorch中使用LayerNorm层,包括NLP和图像处理示例,并讨论了其在深度学习中的作用。特别提到了初始化问题及为何加入小数防止除零,还涵盖了全连接层与卷积层的区别。最后,给出了解决学习率调整建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现

pytorch 实现

Layernorm的官方文档

import torch
import torch.nn as nn

# NLP Example
batch, sentence_length, embedding_dim = 20, 5, 10
embedding = torch.randn(batch, sentence_length, embedding_dim)
layer_norm = nn.LayerNorm(embedding_dim)
# Activate module
layer_norm(embedding)
# Image Example
N, C, H, W = 20, 5, 10, 10
input = torch.randn(N, C, H, W)
# Normalize over the last three dimensions (i.e. the channel and spatial dimensions)
# as shown in the image below
layer_norm = nn.LayerNorm([C, H, W])
output = layer_norm(input)

在这里插入图片描述

self.feature_norm = nn.LayerNorm(self.ddim, elementwise_affine=False)

  File "***.py", line 9, in __init__
    self.feature_norm = nn.LayerNorm(self.ddim, elementwise_affine=False)
  File "***\anaconda3\envs\lshtorch\lib\site-packages\torch\nn\modules\normalization.py", line 171, in __init__
    self.normalized_shape = tuple(normalized_shape)  # type: ignore[arg-type]
TypeError: 'method' object is not iterable

Batch Norm

WARNING:root:NaN or Inf found in input tensor.

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
后 边 方 差 加 上 一 个 很 小 的 数 , 防 止 变 成 0 , 在 下 方 的 归 一 化 的 式 子 中 μ B 和 σ B 是 根 据 当 前 数 据 求 出 的 , γ 和 β 是 可 以 学 习 的 参 数 后边方差加上一个很小的数,防止变成0,在下方的归一化的式子中μ_B和σ_B是根据当前数据求出的,\\ γ和β是可以学习的参数 0,μBσBγβ

在这里插入图片描述

在这里插入图片描述
应 该 可 以 理 解 为 对 于 某 个 随 机 变 量 的 作 用 , 所 以 全 连 接 层 是 对 某 个 数 的 不 同 取 值 作 用 , 而 卷 积 层 是 对 某 个 像 素 的 通 道 作 用 应该可以理解为对于某个随机变量的作用,所以全连接层是对某个数的不同取值作用,\\而卷积层是对某个像素的通道作用 ,

在这里插入图片描述

后 续 有 人 研 究 发 现 没 有 改 变 内 部 协 变 量 后 边 的 研 究 可 能 也 是 不 对 的 后续有人研究发现没有改变 内部协变量\\后边的研究可能也是不对的
😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂😂

在这里插入图片描述
可 以 调 大 一 些 学 习 率 可以调大一些学习率

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值