当前位置:网站首页>【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.]
边栏推荐
- 2022 software engineering final exam recall Edition
- R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
- R语言ggplot2可视化:可视化折线图、使用labs函数为折线图添加自定义的X轴标签信息
- Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
- QT中的QPropertyAnimation使用和toast案列
- 电商系统中常见的 9 大坑,你踩过没?
- Competence of product manager
- M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
- Excel查找一列中的相同值,删除该行或替换为空值
- R language ggplot2 visual Facet: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
猜你喜欢
Yolov3 trains its own data set to generate train txt
Markdown基础语法
Processing strategy of message queue message loss and repeated message sending
The difference between SLC, MLC, TLC and QLC NAND SSD: which is better?
电商系统中常见的 9 大坑,你踩过没?
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
Mini Golf Course: a good place for leisure and tourism in London
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
Excel finds the same value in a column, deletes the row or replaces it with a blank value
随机推荐
Date tool class (updated from time to time)
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
Markdown basic grammar
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
电脑使用哪个录制视频软件比较好
【JVM调优实战100例】03——JVM堆调优四例
Emmet基础语法
R language ggplot2 visualization: gganimate package creates dynamic histogram animation (GIF) and uses transition_ The States function displays a histogram step by step along a given dimension in the
How to delete the border of links in IE? [repeat] - how to remove borders around links in IE? [duplicate]
FastDFS安装
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
日期工具类(不定时更新)
使用CLion编译OGLPG-9th-Edition源码
拦截器与过滤器的区别
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
Binary operation