当前位置:网站首页>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 .
边栏推荐
- Initialize the project using the express framework
- PPT绘图之AI助力论文图
- 3+1保障:高可用系统稳定性是如何炼成的?
- 为何数据库也云原生了?
- (5) Pyqt5 ---- another method of connecting signals and slots
- Laravel is not logged in and cannot access the background by entering the URL
- 地理空间搜索:kd树的实现原理
- 聊聊高可用的 11 个关键技巧
- JS enter three integers a, B and C, and sort them from large to small (two methods)
- JQ dynamic setting radio does not take effect when selected
猜你喜欢

微信全文搜索技术优化

画图常用配色
![按权重随机选择[前缀和+二分+随机target]](/img/84/7f930f55f8006a4bf6e23ef05676ac.png)
按权重随机选择[前缀和+二分+随机target]

Penetration tool environment -- use of cknife Chinese kitchen knife

Navicat premium view password scheme

How to implement a high-performance load balancing architecture?

GPS receiver design (1)

更新pip&下载jupyter lab

三入职场!你可以从我身上学到这些(附毕业Vlog)

Go novice exploration road 2
随机推荐
Guess Tongyuan B
Go novice exploration road 1
The push(), pop(), unshift(), shift() method in the JS array
Laravel multi project mutual access
又是被Visdom搞崩溃的一夜
Total number of MySQL statistics, used and unused
Select randomly by weight [prefix and + dichotomy + random target]
Zhangxiaobai's road of penetration (IV) -- detailed explanation of XSS cross site script vulnerabilities
Disconnected: No supported authentication methods available (server sent: )
美创入选“2022 CCIA中国网络安全竞争力50强”榜单
JS SMS countdown implementation (simple code)
Possible problems when idea encounters errors occurred while compiling module (solved)
Penetration tool environment -- use of cknife Chinese kitchen knife
(6) Pyqt5--- > window jump (registration login function)
visual studio2019链接opencv
Jupyter Notebook主题字体设置及自动代码补全
Using CMD (command prompt) to install MySQL & configure the environment
Maximum number [abstract rules for abstract sorting]
JS uses the for loop in the function to insert and delete the array at the specified position
Swagger document generated by node project API in vscode