千锋教育-做有情怀、有良心、有品质的职业教育机构

手机站
千锋教育

千锋学习站 | 随时随地免费学

千锋教育

扫一扫进入千锋手机站

领取全套视频
千锋教育

关注千锋学习站小程序
随时随地免费学习课程

当前位置:首页  >  技术干货  > torch.mm详解

torch.mm详解

来源:千锋教育
发布人:xqq
时间: 2023-11-21 04:22:15 1700511735

一、torch.mm的基础知识

torch.mm(input, mat2, out=None)函数是计算两个tensor的矩阵乘法。其中,input是第一个矩阵,mat2是第二个矩阵。如果指定out,则结果会被写入该输出张量。

该函数实现了普通矩阵乘法,它也是torch.matmul()函数的一种特殊情况。不同之处在于,torch.matmul()可以广义地计算不同形状的张量的乘积。

下面是一个简单的例子:


import torch

# 生成两个随机矩阵
x = torch.rand(3, 4)
y = torch.rand(4, 5)

# 计算矩阵乘积
z = torch.mm(x, y)
print(z)

输出结果:


tensor([[0.8313, 0.3308, 0.8844, 1.1625, 0.6847],
        [1.1002, 1.0427, 1.2463, 1.4015, 1.1074],
        [0.7341, 0.7045, 0.8077, 0.9469, 0.6974]])

这里,我们先生成了两个随机矩阵xy,它们的形状分别是(3,4)和(4,5)。然后,使用torch.mm()计算它们的矩阵乘积z

二、torch.mm的高级用法

import torch batch_size, input_channels, input_width = 10, 5, 100 output_channels, kernel_width = 8, 3 # 生成随机的输入,卷积核和偏置项 input_tensor = torch.randn(batch_size, input_channels, input_width) weight_tensor = torch.randn(output_channels, input_channels * kernel_width) bias_tensor = torch.randn(output_channels) # 通过reshape操作将输入和卷积核转换为二维矩阵 input_matrix = input_tensor.view(batch_size, input_channels, -1).transpose(1, 2).contiguous().view(batch_size * input_width, input_channels) weight_matrix = weight_tensor.view(output_channels, -1) # 计算矩阵乘积和偏置项 output_matrix = torch.mm(input_matrix, weight_matrix.t()) + bias_tensor output_tensor = output_matrix.view(batch_size, output_channels, -1).transpose(1, 2).contiguous() print(output_tensor.shape)

这里我们先生成了所有随机输入,卷积核和偏置项,然后通过reshape操作将输入和卷积核转换为二维矩阵。然后,我们可以使用torch.mm()计算矩阵乘积,并将结果reshape为输出tensor的形状。

import torch import torch.nn.functional as F import numpy as np import cv2 # 读取一张图片 img = cv2.imread('test.jpg').astype(np.float32) / 255. img = torch.from_numpy(img.transpose((2, 0, 1))[None]) # 缩放图片 scale_factor = 2 h, w = img.shape[2:] new_h, new_w = int(h * scale_factor), int(w * scale_factor) src_h, src_w = np.arange(new_h), np.arange(new_w) dst_h, dst_w = np.zeros(new_h), np.zeros(new_w) dst_h[1:-1] = (src_h[1:-1] + 0.5) / scale_factor - 0.5 dst_w[1:-1] = (src_w[1:-1] + 0.5) / scale_factor - 0.5 # 构造网格坐标 grid_x, grid_y = np.meshgrid(dst_w, dst_h) # (new_h, new_w) grid_x = np.clip(grid_x, 0, w - 1) grid_y = np.clip(grid_y, 0, h - 1) y_0, x_0 = np.floor(grid_y).astype(np.int32), np.floor(grid_x).astype(np.int32) y_1, x_1 = y_0 + 1, x_0 + 1 dy, dx = grid_y - y_0, grid_x - x_0 # 通过torch.mm()实现双线性插值 I00 = img[..., y_0, x_0] I01 = img[..., y_0, x_1] I10 = img[..., y_1, x_0] I11 = img[..., y_1, x_1] img_new = (1 - dx) * (1 - dy) * I00 + dx * (1 - dy) * I01 + (1 - dx) * dy * I10 + dx * dy * I11 # 显示原图和新图 img = img.numpy()[0].transpose((1, 2, 0)) img_new = img_new.numpy()[0].transpose((1, 2, 0)) cv2.imshow('input', img) cv2.imshow('output', img_new) cv2.waitKey(0)

这里我们首先读取一张图片,并将它转换为tensor格式。然后,我们使用np.meshgrid()函数构造目标网格,并使用np.floor()函数和np.clip()函数计算出每个网格对应的源像素位置。接着,我们可以使用torch.mm()函数计算双线性插值的结果。img_new就是缩放后的新图。

三、总结

本文详细介绍了torch.mm()函数的基础知识和高级用法。在深度学习中,我们经常使用矩阵乘法来实现一些复杂的操作,如卷积运算和双线性插值。希望本文可以为大家提供一些帮助。

tags: torchmm
声明:本站稿件版权均属千锋教育所有,未经许可不得擅自转载。
10年以上业内强师集结,手把手带你蜕变精英
请您保持通讯畅通,专属学习老师24小时内将与您1V1沟通
免费领取
今日已有369人领取成功
刘同学 138****2860 刚刚成功领取
王同学 131****2015 刚刚成功领取
张同学 133****4652 刚刚成功领取
李同学 135****8607 刚刚成功领取
杨同学 132****5667 刚刚成功领取
岳同学 134****6652 刚刚成功领取
梁同学 157****2950 刚刚成功领取
刘同学 189****1015 刚刚成功领取
张同学 155****4678 刚刚成功领取
邹同学 139****2907 刚刚成功领取
董同学 138****2867 刚刚成功领取
周同学 136****3602 刚刚成功领取
相关推荐HOT