当前位置:网站首页>Torch learning notes (2) -- 11 common operation modes of tensor
Torch learning notes (2) -- 11 common operation modes of tensor
2022-07-03 18:22:00 【ZRX_ GIS】
import torch
import numpy as np
tensor = torch.ones((3, 3))
print(tensor)
# tensor Splicing
# cat() take tensor By dimension dim Splicing .tensors= Tensor sequence .dim= Dimensions to be spliced
t_cat_0 = torch.cat([tensor, tensor], dim=0) # tensor In the 0 Dimension self splicing , The first 0 The dimension is equivalent to (x,y) Of x Splices , So the output is (6,3)
print(t_cat_0, t_cat_0.shape)
t_cat_1 = torch.cat([tensor, tensor], dim=1) # tensor In the 1 Dimension self splicing , The first 1 The dimension is equivalent to (x,y) Of y Splices , So the output is (3,6)
print(t_cat_1, t_cat_1.shape)
# stack() In the created dimension dim Splice on .tensors= Tensor sequence .dim= Dimensions to be spliced
t_stack_2 = torch.stack([tensor, tensor, tensor], dim=2) # tensor In the created page 2 Dimension self splicing , If the selected dimension is an existing dimension , Then the existing dimension is directed to +1 Direction backward
print(t_stack_2, t_stack_2.shape)
# tensor segmentation
# chunk() take tensor In the specified dim Upper average segmentation input = tensor ; chunks = Number of cuts ; dim = Dimension to be cut ,return tensor_list
# war: If it's not divisible , The last one tensor Smaller than others tensor
t_chunk_1_2 = torch.chunk(tensor, dim=1, chunks=2) # For two-dimensional 3*3tensor The first dimension is segmented , cut 2
for i, t in enumerate(t_chunk_1_2):
print(i, t, t.shape)
# split() take tensor By dimension dim segmentation ,tensor Tensor to be cut ,split_szie_or_sections:int Indicates the length of each piece ,list According to the said list segmentation .dim = Dimension to be cut ,return tensor_list
t_split_1_2 = torch.split(tensor, 2, dim=1) # For two-dimensional 3*3tensor The first dimension is segmented , The length is 2, here split_szie_or_sections Is an integer
for i, t in enumerate(t_split_1_2):
print(i, t, t.shape)
t_split_1_2 = torch.split(tensor, [1, 2],
dim=1) # For two-dimensional 3*3tensor The first dimension is segmented , The length is 2, here split_szie_or_sections As the list ,list_sum = tensor_dim
for i, t in enumerate(t_split_1_2):
print(i, t, t.shape)
# tensor Indexes
# index_select() In dimension dim On , Press index Index data ,return:index For drinking data splicing tensor,input= To index tensor,dim= To index dimensions ,index= To index serial numbers
tensor = torch.randint(0, 6, size=(3, 3))
t_select = torch.select(tensor, dim=0, index=1)
print(tensor, '\n', t_select)
# masked_select() Press mask Medium True Index ,return: One dimensional qualified position tensor,input= To index tensor,mask= And input Of the same shape bool_tensor # It is often used to filter data
mask = tensor.ge(5) # ge Indicates greater than or equal bool,le Less than or equal to ,lt Say less than
t_maks = torch.masked_select(tensor, mask)
print(t_maks)
# tensor Transformation
# reshape() Transformation tensor shape input Indicates input tensor shape Express tensor shape
# Be careful : When tensor When continuous in memory , new tensor And input share id
t_reshape = torch.reshape(tensor, shape=(9, 1))
print(t_reshape)
# transpose() In exchange for tensor Two dimensions of input: To exchange tensor dim0,dim1
t_transpose = torch.transpose(tensor, dim0=0, dim1=1)
print(t_transpose)
# t() A two-dimensional tensor Transposition , Equivalent to transpose(input,0,1)
t_t = torch.t(tensor)
print(t_t)
# squeeze() The compression length is 1 Dimensions dim =None It means to remove all items with a length of 1 The shaft , If specified dim, Then if and only if the shaft length is 1 when , Can be removed
tensor = torch.rand((1, 2, 3, 1))
print(tensor.shape)
t_squeeze = torch.squeeze(tensor)
print(t_squeeze.shape)
# unsqueeze() basis dim Expand dimensions ,dim = Extended dimensions
t_unsqueeze = torch.unsqueeze(tensor,dim=1)
print(tensor.shape)
print(t_unsqueeze.shape)
边栏推荐
- SSL / bio pour OpenSSL Get FD
- BFS - topology sort
- [LINUX]CentOS 7 安装MYSQL时报错“No package mysql-server available“No package zabbix-server-mysql availabl
- CTO and programmer were both sentenced for losing control of the crawler
- How do microservices aggregate API documents? This wave of operation is too good
- [combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation)
- Computer graduation design PHP makeup sales Beauty shopping mall
- Theoretical description of linear equations and summary of methods for solving linear equations by eigen
- Nodejs (01) - introductory tutorial
- PHP determines which constellation it belongs to today
猜你喜欢
Redis core technology and practice - learning notes (VI) how to achieve data consistency between master and slave Libraries
The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
[Godot] add menu button
English语法_名词 - 分类
CTO and programmer were both sentenced for losing control of the crawler
Redis core technology and practice - learning notes (VIII) sentinel cluster: sentinel hung up
2022-2028 global plasmid DNA cdmo industry research and trend analysis report
Computer graduation design PHP sports goods online sales system website
How to draw non overlapping bubble chart in MATLAB
Theoretical description of linear equations and summary of methods for solving linear equations by eigen
随机推荐
SSL / bio pour OpenSSL Get FD
A. Berland Poker & 1000 [simple mathematical thinking]
Change the single node of Postgres database into master-slave
Recent learning experience
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
[untitled]
Win32: dump file analysis of heap corruption
Fedora 21 installs lamp host server
模块九作业
Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]
解决Zabbix用snmp监控网络流量不准的问题
Sensor 调试流程
How to analyze the rising and falling rules of London gold trend chart
OpenSSL的SSL/BIO_get_fd
Bloom filter [proposed by bloom in 1970; redis cache penetration solution]
[combinatorics] exponential generating function (properties of exponential generating function | exponential generating function solving multiple set arrangement)
AcWing 271. Teacher Yang's photographic arrangement [multidimensional DP]
win32:堆破坏的dump文件分析
2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report
Applet with multiple tabs and Swipers + paging of each tab