当前位置:网站首页>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)
边栏推荐
- [combinatorics] exponential generating function (proving that the exponential generating function solves the arrangement of multiple sets)
- Micro service component sentinel console call
- Administrative division code acquisition
- Redis cache avalanche, penetration, breakdown
- 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
- [Godot] add menu button
- After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
- SSL / bio pour OpenSSL Get FD
- PHP determines which constellation it belongs to today
- What London Silver Trading software supports multiple languages
猜你喜欢

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

2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report

Computer graduation design PHP sports goods online sales system website

Class exercises

Install apache+php+mysql+phpmyadmin xampp and its error resolution

Computer graduation project PHP library book borrowing management system

SQL injection -day16
![[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)](/img/50/f89092b492d0138304209a72ff05b4.jpg)
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)

As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation

The vscode code is automatically modified to a compliance code when it is formatted and saved
随机推荐
Applet with multiple tabs and Swipers + paging of each tab
Line by line explanation of yolox source code of anchor free series network (5) -- mosaic data enhancement and mathematical understanding
CTO and programmer were both sentenced for losing control of the crawler
ES7 - Optimization of promise
How to expand the capacity of golang slice slice
统计图像中各像素值的数量
BFS - topology sort
Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)
What London Silver Trading software supports multiple languages
win32:堆破壞的dump文件分析
Count the number of pixel values in the image
Supervisor monitors gearman tasks
198. Looting - Dynamic Planning
Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
[combinatorics] exponential generating function (concept of exponential generating function | permutation number exponential generating function = combinatorial number ordinary generating function | e
Enterprise custom form engine solution (12) -- form rule engine 2
Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition
189. Rotation array
What kind of experience is it when the Institute earns 20000 yuan a month?
PHP MySQL reads data