当前位置:网站首页>【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.]
边栏推荐
- [论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
- Imitation Jingdong magnifying glass effect (pink teacher version)
- 教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
- R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
- Memory management of C
- 使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
- C file input operation
- 从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
- 机器学习笔记 - 时间序列预测研究:法国香槟的月销量
- GMapping代码解析[通俗易懂]
猜你喜欢

Introduction to the paper | application of machine learning in database cardinality estimation

云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统

Mini Golf Course: a good place for leisure and tourism in London
![[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning](/img/59/6c776e0607a52962b72fbea2e64c8e.png)
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning

Excel finds the same value in a column, deletes the row or replaces it with a blank value

STM32G0 USB DFU 升级校验出错-2

ICDE 2023|TKDE Poster Session(CFP)
![[0701] [paper reading] allowing data imbalance issue with perforated input during influence](/img/c7/9b7dc4b4bda4ecfe07aec1367fe059.png)
[0701] [paper reading] allowing data imbalance issue with perforated input during influence

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

Markdown基础语法
随机推荐
How performance testing creates business value
R language uses lrtest function of epidisplay package to perform likelihood ratio test on multiple GLM models (logisti regression). Compare whether the performance of the two models is different, and
mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
How can retail enterprises open the second growth curve under the full link digital transformation
Markdown basic grammar
R语言使用epiDisplay包的lsNoFunction函数列出当前空间中的所有对象、除了用户自定义的函数对象
Memory management of C
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
[daily question] the next day
仿京东放大镜效果(pink老师版)
PyTorch函数中的__call__和forward函数
Machine learning notes - time series prediction research: monthly sales of French champagne
Yolov3 trains its own data set to generate train txt
以太网PHY层芯片LAN8720A简介
【ERP软件】ERP体系二次开发有哪些危险?
Have you stepped on the nine common pits in the e-commerce system?
高频面试题
Fastdfs installation
juypter notebook 修改默认打开文件夹以及默认浏览器
Codeworks 5 questions per day (1700 average) - day 4