当前位置:网站首页>【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.]
边栏推荐
- Page title component
- Progress progress bar
- Introduction to the paper | application of machine learning in database cardinality estimation
- R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA
- Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey
- Binary operation
- 高级性能测试系列《24. 通过jdbc执行sql脚本》
- Codeworks 5 questions per day (1700 average) - day 4
- 二进制操作
- R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
猜你喜欢
![[fluent] dart data type (VaR data type | object data type)](/img/1b/fe2529af5f6663fad1fb7861f14ab5.jpg)
[fluent] dart data type (VaR data type | object data type)

Machine learning notes - time series prediction research: monthly sales of French champagne

codeforces每日5题(均1700)-第四天

高级性能测试系列《24. 通过jdbc执行sql脚本》

High frequency interview questions

医院在线问诊源码 医院视频问诊源码 医院小程序源码

论文导读 | 机器学习在数据库基数估计中的应用

使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5

LightGroupButton* sender = static_ cast<LightGroupButton*>(QObject::sender());
随机推荐
Tips for material UV masking
[fluent] dart data type (VaR data type | object data type)
R语言使用epiDisplay包的lrtest函数对多个glm模型(logisti回归)执行似然比检验(Likelihood ratio test)对比两个模型的性能是否有差异、广义线性模型的似然比检
Emmet basic syntax
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
Golang并发编程——goroutine、channel、sync
Yunna | why use the fixed asset management system and how to enable it
What is 9D movie like? (+ common sense of dimension space)
Emmet基础语法
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
R language ggplot2 visual Facet: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
Qpropertyanimation use and toast case list in QT
Crypto usage in nodejs
High frequency interview questions
FastDFS安装
mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
juypter notebook 修改默认打开文件夹以及默认浏览器
日期工具类(不定时更新)
[paper reading] Ca net: leveraging contextual features for lung cancer prediction
yolov3 训练自己的数据集之生成train.txt