看模型结构 分析模型结构

from transformers import ForImageClassification

model = ForImageClassification.from_pretrained(

""

)

print(model)

打印模型结构

Some weights of ForImageClassification were not initialized from the model checkpoint at /liujiangli-dataand are newly initialized: ['classifier.bias', 'classifier.weight']

You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.

意思就是需要自己训练分类头

ForImageClassification(

(vision_model):VisionTransformer(

这部分负责将图像转化为一系列的token

(embeddings): VisionEmbeddings(

(patch_embedding): Conv2d(3, 768, kernel_size=(16, 16), stride=(16, 16), padding=valid)

  • 用 3×3 RGB 输入,卷积核大小 16、步幅 16,(卷积核大小和步长大小相同)相当于把图像按照 16×16 的不重叠 patch 切分并对每个 patch 做线性投影(这是很多 ViT 实现常用的 trick,用 conv 实现 patch embedding)。

  • 输出通道数 768 表示每个 patch 被投影到 768 维特征向量。

  • 若输入是 224×224,输出的 patch 网格大小是 14×14,总 patch 数 196(14×14)。

  • 数据形状变化(假设 batch size = B):

    • 输入: (B, 3, 224, 224)

    • conv 输出: (B, 768, 14, 14)

    • flatten/transpose 后: (B, 196, 768) 或常见的 (B, 768, 196) 根据实现差别(你的打印显示 position embedding 是 Embedding(196,768),所以 tokens 数=196)。

(position_embedding): Embedding(196, 768)

)

  • 为每个 patch token 加上可学习的位置编码(长度 196,每个位置 768 维)。

  • 这样模型能区分不同 patch 的空间位置。

包括embeddings 包括patch embedding 和 position embedding

接下来Transformer 的堆叠编码器,负责在所有 patch token 间建模全局交互。

(encoder): Encoder(

(layers): ModuleList(

有12层这样的堆叠在一起 一共 12 层 encoder layer(典型 ViT-Base 的层数)。每一层都包括以下几个部分

(0-11): 12 x EncoderLayer(

(layer_norm1): LayerNorm((768,), eps=1e-06, elementwise_affine=True)

(self_attn): Attention(

(k_proj): Linear(in_features=768, out_features=768, bias=True)

(v_proj): Linear(in_features=768, out_features=768, bias=True)

(q_proj): Linear(in_features=768, out_features=768, bias=True)

(out_proj): Linear(in_features=768, out_features=768, bias=True)

)

(layer_norm2): LayerNorm((768,), eps=1e-06, elementwise_affine=True)

(mlp): MLP(

(activation_fn): GELUTanh()

(fc1): Linear(in_features=768, out_features=3072, bias=True)

(fc2): Linear(in_features=3072, out_features=768, bias=True)

)

)

)

)

EncoderLayer
  1. layer_norm1: LayerNorm((768,), eps=1e-06, elementwise_affine=True)

    • 对 token 的 768 维做 Layer Normalization(归一化),eps 是数值稳定项,elementwise_affine=True 意味着有可学习的缩放和平移参数(γ, β)。
  2. self_attn: Attention

    • Transformer 的自注意力子层,包含 Q/K/V 的投影和输出投影:

      • q_proj, k_proj, v_proj: Linear(in_features=768, out_features=768, bias=True)

        • 把 768 维输入线性变换成 Q、K、V,维度仍是 768。通常会在内部 reshape 为 (num_heads, head_dim) 形式进行多头计算。

        • 虽然打印没有给出 num_heads,但常见配置是 12 头(768 / 12 = 64 head_dim)。这是个合理假设,但具体头数要看实现。

      • out_proj: Linear(768 -> 768):把多头注意力拼回 768 维。

    • 自注意力作用:每个 patch token 会基于所有 token 的相互注意力加权组合信息,捕捉全局依赖和上下文。

  3. layer_norm2: LayerNorm((768,), eps=1e-06, elementwise_affine=True)

    • 注意力后的第二个 LayerNorm(用于残差连接后的规范化 / 稳定训练)。
  4. mlp: MLP

    • 前馈网络(通常称为 MLP 或 FFN),结构如下:

      • fc1: Linear(in_features=768, out_features=3072, bias=True):扩展维度到 3072(通常是 4× hidden dim)。

      • activation_fn: GELUTanh():这是一个自定义激活,名字暗示可能是结合了 GELU 与 tanh 的形式,或是某种带有 tanh 缓和的 GELU 风格激活(不是标准的,但用途类似于 GELU 提供非线性)。作用是引入非线性并帮助模型表达复杂变换。

      • fc2: Linear(in_features=3072, out_features=768, bias=True):把维度投回 768。

    • MLP 的作用是对每个 token 进行位置无关的逐 token 非线性变换(增强表示能力)。

每个 encoder layer 常见的数据流是:x = x + SelfAttn(LN(x))x = x + MLP(LN(x))(即 Pre-LN 或 Post-LN 变体,这里看起来是 pre-LN,因为 LayerNorm 在子层前面)。

(post_layernorm): LayerNorm((768,), eps=1e-06, elementwise_affine=True)

  • 在全部 encoder layer 堆叠之后再做一次 LayerNorm,输出一个稳定的 token 表示序列。

(head): MultiheadAttentionPoolingHead(

(attention): MultiheadAttention(

(out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True)

)

(layernorm): LayerNorm((768,), eps=1e-06, elementwise_affine=True)

(mlp): MLP(

(activation_fn): GELUTanh()

(fc1): Linear(in_features=768, out_features=3072, bias=True)

(fc2): Linear(in_features=3072, out_features=768, bias=True)

)

)

)

这个 head 不是简单的 global average 或 class token,而是用 attention pooling(多头注意力池化)把 token 序列聚合为一个全局表示。

  • (attention): MultiheadAttention( (out_proj): NonDynamicallyQuantizableLinear(in_features=768, out_features=768, bias=True) )

    • MultiheadAttention 作为 pooling 操作:通常做法是引入一个 learnable query(或使用一个特殊的 class token),通过 query 对所有 token 做 attention,加权得到一个聚合向量。

    • out_proj 把多头拼回 768 维。NonDynamicallyQuantizableLinear 是 PyTorch 的一种线性层实现细节(与量化相关的实现差异),对功能没有影响。

    • 这一步的结果是把 (B, N, 768) 的 token 序列压缩为 (B, 1, 768)(B, 768) 的全局向量(取决于实现)。

  • (layernorm): LayerNorm((768,), eps=1e-06, elementwise_affine=True)

    • 对聚合向量做规范化。
  • (mlp): MLP( fc1: 768->3072, activation GELUTanh, fc2: 3072->768 )

    • 对聚合向量再做一次 MLP 变换以增强判别能力(类似 encoder 内部的 MLP,但只作用于 pooled vector)。

总结:head 把所有 patch token 的信息用注意力方式加权整合成单个语义向量(比简单平均更灵活,因为 attention 可以学习关注重要 patch)。

(classifier): Linear(in_features=768, out_features=2, bias=True)

)

