pytorch - 在pytorch中合并两个张量

标签 pytorch

张量a:

tensor([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]])

张量 b:

tensor([4,4,4,4])

问题1:

如何合并两个张量并得到结果c:

tensor([[1, 2, 3, 4],
        [1, 2, 3, 4],
        [1, 2, 3, 4],
        [1, 2, 3, 4]])

问题2:如何将张量c除以得到原始的a和b。

最佳答案

问题 1:合并两个张量 -

torch.cat((a, b.unsqueeze(1)), 1)
>>> tensor([[1, 2, 3, 4],
            [1, 2, 3, 4],
            [1, 2, 3, 4],
            [1, 2, 3, 4]])

首先,我们使用torch.unsqueezeb张量中添加单个dim以匹配要连接的adim。然后使用torch.cat连接张量 ab

问题2:

a = c[:][:,:-1]
a
>>> tensor([[1, 2, 3],
            [1, 2, 3],
            [1, 2, 3],
            [1, 2, 3]])

b = c[:][:,-1:].squeeze(1)
b
>>> tensor([4, 4, 4, 4])

关于pytorch - 在pytorch中合并两个张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59558460/

相关文章:

python - "AssertionError: Torch not compiled with CUDA enabled"尽管升级到 CUDA 版本

python-2.7 - 在使用 Google Colab 中的所有可用 RAM 后,您的 session 崩溃了

python - PyTorch 在用线性模型逼近平方函数时不收敛

python - Pytorch:如何从 2D 矢量/图像预测 1D 矢量?

python - PyTorch:检查模型准确性导致 "TypeError: ' bool' 对象不可迭代。”

deep-learning - 如何在pytorch中正确使用分布式数据并行

linux - 通过 SSH 连接到 Docker?还是 SSH 上的 docker?我需要指挥

python-3.x - 元素明智计算破坏了 autograd

python - 什么是非动态可量化线性?

python - 如何在 pytorch 中逐步发展神经网络?