当前位置:网站首页>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)
边栏推荐
- Redis on local access server
- Enterprise custom form engine solution (12) -- form rule engine 2
- (9) Opencv Canny edge detection
- 分布式的任务分发框架-Gearman
- PHP MySQL inserts data
- How to draw non overlapping bubble chart in MATLAB
- Redis cache avalanche, penetration, breakdown
- 2022-2028 global aircraft head up display (HUD) industry research and trend analysis report
- Line by line explanation of yolox source code of anchor free series network (6) -- mixup data enhancement
- Redis core technology and practice - learning notes (IX): slicing cluster
猜你喜欢

How do microservices aggregate API documents? This wave of operation is too good

Grammaire anglaise Nom - Classification

Three gradient descent methods and code implementation

Computer graduation design PHP sports goods online sales system website

English grammar_ Adjective / adverb Level 3 - multiple expression

Class exercises

Redis core technology and practice - learning notes (IX): slicing cluster

Win 11 major updates, new features love love.

2022-2028 global physiotherapy clinic industry research and trend analysis report

English语法_名词 - 分类
随机推荐
Keepalived setting does not preempt resources
Grammaire anglaise Nom - Classification
圖像24比特深度轉8比特深度
Solve the problem of inaccurate network traffic monitored by ZABBIX with SNMP
Introduction to PHP MySQL
[combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
Use of unsafe class
[教程]在 CoreOS 上构建你的第一个应用
[combinatorics] generating function (example of using generating function to solve the number of solutions of indefinite equation)
Bidding procurement scheme management of Oracle project management system
2022-2028 global petroleum pipe joint industry research and trend analysis report
Redis on local access server
English grammar_ Adjective / adverb Level 3 - multiple expression
Summary and Reflection on the third week of winter vacation
The second largest gay dating website in the world was exposed, and the status of programmers in 2022
Life perception 1
Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
[combinatorics] generating function (commutative property | derivative property | integral property)
[tutorial] build your first application on coreos
Nodejs (01) - introductory tutorial