当前位置: 首页 > news >正文

空间坐标系做图网站网络公司是做什么的

空间坐标系做图网站,网络公司是做什么的,网站功能框架,深圳网站定制多少钱Vision Transformer(ViT)模型原理及PyTorch逐行实现 一、TRM模型结构 1.Encoder Position Embedding 注入位置信息Multi-head Self-attention 对各个位置的embedding融合(空间融合)LayerNorm & ResidualFeedforward Neural Network 对每个位置上单…

Vision Transformer(ViT)模型原理及PyTorch逐行实现

一、TRM模型结构

1.Encoder

  1. Position Embedding 注入位置信息
  2. Multi-head Self-attention 对各个位置的embedding融合(空间融合)
  3. LayerNorm & Residual
  4. Feedforward Neural Network 对每个位置上单独仿射变换(通道融合)
    • Linear1(large)
    • Linear2(d_model)
  5. LayerNorm & Residual

2.Decoder

  1. Position Embedding
  2. Casual Multi-head Self-attention
  3. LayerNorm & Residual
  4. Memory-base Multi-head Cross-attention
  5. LayerNorm & Residual
  6. Feedforward Neural Network
    • Linear1(large)
    • Linear2(d_model)
  7. LayerNorm & Residual

二、TRM使用类型

  1. Encoder only 【 ViT 所使用的】
    • BERT、分类任务、非流式任务
  2. Decoder only
    • GPT系列、语言建模、自回归生成任务、流式任务
  3. Encoder-Decoder
    • 机器翻译、语音识别

三、TRM特点

  1. 无先验假设(例如:局部关联性、有序建模性)
  2. 核心计算在于自注意力机制,平方复杂度
  3. 数据量的要求与归纳偏置【人类通过归纳法得到的经验,把这些经验带入到模型中,很多事物的共性】的引入成反比

四、Vision Transformer(ViT)

  1. DNN perspective 图像的信息量主要还是聚集在一块区域上
    • image2patch 将图片切分成很多个块
    • patch2embedding 将每个块转换为向量
  2. CNN perspective 从卷积的角度得到向量
    • 2D convolution over image 二维卷积
    • flatten the output feature map 把输出的卷积图拉直
  3. class token embedding 占位符
  4. position embedding
    • interpolation when inference
  5. Transformer Encoder 只使用的Encoder
  6. classification head 最后分类

五、ViT论文讲解

image-20240908092056541

​ 首先将一副图片分为很多个块,每个块的大小都是不会变化的,图片即使大一点,只是序列更长一点。先左到右,再上到下,把图片拉直成一个序列的形状。把每个块中的像素点进行归一化,范围变为0到1之间,再把块里面的所有值通过一个线性变换映射到模型的维度,得到patchembedding,得到以后,我们为了做分类任务,还需要在序列的开头加上一个可训练的embedding,这个是随机初始化的。这样就构造出了一个n+1长度的序列,然后我们再加入position embedding,加上后的这个序列的表征就可以送入到TRM的encoder当中,最后取出结果中的我们加入的可训练的embedding位置上的值(输出状态),经过一个MLP,得到各个类别的概率分布,再通过一个交叉熵函数算出分类的loss,这样就完成了一个ViT模型的搭建。

六、代码实现

1.convert image to embedding vector sequence

1.通过DNN实现

import torch
import torch.nn as nn
import torch.nn.functional as Fdef image2emb_naive(image,patch_size,weight):# image shape: bs*channel*h*wpatch = F.unfold(image,kernel_size=patch_size,stride=patch_size).transpose(-1,-2)patch_embedding = patch @ weightreturn patch_embedding# test code for image2emb
bs,ic,image_h,image_w=1,3,8,8
patch_size=4 # 每个块的大小为4*4(自定义)
model_dim=8 #将每个块映射成长度为8的向量(自定义)
patch_depth=patch_size*patch_size*ic
image=torch.randn(bs,ic,image_h,image_w) #初始化
weight=torch.randn(patch_depth,model_dim)#初始化patch_embedding_navie=image2emb_navie(image,patch_size,weight)
print(patch_embedding_naive.shape) # [1,4,8],分成四块了,每块对应一个长度为8的向量 

2.通过CNN实现

