【transformer】【ViT】【code】ViT代码

桃叶儿尖上尖,柳絮儿飞满了天…

1 导入库
import torch
from torch import nn, einsum
import torch.nn.functional as F

from einops import rearrange, repeat
from einops.layers.torch import Rearrange

解释:其中einops库用于张量操作,增强代码的可读性,使用还是比较方便的。教程链接:

einops基础教程
einsum讲解

2 调用
if __name__=="__main__":
    net = ViT(image_size=256, 
        patch_size=32,#pathces的尺寸
        num_classes=1000,
        dim=1024, #embddings的长度,也就是每个block的输入输出的尺寸
        depth=6,#网络深度,多少个block
        heads=16,#注意力抽头的个数
        mlp_dim=2048,#mlp中反瓶颈结构的中间维度,也就是先升维,再降维
        dropout=0.1,
        emb_dropout=0.1)
    x = torch.rand((2, 3, 256, 256))#测试数据
    output = net(x)

从主干到分支解释代码。

3 ViT网络
class ViT(nn.Module):
    def __init__(self, *, image_size, patch_size, num_classes, dim, depth, heads,
                 mlp_dim, pool = 'cls', channels = 3, dim_head = 64, dropout = 0., emb_dropout = 0.):
        super().__init__()
        assert image_size % patch_size == 0, 'Image dimensions must be divisible by the patch size.'
        num_patches = (image_size // patch_size) ** 2
        patch_dim = channels * patch_size ** 2
        assert
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值