当前位置:网站首页>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 .
边栏推荐
- Match regular with fixed format beginning and fixed end
- (7) Pyqt5 tutorial -- > > window properties and basic controls (continuous update)
- Swagger document generated by node project API in vscode
- Jeecgboot startup popup configuration is still incorrect
- 百度搜索稳定性问题分析的故事
- 三入职场!你可以从我身上学到这些(附毕业Vlog)
- 地理空间搜索:kd树的实现原理
- Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"
- Go novice exploration road 2
- [Visio]平行四边形在Word中模糊问题解决
猜你喜欢

Geospatial search - > R tree index

Possible problems when idea encounters errors occurred while compiling module (solved)

架构师需要具备的能力

Zhangxiaobai's road to penetration (7) -sql injection detailed operation steps -union joint query injection

Penetration tool environment - installing sqli labs in centos7 environment

(2) Pyqt5 tutorial -- > using qtdesigner to separate interface code

架构师必备的七种能力

2021-09-22

Total number of MySQL statistics, used and unused

It is extraordinary to make a move, which is very Oracle!
随机推荐
Idea2017 how to set not to automatically open a project at startup
First acquaintance with CANopen
Oracle trigger error report table or view does not exist
Jeecgboot startup popup configuration is still incorrect
mysql FIND_ IN_ Set function
PHP numeric array sorting and associative array sorting
(5) Pyqt5 ---- another method of connecting signals and slots
Elemntui's select+tree implements the search function
Go novice exploration road 2
2021-09-02
STM32 在flash中存储float数据
2021-10-21
STM32 stores float data in flash
Laravel is not logged in and cannot access the background by entering the URL
利用cmd(命令提示符)安装mysql&&配置环境
The amount is verified, and two zeros are spliced by integers during echo
架构师必备的七种能力
重装cuda/cudnn/pytorch
Error while sending STMT_ PREPARE packet. PID=29294
按权重随机选择[前缀和+二分+随机target]