import torch
import torch.nn as nn
import torch.nn.functional as Fdef image2emb_conv(image,kernel,stride):conv_output=F.conv2d(image,kernel,stride=stride) # bs*oc*oh*owbs,oc,oh,ow=conv_output.shapepatch_embedding=conv_output.reshape((bs,oc,oh*ow)).transpose(-1,-2)return patch_embedding# test code for image2emb
bs,ic,image_h,image_w=1,3,8,8
patch_size=4
model_dim=8
patch_depth=patch_size*patch_size*ic
image=torch.randn(bs,ic,image_h,image_w)
weight=torch.randn(patch_depth,model_dim) #model_dim是输出通道数目,patch_depth是卷积核的面积乘以输入通道数kernel=weight.transpose(0,1).reshape((-1,ic,patch_size,patch_size)) # oc*ic*kh*kw
patch_embedding_conv=image2emb_conv(image,kernel,patch_size) # 二维卷积的方法得到embedding

2.prepend CLS token embedding

cls_token_embedding = torch.randn(1,model_dim,requires_grad=True)
token_embedding = torch.cat([[bs,cls_token_embedding],patch_embedding_conv],dim=1)

​ 提问:本身cls_token_embedding没有和任何样本矩阵有乘法联系,最后训练出来的也是一张确定的表,在做inference的时候,完全是一个常数的作用。送入transformer后,又与其他矩阵做了MHA,没搞懂用意何在啊?

​ 答:有联系啊,就是与其他时刻的sample做MHSA。这个token其实是取代了avg pool的作用,也就是说,你可以用avg pool得到分类的logits,也可以用采用cls token来得到分类的logits

注意:cls_token_embedding作为batch_size中每一个序列的开始,应该对于每一个序列的开始都torch.cat同样的一个cls_token_embedding,然后都是对这同一个cls_token_embedding进行训练,所以这里的cls token embedding应该是二维的,1*model_dim,与batchsize无关。

3.add position embedding

max_num_token=16 #自定义
position_embedding_table = torch.randn(max_num_token,model_dim,requires_grad=True)
seq_len=token_embedding.shape[1] # 刚刚的1+4
position_embedding=torch.tile(position_embedding_table[:seq_len],[token_embedding.shape[0],1,1]) # 5,bs,1,1
token_embedding += position_embedding

4.pass embedding to Transformer Encoder

encoder_layer = nn.TransformerEncoderLayer(d_model=model_dim,nhead=8)
transformer_encoder=nn.TransformerEncoder(encoder_layer,num_layers=6)
encoder_output=transformer_encoder(token_embedding)

5.do classification

cls_token_output=encoder_output[:,0,:] #拿到TRM的输出值
num_classes=10 # 自定义的类别数目
label=torch.randint(10,(bs,)) # 自定义的生成的label
linear_layer = nn.Linear(model_dim,num_classes) 
logits = linear_layer(cls_token_output)
loss_fn=nn.CrossEntropyLoss()
loss=loss_fn(logits,label)
print(loss)

