当前位置:网站首页>[pytorch learning notes] tensor
[pytorch learning notes] tensor
2022-07-02 19:20:00 【liiiiiiiiiiiiike】
Plate purpose :
Systematization Pytorch The learning process , Only record dry goods
tensor
Tensor and ndarrays similar , Both can usually share the same underlying memory , This eliminates the need to replicate data .
import torch
import numpy as np
tensor initialization
import torch
import numpy as np
## Initialization tensor
# Directly from data
data = [[1,2], [3,4]]
x_data = torch.tensor(data)
# numpy turn torch
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
# From the other tensor
x_ones = torch.ones_like(x_data)
x_rand = torch.rand_like(x_data,dtype=torch.float)
# Use random or constant values
shape = (2,3)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)
# tensor attribute
tensor = torch.rand(3,4)
print(tensor.shape) # dimension
print(tensor.dtype) # data type
print(tensor.device) # tensor Operation unit
tensor operation
# section
tensor = torch.ones(4,4)
print(tensor[0]) # first line
print(tensor[:,0]) # First column
print(tensor[...,-1]) # The last line
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.]]) '''
# Splicing
t1 = torch.cat([tensor,tensor,tensor],dim=1) # Stack by column
print(t1)
# Arithmetic operations
y1 = tensor @ tensor.T # @ Point multiplication .T Transposition
y2 = tensor.matmul(tensor.T)
y3 = torch.rand_like(y1)
torch.matmul(tensor, tensor.T,out=y3) # tensor Point multiplication Output y3
z1 = tensor * tensor
z2 = tensor.mul(tensor)
z3 = torch.rand_like(tensor)
torch.mul(tensor,tensor,out=z3)
# tensor Aggregate into a value
agg = tensor.sum()# <class 'torch.Tensor'>
agg_item = agg.item()#<class 'float'>
print(type(agg_item))
# Local operation
tensor.add_(5) # _ Indicates in situ operation , Directly modifying tensor, Can save memory , But there will be problems when calculating the derivative , Loss of raw data
print(tensor)
# cpu and numpy Upper tensor Can share their underlying memory location
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 compilation principle final examination recall Edition
- Markdown基础语法
- Memory management of C
- R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)
- 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
- How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
- Masa framework - DDD design (1)
- 机器学习笔记 - 时间序列预测研究:法国香槟的月销量
- 数据降维——主成分分析
- C的内存管理
猜你喜欢
新手必看,点击两个按钮切换至不同的内容
思维意识转变是施工企业数字化转型成败的关键
Juypter notebook modify the default open folder and default browser
Imitation Jingdong magnifying glass effect (pink teacher version)
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
ICDE 2023|TKDE Poster Session(CFP)
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式
随机推荐
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
What is 9D movie like? (+ common sense of dimension space)
ICDE 2023|TKDE Poster Session(CFP)
Excel finds the same value in a column, deletes the row or replaces it with a blank value
[fluent] dart data type (VaR data type | object data type)
R language uses Cox of epidisplay package Display function obtains the summary statistical information of Cox regression model (risk rate HR, adjusted risk rate and its confidence interval, P value of
Progress-进度条
数据降维——主成分分析
【JVM调优实战100例】03——JVM堆调优四例
[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference
高级性能测试系列《24. 通过jdbc执行sql脚本》
Markdown基础语法
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
Learn the knowledge points of eight part essay ~ ~ 1
Virtual machine initialization script, virtual machine mutual secret key free
R language dplyr package Na_ The if function converts the control in the vector value into the missing value Na, and converts the specified content into the missing value Na according to the mapping r
Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
【pytorch学习笔记】Tensor
2022软件工程期末考试 回忆版