当前位置:网站首页>【pytorch学习笔记】Tensor
【pytorch学习笔记】Tensor
2022-07-02 18:13:00 【liiiiiiiiiiiiike】
板块目的:
系统整理Pytorch学习过程,只记录干货
tensor
Tensor和ndarrays类似,两者通常可以共享相同底层内存,从而无需复制数据。
import torch
import numpy as np
tensor初始化
import torch
import numpy as np
## 初始化张量
# 直接从数据
data = [[1,2], [3,4]]
x_data = torch.tensor(data)
# numpy 转torch
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
# 从另外一个tensor
x_ones = torch.ones_like(x_data)
x_rand = torch.rand_like(x_data,dtype=torch.float)
# 使用随机或恒定值
shape = (2,3)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)
# tensor属性
tensor = torch.rand(3,4)
print(tensor.shape) # 维度
print(tensor.dtype) # 数据类型
print(tensor.device) # tensor运行单元
tensor运算
# 切片
tensor = torch.ones(4,4)
print(tensor[0]) # 第一行
print(tensor[:,0]) # 第一列
print(tensor[...,-1]) # 最后一行
tensor[:,1] = 0
print(tensor)
''' tensor([1., 1., 1., 1.]) tensor([1., 1., 1., 1.]) tensor([1., 1., 1., 1.]) tensor([[1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.]]) '''
# 拼接
t1 = torch.cat([tensor,tensor,tensor],dim=1) # 按照列来堆叠
print(t1)
# 算术运算
y1 = tensor @ tensor.T # @ 点乘 .T转置
y2 = tensor.matmul(tensor.T)
y3 = torch.rand_like(y1)
torch.matmul(tensor, tensor.T,out=y3) # tensor点乘 输出y3
z1 = tensor * tensor
z2 = tensor.mul(tensor)
z3 = torch.rand_like(tensor)
torch.mul(tensor,tensor,out=z3)
# tensor聚合为一个值
agg = tensor.sum()# <class 'torch.Tensor'>
agg_item = agg.item()#<class 'float'>
print(type(agg_item))
# 就地操作
tensor.add_(5) # _表示原地操作,直接修改tensor,可节省内存,但在计算导数时会出现问题,丢失原始数据
print(tensor)
# cpu和numpy上的tensor可以共享它们的底层内存位置
t = torch.ones(5) # torch.tensor
n = t.numpy()# ndarray
t.add_(1)
print(t)# tensor([2., 2., 2., 2., 2.])
print(n)# [2. 2. 2. 2. 2.]
边栏推荐
- Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
- Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
- ICDE 2023|TKDE Poster Session(CFP)
- Introduction to the paper | application of machine learning in database cardinality estimation
- [test development] takes you to know what software testing is
- codeforces每日5题(均1700)-第四天
- R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
- Codeworks 5 questions per day (1700 average) - day 4
- 【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
- Golang concurrent programming goroutine, channel, sync
猜你喜欢
![[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter](/img/c4/3bba96fda92328704c2ddd929dcdf6.png)
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter

【JVM调优实战100例】03——JVM堆调优四例

线程应用实例

新手必看,點擊兩個按鈕切換至不同的內容

M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)

Singapore summer tourism strategy: play Singapore Sentosa Island in one day

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5

全志A33使用主线U-Boot

【测试开发】软件测试—概念篇

Excel查找一列中的相同值,删除该行或替换为空值
随机推荐
R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
Imitation Jingdong magnifying glass effect (pink teacher version)
[daily question] the next day
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning
【JVM调优实战100例】01——JVM的介绍与程序计数器
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
数据降维——主成分分析
Progress progress bar
Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
Tips for material UV masking
Golang并发编程——goroutine、channel、sync
R语言使用epiDisplay包的lsNoFunction函数列出当前空间中的所有对象、除了用户自定义的函数对象
仿京东放大镜效果(pink老师版)
yolov3 训练自己的数据集之生成train.txt
GMapping代码解析[通俗易懂]
Machine learning notes - time series prediction research: monthly sales of French champagne
Fastdfs installation
Singapore summer tourism strategy: play Singapore Sentosa Island in one day
Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比