当前位置:网站首页>torch. Tensor splicing and list (tensors)
torch. Tensor splicing and list (tensors)
2022-06-25 12:51:00 【Yancexi】
tensor&list[tensors]
Construct list(tensors)
Create a list of tensors , as well as 2 The tensor is as follows :
import toroch
a = [torch.tensor([[0.7, 0.3], [0.2, 0.8]]),
torch.tensor([[0.5, 0.9], [0.5, 0.5]])]
b = torch.tensor([[0.1, 0.9], [0.3, 0.7]])
c = torch.tensor([[0.1, 0.9, 0.5], [0.3, 0.7, 0.0]])
To stack list(tensors)
Before stacking stack Function to give a little explanation .Stack operation , First upgrade dimension , Re amplification . Reference resources stack And cat. Stack the tensors , Requiring tensor shape Agreement :
stack1 = torch.stack(a) # default: dim=0, [2, 2, 2]
print(stack1)
stack2 = torch.stack((stack1, b), 0)
print(stack2)
output:
tensor([[[0.7000, 0.3000],
[0.2000, 0.8000]],
[[0.5000, 0.9000],
[0.5000, 0.5000]]])
RuntimeError:
stack expects each tensor to be equal size, but got [2, 2, 2] at entry 0 and [2, 2] at entry 1
To concatenate list(tensors)
c = torch.cat([torch.stack(a), b[None]], 0)
# (2, 2, 2), (1, 2, 2) ⇒ (3, 2, 2)
print(c)
Output:
tensor([
[[0.7000, 0.3000],
[0.2000, 0.8000]],
[[0.5000, 0.9000],
[0.5000, 0.5000]],
[[0.1000, 0.9000],
[0.3000, 0.7000]]])
however , If you want to use stack Replace the above cat operation , May be an error , because stack requirement Two input shape Exactly the same , and cat Non spliced parts are allowed to be different . except torch.cat outside , You can also use list.append Complete the above operations .
a.append(b)
print(a)
a.append(c)
print(a)
[tensor([[0.7000, 0.3000],[0.2000, 0.8000]]),
tensor([[0.5000, 0.9000], [0.5000, 0.5000]]),
tensor([[0.1000, 0.9000], [0.3000, 0.7000]])]
[tensor([[0.7000, 0.3000], [0.2000, 0.8000]]),
tensor([[0.5000, 0.9000], [0.5000, 0.5000]]),
tensor([[0.1000, 0.9000], [0.3000, 0.7000]]),
tensor([[0.1000, 0.9000, 0.5000],
[0.3000, 0.7000, 0.0000]])]
be aware ,list.append() Can not only stack the same shape tensors, And can accommodate different shape Of tensor, It's a tensor Containers ! But I have the following problems in the process of using low-level error , Please beware of :
d = []
d.append(a), d.append(b)
print(d)
e = a.append(b)
print(e) # Empty !
Output:
[[tensor([[0.7000, 0.3000],
[0.2000, 0.8000]]), tensor([[0.5000, 0.9000],
[0.5000, 0.5000]])], tensor([[0.1000, 0.9000],
[0.3000, 0.7000]])]
None
list_x.append Nothing new will be returned in the process , Only from list_x In order to get .
The end , And the flower .
边栏推荐
- Tp6 exception handling try catch writing method
- Go defer little knowledge
- Event triggered when El select Clear clears content
- (6) Pyqt5--- > window jump (registration login function)
- 顺序表的折半查找法
- JS picture switching (simple and practical)
- 聊聊高可用的 11 个关键技巧
- 初识CANOpen
- Select randomly by weight [prefix and + dichotomy + random target]
- JSTL tag: fmt:formatdate tag format Chinese standard time or timestamp
猜你喜欢

How to implement a high-performance load balancing architecture?

The amount is verified, and two zeros are spliced by integers during echo

Go novice exploration road 2

美创入选“2022 CCIA中国网络安全竞争力50强”榜单

Three jobs! You can learn this from me (attached with graduation vlog)

Parse JSON format data and save it to entity class

2021-10-21

Elemntui's select+tree implements the search function

C program linking SQLSERVER database: instance failed

Matlab simulation of m-sequence
随机推荐
2021-09-22
词法陷阱(C)
Initialize the project using the express framework
flutter 收到推送后自动消失问题
Embedded software development written examination and interview notes (latest update: February 17, 2022)
利用cmd(命令提示符)安装mysql&&配置环境
MySQL writes user-defined functions and stored procedure syntax (a detailed case is attached, and the problem has been solved: errors are reported when running user-defined functions, and errors are r
(6) Pyqt5--- > window jump (registration login function)
Possible causes of wechat applet decryption failure
5 kinds of viewer for browser
Summary of common MySQL database commands (from my own view)
(4) Pyqt5 tutorial -- > Custom signal and slot (super winding...)
Node child processes and threads
英语口语 - 弱读
(7) Pyqt5 tutorial -- > > window properties and basic controls (continuous update)
JS uses the for loop in the function to insert and delete the array at the specified position
画图常用配色
Draw the satellite sky map according to the azimuth and elevation of the satellite (QT Implementation)
Maximum number [abstract rules for abstract sorting]
[data visualization] 360 ° teaching you how to comprehensively learn visualization - Part 1