当前位置:网站首页>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)
边栏推荐
- What is SQL get connection
- [combinatorics] generating function (commutative property | derivative property | integral property)
- 【统信UOS】扫描仪设备管理驱动安装
- Win32: dump file analysis of heap corruption
- Codeforces Round #803 (Div. 2) C. 3SUM Closure
- How to analyze the rising and falling rules of London gold trend chart
- OpenSSL的SSL/BIO_get_fd
- [combinatorics] exponential generating function (example of exponential generating function solving multiple set arrangement)
- 2022-2028 global scar care product industry research and trend analysis report
- Usage of laravel conditional array in
猜你喜欢

English grammar_ Adjective / adverb Level 3 - multiple expression

PHP MySQL preprocessing statement

Computer graduation design PHP sports goods online sales system website
![[combinatorics] generating function (summation property)](/img/74/e6ef8ee69ed07d62df9f213c015f2c.jpg)
[combinatorics] generating function (summation property)

On Data Mining

English语法_名词 - 分类

2022-2028 global aircraft head up display (HUD) industry research and trend analysis report

Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download

Win 11 major updates, new features love love.

Prototype inheritance..
随机推荐
PHP MySQL reads data
204. Count prime
Image 24 bit depth to 8 bit depth
网格图中递增路径的数目[dfs逆向路径+记忆dfs]
[combinatorics] exponential generating function (proving that the exponential generating function solves the arrangement of multiple sets)
图像24位深度转8位深度
win32:堆破壞的dump文件分析
Computer graduation design PHP campus address book telephone number inquiry system
Mature port AI ceaspectus leads the world in the application of AI in terminals, CIMC Feitong advanced products go global, smart terminals, intelligent ports, intelligent terminals
Nodejs (01) - introductory tutorial
[linux]centos 7 reports an error when installing MySQL "no package MySQL server available" no package ZABBIX server MySQL available
(9) Opencv Canny edge detection
The vscode code is automatically modified to a compliance code when it is formatted and saved
Closure and closure function
What problems can cross-border e-commerce sellers solve with multi platform ERP management system
Distributed task distribution framework gearman
【统信UOS】扫描仪设备管理驱动安装
[LINUX]CentOS 7 安装MYSQL时报错“No package mysql-server available“No package zabbix-server-mysql availabl
Redis core technology and practice - learning notes (VI) how to achieve data consistency between master and slave Libraries
Grammaire anglaise Nom - Classification