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

PyTorch 切片运算 (Slice Operator)

PyTorch 切片运算 {Slice Operator}

  • 1. `[:, -1, :]`
  • 2. `[:, [-1], :]`
  • References

1. [:, -1, :]

https://github.com/karpathy/llama2.c/blob/master/model.py

import torch

logits = torch.arange(1, 16)
print("logits.shape:", logits.shape)
print("logits:\n", logits)

logits = logits.view(1, 3, 5)
print("\nlogits.shape:", logits.shape)
print("logits:\n", logits)

final_logit_1 = logits[:, -1, :]
print("\nfinal_logit_1.shape:", final_logit_1.shape)
print("final_logit_1:\n", final_logit_1)

final_logit_2 = logits[:, -1]
print("\nfinal_logit_2.shape:", final_logit_2.shape)
print("final_logit_2:\n", final_logit_2)

final_logit_3 = logits[:, 2, :]
print("\nfinal_logit_3.shape:", final_logit_3.shape)
print("final_logit_3:\n", final_logit_3)

final_logit_4 = logits[:, 2]
print("\nfinal_logit_4.shape:", final_logit_4.shape)
print("final_logit_4:\n", final_logit_4)

/home/yongqiang/miniconda3/bin/python /home/yongqiang/llm_work/llama2.c/yongqiang.py 
logits.shape: torch.Size([15])
logits:
 tensor([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])

logits.shape: torch.Size([1, 3, 5])
logits:
 tensor([[[ 1,  2,  3,  4,  5],
         [ 6,  7,  8,  9, 10],
         [11, 12, 13, 14, 15]]])

final_logit_1.shape: torch.Size([1, 5])
final_logit_1:
 tensor([[11, 12, 13, 14, 15]])

final_logit_2.shape: torch.Size([1, 5])
final_logit_2:
 tensor([[11, 12, 13, 14, 15]])

final_logit_3.shape: torch.Size([1, 5])
final_logit_3:
 tensor([[11, 12, 13, 14, 15]])

final_logit_4.shape: torch.Size([1, 5])
final_logit_4:
 tensor([[11, 12, 13, 14, 15]])

Process finished with exit code 0

2. [:, [-1], :]

https://github.com/karpathy/llama2.c/blob/master/model.py

import torch

logits = torch.arange(1, 16)
print("logits.shape:", logits.shape)
print("logits:\n", logits)

logits = logits.view(1, 3, 5)
print("\nlogits.shape:", logits.shape)
print("logits:\n", logits)

final_logit_1 = logits[:, -1, :]
print("\nfinal_logit_1.shape:", final_logit_1.shape)
print("final_logit_1:\n", final_logit_1)

final_logit_2 = logits[:, [-1], :]
print("\nfinal_logit_2.shape:", final_logit_2.shape)
print("final_logit_2:\n", final_logit_2)

final_logit_3 = logits[:, -1]
print("\nfinal_logit_3.shape:", final_logit_3.shape)
print("final_logit_3:\n", final_logit_3)

final_logit_4 = logits[:, [-1]]
print("\nfinal_logit_4.shape:", final_logit_4.shape)
print("final_logit_4:\n", final_logit_4)

/home/yongqiang/miniconda3/bin/python /home/yongqiang/llm_work/llama2.c/yongqiang.py 
logits.shape: torch.Size([15])
logits:
 tensor([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])

logits.shape: torch.Size([1, 3, 5])
logits:
 tensor([[[ 1,  2,  3,  4,  5],
         [ 6,  7,  8,  9, 10],
         [11, 12, 13, 14, 15]]])

final_logit_1.shape: torch.Size([1, 5])
final_logit_1:
 tensor([[11, 12, 13, 14, 15]])

final_logit_2.shape: torch.Size([1, 1, 5])
final_logit_2:
 tensor([[[11, 12, 13, 14, 15]]])

final_logit_3.shape: torch.Size([1, 5])
final_logit_3:
 tensor([[11, 12, 13, 14, 15]])

final_logit_4.shape: torch.Size([1, 1, 5])
final_logit_4:
 tensor([[[11, 12, 13, 14, 15]]])

Process finished with exit code 0

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/


http://www.kler.cn/a/427996.html

相关文章:

  • 搜索引擎快速收录:关键词布局的艺术
  • RK3568中使用QT opencv(显示基础图像)
  • 【MySQL】MySQL客户端连接用 localhost和127.0.0.1的区别
  • Pandas进行MongoDB数据库CRUD
  • 【狂热算法篇】探秘图论之Dijkstra 算法:穿越图的迷宫的最短路径力量(通俗易懂版)
  • Golang笔记——常用库context和runtime
  • 【人工智能】用Python构建高效的自动化数据标注工具:从理论到实现
  • MySQL 存储引擎详解
  • 负载均衡OJ项目中遇到的问题
  • 我的“ai学伴”助力“程序”迭代
  • 应用案例 | 船舶海洋: 水下无人航行器数字样机功能模型构建
  • RK3568平台(内存篇)DDR定频修改
  • OD C卷【热点网站统计】
  • 漫画之家Spring Boot应用:打造您的数字漫画馆
  • 如何从命令行和用户输入收集输入
  • 读取csv里面的文件数据画曲线
  • B4X编程语言:B4J控件的样式设置属性(Style/StyleClasses)
  • 利用R包QstFstComp包进行Qst-Fst分析
  • 处理海量数据的查重方法总结
  • 【WRF运行第一期(Ubuntu)】模型运行前准备
  • 高数极限与连续练习题(自用)
  • 网络渗透实验二(渗透课)
  • 新160个crackme - 109-Jony-crackme
  • ElementUI:el-tabs 切换之前判断是否满足条件
  • docker-3.docker权限问题
  • 开发一个AMT(automatic multicast tunnel)协议库 C++版本,Client,Server详细的设计