当前位置:网站首页>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)
边栏推荐
- The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
- Redis core technology and practice - learning notes (IX): slicing cluster
- Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]
- 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
- Change the single node of Postgres database into master-slave
- On Data Mining
- The second largest gay dating website in the world was exposed, and the status of programmers in 2022
- Unsafe类的使用
- [combinatorics] generating function (summation property)
- Line by line explanation of yolox source code of anchor free series network (5) -- mosaic data enhancement and mathematical understanding
猜你喜欢

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

Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition

Bidding procurement scheme management of Oracle project management system

What problems can cross-border e-commerce sellers solve with multi platform ERP management system

Win 11 major updates, new features love love.

How to analyze the rising and falling rules of London gold trend chart

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

What is SQL get connection

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

2022-2028 global plasmid DNA cdmo industry research and trend analysis report
随机推荐
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
AcWing 271. Teacher Yang's photographic arrangement [multidimensional DP]
Fedora 21 installs lamp host server
Introduction to PHP MySQL
OpenSSL的SSL/BIO_get_fd
[combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
Three gradient descent methods and code implementation
A. Odd Selection【BruteForce】
Applet with multiple tabs and Swipers + paging of each tab
Bloom filter [proposed by bloom in 1970; redis cache penetration solution]
[Tongxin UOS] scanner device management driver installation
Redis core technology and practice - learning notes (11): why not just string
English语法_形容词/副词3级 - 倍数表达
The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
Ml (machine learning) softmax function to realize the classification of simple movie categories
Gear2021 monthly update - December
2022-2028 global petroleum pipe joint industry research and trend analysis report
OpenSSL的SSL/BIO_get_fd
[combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)
English语法_名词 - 分类