看模型结构 可以决定冻结哪些 微调的数据是原本模型 感觉还是适合全参数微调 把这个前面的vision model和后面的分类头一起训练

相关推荐
禁默8 分钟前
【硬核入门】无需板卡也能造 AI 算子?深度玩转 CANN ops-math 通用数学库
人工智能·aigc·cann
程序员清洒8 分钟前
CANN模型剪枝:从敏感度感知到硬件稀疏加速的全链路压缩实战
算法·机器学习·剪枝
敏叔V58713 分钟前
AI智能体的工具学习进阶:零样本API理解与调用
人工智能·学习
徐小夕@趣谈前端22 分钟前
拒绝重复造轮子?我们偏偏花365天,用Vue3写了款AI协同的Word编辑器
人工智能·编辑器·word
阿里云大数据AI技术22 分钟前
全模态、多引擎、一体化,阿里云DLF3.0构建Data+AI驱动的智能湖仓平台
人工智能·阿里云·云计算
陈天伟教授23 分钟前
人工智能应用- 语言理解:05.大语言模型
人工智能·语言模型·自然语言处理
池央24 分钟前
CANN GE 深度解析:图编译器的核心优化策略、执行流调度与模型下沉技术原理
人工智能·ci/cd·自动化
七月稻草人27 分钟前
CANN ops-nn:AIGC底层神经网络算力的核心优化引擎
人工智能·神经网络·aigc·cann
种时光的人27 分钟前
CANN仓库核心解读:ops-nn打造AIGC模型的神经网络算子核心支撑
人工智能·神经网络·aigc
晚霞的不甘29 分钟前
守护智能边界:CANN 的 AI 安全机制深度解析
人工智能·安全·语言模型·自然语言处理·前端框架