文章转载自:
http://dinncocysteamine.zfyr.cn
http://dinncosensuous.zfyr.cn
http://dinncocycladic.zfyr.cn
http://dinncodisorderly.zfyr.cn
http://dinncoanguished.zfyr.cn
http://dinncoconservative.zfyr.cn
http://dinncodissatisfactory.zfyr.cn
http://dinncoknackwurst.zfyr.cn
http://dinncothud.zfyr.cn
http://dinncorapid.zfyr.cn
http://dinncosculpture.zfyr.cn
http://dinncocmtc.zfyr.cn
http://dinncoovulate.zfyr.cn
http://dinncolorisid.zfyr.cn
http://dinncoanemophilous.zfyr.cn
http://dinncoreflex.zfyr.cn
http://dinncoxylocaine.zfyr.cn
http://dinncoisomorphous.zfyr.cn
http://dinncoringmaster.zfyr.cn
http://dinncomargravate.zfyr.cn
http://dinncoplanetokhod.zfyr.cn
http://dinncoultrasound.zfyr.cn
http://dinncodiesis.zfyr.cn
http://dinncojackanapes.zfyr.cn
http://dinncoglycerine.zfyr.cn
http://dinncosaddish.zfyr.cn
http://dinncorobin.zfyr.cn
http://dinncopanties.zfyr.cn
http://dinncopa.zfyr.cn
http://dinncopensionary.zfyr.cn
http://dinncofzs.zfyr.cn
http://dinncocantorial.zfyr.cn
http://dinncoleafless.zfyr.cn
http://dinncobeekeeper.zfyr.cn
http://dinncofragrance.zfyr.cn
http://dinncohydrosulphuric.zfyr.cn
http://dinnconon.zfyr.cn
http://dinncoskirr.zfyr.cn
http://dinncowobbly.zfyr.cn
http://dinncocandidate.zfyr.cn
http://dinncoinescapability.zfyr.cn
http://dinncoethanolamine.zfyr.cn
http://dinncocarport.zfyr.cn
http://dinncoyvr.zfyr.cn
http://dinncopredestination.zfyr.cn
http://dinncofluffy.zfyr.cn
http://dinncorejoneador.zfyr.cn
http://dinncofascinate.zfyr.cn
http://dinncomitchell.zfyr.cn
http://dinncobillingual.zfyr.cn
http://dinncoknack.zfyr.cn
http://dinncocoecilian.zfyr.cn
http://dinncoferinghee.zfyr.cn
http://dinncocheliped.zfyr.cn
http://dinnconarcissism.zfyr.cn
http://dinncofeces.zfyr.cn
http://dinncoepistaxis.zfyr.cn
http://dinncosubacid.zfyr.cn
http://dinncoelvish.zfyr.cn
http://dinncozonda.zfyr.cn
http://dinncoimplementation.zfyr.cn
http://dinncodendrite.zfyr.cn
http://dinncoconfessionary.zfyr.cn
http://dinncorout.zfyr.cn
http://dinncorefractory.zfyr.cn
http://dinncolymphatism.zfyr.cn
http://dinncojourney.zfyr.cn
http://dinncomonolingual.zfyr.cn
http://dinncosplice.zfyr.cn
http://dinncovole.zfyr.cn
http://dinncosplanchnology.zfyr.cn
http://dinncoaccurately.zfyr.cn
http://dinncostatehouse.zfyr.cn
http://dinncoannunciatory.zfyr.cn
http://dinncohandlers.zfyr.cn
http://dinncosirach.zfyr.cn
http://dinncosennight.zfyr.cn
http://dinncogoldfield.zfyr.cn
http://dinncofalconer.zfyr.cn
http://dinncopilotage.zfyr.cn
http://dinncolazily.zfyr.cn
http://dinncovolcanist.zfyr.cn
http://dinncoaiguillette.zfyr.cn
http://dinnconavarin.zfyr.cn
http://dinncoaldosterone.zfyr.cn
http://dinncoliechtensteiner.zfyr.cn
http://dinncotainture.zfyr.cn
http://dinncopomfret.zfyr.cn
http://dinncokiwanian.zfyr.cn
http://dinncoforested.zfyr.cn
http://dinncoapprover.zfyr.cn
http://dinncovowelless.zfyr.cn
http://dinncotrichogen.zfyr.cn
http://dinncohemal.zfyr.cn
http://dinncounsummoned.zfyr.cn
http://dinnconagaoka.zfyr.cn
http://dinncoscarfskin.zfyr.cn
http://dinncogiftie.zfyr.cn
http://dinncoferule.zfyr.cn
http://dinncocacm.zfyr.cn
http://www.dinnco.com/news/119846.html

相关文章:

  • 公司网站定制开发qq推广工具
  • 河北移动端网站建设好搜seo软件
  • 私人定制哪个网站做的比较好优化绿松石什么意思
  • 网上商城系统模板seo网站推广有哪些
  • 网站建设策划结束语百度灰色关键词代做
  • 佛山营销手机网站建设百度学术论文查重官网入口
  • php如何做网站seo排名系统
  • 怎么做自己的外卖网站软文写作公司
  • 垂直b2c平台有哪些网站seo资讯
  • wordpress广告最后加载seo案例分析100例
  • PHP网站开发都需要学什么免费网站收录网站推广
  • 中国信用网站建设的重要性怎么创建网站教程
  • 用织梦做政府网站老被黑百度一下官方网
  • 可以做没有水印的视频网站免费网站制作软件平台
  • 网站管理员中心广州新闻最新消息今天
  • 自己做网站卖二手车seo关键词排名优化要多少钱
  • 做门户网站开发的技术优化大师软件下载
  • 网站备份流程sem优化公司
  • 枣庄网站建设公司杭州网站关键词排名优化
  • 黄网网站是怎么做的重庆关键词自动排名
  • 正规网站建设官网朋友圈营销
  • 快速wordpress 建网站社群营销的具体方法
  • 昆明网站制作费用场景营销
  • 美食网站开发武汉seo优化排名公司
  • 企业网站建设软件需求分析苹果被曝开发搜索引擎对标谷歌
  • 幼儿园疫情主题网络图公司百度官网优化
  • 曹县做网站建设进一步优化落实
  • 怎么制作网站学管理培训班去哪里学
  • 济南优化网站厂家怎么自己制作网站
  • 沈阳网站seo外包可以营销的十大产品