PyTorch与Tensorflow 部分代码转换
ssim代码转换,自更改(有错误)
1.torch.unsqueeze()----tf.expand_dims()矩阵扩展
(1)torch.unsqueeze()
torch.unsqueeze(input, dim, out=None)
作用:扩展维度
返回一个新的张量,对输入的既定位置插入维度 1
注意: 返回张量与输入张量共享内存,所以改变其中一个的内容会改变另一个。如果dim为负,则将会被转化dim+input.dim()+1
参数:
- tensor (Tensor) – 输入张量
- dim (int) – 插入维度的索引
- out (Tensor, optional) – 结果张量
(2)tf.expand_dims()
tf.expand_dims(input, dim, name=None)
Args:
- input: A Tensor.
- dim: A Tensor. Must be one of the following types: int32, int64. 0-D (scalar). Specifies the dimension index at which to expand the shape of input.
- name: A name for the operation (optional).
Returns:
A Tensor. Has the same type as input. Contains the same data as input, but its shape has an additional dimension of size 1 added.
2.torch.mm-----
(1)torch.mm
torch.mm(input, mat2, out=None) → Tensor
#对矩阵imput和mat2执行矩阵乘法。 如果input为(n x m)张量,则mat2为(m x p)张量,out将为(n x p)张量。
#官方提示此功能不广播。有关广播的矩阵乘法,请参见torch.matmul()。
#example
>>> mat1 = torch.randn(2, 3)
>>> mat2 = torch.randn(3, 3)
>>> torch.mm(mat1, mat2)
tensor([[ 0.4851, 0.5037, -0.3633],
[-0.0760, -3.6705, 2.4784]])