当前位置:网站首页>[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.]
边栏推荐
- MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
- 使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
- 新手必看,点击两个按钮切换至不同的内容
- metric_logger小解
- Juypter notebook modify the default open folder and default browser
- 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
- Develop fixed asset management system, what voice is used to develop fixed asset management system
- Virtual machine initialization script, virtual machine mutual secret key free
- Progress-进度条
- 使用CLion编译OGLPG-9th-Edition源码
猜你喜欢

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

Novice must see, click two buttons to switch to different content

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

ICDE 2023|TKDE Poster Session(CFP)

Juypter notebook modify the default open folder and default browser

9D电影是怎样的?(+维度空间常识)

Excel如何进行隔行复制粘贴

论文导读 | 关于将预训练语言模型作为知识库的分析与批评

yolov3 训练自己的数据集之生成train.txt

Introduction to the paper | application of machine learning in database cardinality estimation
随机推荐
mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
Juypter notebook modify the default open folder and default browser
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
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
FastDFS安装
思维意识转变是施工企业数字化转型成败的关键
二进制操作
Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
yolov3 训练自己的数据集之生成train.txt
MySQL advanced (Advanced) SQL statement
Use cheat engine to modify money, life and stars in Kingdom rush
Develop fixed asset management system, what voice is used to develop fixed asset management system
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
[daily question] the next day
Codeworks 5 questions per day (1700 average) - day 4
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
How can retail enterprises open the second growth curve under the full link digital transformation
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)