当前位置:网站首页>torch.tensor拼接与list(tensors)
torch.tensor拼接与list(tensors)
2022-06-25 12:10:00 【燕策西】
tensor&list[tensors]
Construct list(tensors)
创建一个包含张量的列表,以及2个张量如下:
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)
堆叠之前对stack函数做一点说明。Stack操作,先升维,再扩增。参考stack与cat。对张量进行堆叠操作,要求张量的shape一致:
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]]])
但是,如果要使用stack替代上述cat操作,则会报错,因为stack要求两个输入的shape完全相同,而cat允许非拼接部分不同。除了torch.cat以外,也可以使用list.append完成以上操作。
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]])]
注意到,list.append()不仅可以堆叠同形状的tensors,而且可以容纳不同shape的tensor,是一个tensor容器!但是本人在使用过程中出现了以下 low-level 错误,请读者谨防:
d = []
d.append(a), d.append(b)
print(d)
e = a.append(b)
print(e) # 空的!
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 过程中不会返回新的东西,只能从list_x中获取。
完结,撒花。
边栏推荐
- Some fields are ignored in tp6 query
- Zhengzheng e-commerce source code -- Zhengzheng advertising e-commerce system development source code sharing
- 2021-09-02
- (6) Pyqt5--- > window jump (registration login function)
- Upgrade opsenssh to 8.8p1
- JS enter three integers a, B and C, and sort them from large to small (two methods)
- PHP multidimensional array sorting
- MySQL common interview questions
- High performance + million level Excel data import and export
- Huile optimization system -- sharing of secondary development source code of huile optimization app system
猜你喜欢

K8s, docker compose install MySQL 8.0.18
![Select randomly by weight [prefix and + dichotomy + random target]](/img/84/7f930f55f8006a4bf6e23ef05676ac.png)
Select randomly by weight [prefix and + dichotomy + random target]

What is the primordial universe

Matlab simulation of m-sequence

Total number of MySQL statistics, used and unused

高性能负载均衡架构如何实现?

An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down

MySQL common interview questions

ECSHOP quickly purchases goods, simplifies the shopping process, and improves the user experience through one-step shopping

微信全文搜索技术优化
随机推荐
Laravel task scheduling
JS array length is defined
K8s, docker compose install MySQL 8.0.18
PHP replaces the key of a two-dimensional array with a specified element value
Negative sample image used in yolov5 training
JS function exercises
Error while sending STMT_ PREPARE packet. PID=29294
JS enter three integers a, B and C, and sort them from large to small (two methods)
Laravel excel export
Navicat premium view password scheme
Repair the error that ECSHOP background orders prompt insufficient inventory when adding goods. Please reselect
Methods of strings in JS charat(), charcodeat(), fromcharcode(), concat(), indexof(), split(), slice(), substring()
Array reorder based on a field
ECSHOP commodity page multi-attribute batch purchase plug-ins ECSHOP wholesale plug-ins multi-attribute order placing, multi-attribute batch purchase of commodities
Zhengzheng e-commerce source code -- Zhengzheng advertising e-commerce system development source code sharing
Penetration tool environment - Installation of sqlmap
Spicy food advertising e-commerce system development function and spicy food advertising e-commerce app system development source code sharing
An example of using dynamic datalist
3+1保障:高可用系统稳定性是如何炼成的?
PHP numeric array sorting and associative array sorting