pytorch:如何将 3d 张量与 2d 张量相乘

标签 pytorch

我有一个 3D 张量 和一个 2D 张量 ,需要将它们相乘。例如,尺寸为:

three.shape = 4x100x700
two.shape = 4x100

输出形状应该是:

output.shape = 4x100x700

所以基本上,在 output[a,b] 中应该有 700 个标量,这些标量是通过将 two[a,b] 中的所有 700 个标量与单个标量相乘来计算的来自 two[a,b] 的标量。

最佳答案

您可以简单地向two添加一个额外的维度:

output = three * two.unsqueeze(-1)

还有其他语法,例如:

output = three * two[..., None]

关于pytorch:如何将 3d 张量与 2d 张量相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62559382/

相关文章:

tensorflow - 寻找与 Pytorch GRU 功能等效的 TensorFlow

python - 对于语言模型微调(BERT 通过 Huggingface Transformers),输入文件的格式究竟应该如何设置?

python - pytorch model.parameter的形状与模型中的定义不一致

python - 如何将不规则间隔的 x 和 y 坐标张量平均到具有特定单元大小的网格中?

python-3.x - pytorch 中的 apply(fn) 函数如何与没有 return 语句作为参数的函数一起工作?

python - 为什么在使用 Deep Q 学习时会弹出此错误?

python - AttributeError : module 'torch' has no attribute '_six' . Pytorch 中的 Bert 模型

nlp - 如何在 PyTorch 中将句子长度批量转换为掩码?

python - 使用相同大小的索引张量拆分 torch 张量

neural-network - 是否可以使用未见过的数据(与输入数据不同的数据)上的损失函数来训练神经网络?