随机置矩阵列为0[矩阵乘法pytorch版]

文章目录

  • [1. 举例:](#1. 举例:)
  • [2. python 代码](#2. python 代码)

1. 举例:

A = [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 ] , r a n d = [ 0 , 5 , 2 ] → A = [ 0 1 0 3 4 0 6 7 0 9 0 11 12 0 14 15 0 17 0 19 20 0 22 23 0 25 0 27 28 0 30 31 0 33 0 35 36 0 38 39 0 41 0 43 44 0 46 47 ] \begin{equation} A=\begin{bmatrix} 0&1&2&3&4&5&6&7\\\\ 8&9&10&11&12&13&14&15\\\\ 16&17&18&19&20&21&22&23\\\\ 24&25&26&27&28&29&30&31\\\\ 32&33&34&35&36&37&38&39\\\\ 40&41&42&43&44&45&46&47 \end{bmatrix},rand=[0,5,2]\to A=\begin{bmatrix} 0&1&0&3&4&0&6&7\\\\ 0&9&0&11&12&0&14&15\\\\ 0&17&0&19&20&0&22&23\\\\ 0&25&0&27&28&0&30&31\\\\ 0&33&0&35&36&0&38&39\\\\ 0&41&0&43&44&0&46&47 \end{bmatrix} \end{equation} A= 08162432401917253341210182634423111927354341220283644513212937456142230384671523313947 ,rand=[0,5,2]→A= 000000191725334100000031119273543412202836440000006142230384671523313947

2. python 代码

python 复制代码
import torch
import torch.nn as nn

torch.manual_seed(234)


class RandomPermute():
    def __init__(self, matrix, num):
        super(RandomPermute, self).__init__()
        self.matrix = matrix
        self.row, self.column = self.matrix.shape
        self.num = num

    def get_result(self):
        if self.num > self.column:
            set_num = self.column
        else:
            set_num = self.num
        my_permute = torch.randperm(self.column)[:set_num]
        my_ones = torch.eye(self.column)
        print(f"before:my_ones=\n{my_ones}")
        my_ones[:, my_permute] = 0
        print(f"after:my_ones=\n{my_ones}")
        print(f"matrix=\n{self.matrix}")
        print(f"my_permute={my_permute}")
        my_result = self.matrix @ my_ones
        print(f"my_result=\n{my_result}")
        return my_result


if __name__ == "__main__":
    run_code = 0
    a_matrix = torch.arange(48).reshape(6, 8).to(torch.float)
    set_num = 3
    my_rand_perm = RandomPermute(a_matrix, set_num)
    out_result = my_rand_perm.get_result()
  • 结果:
python 复制代码
before:my_ones=
tensor([[1., 0., 0., 0., 0., 0., 0., 0.],
        [0., 1., 0., 0., 0., 0., 0., 0.],
        [0., 0., 1., 0., 0., 0., 0., 0.],
        [0., 0., 0., 1., 0., 0., 0., 0.],
        [0., 0., 0., 0., 1., 0., 0., 0.],
        [0., 0., 0., 0., 0., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0., 1., 0.],
        [0., 0., 0., 0., 0., 0., 0., 1.]])
after:my_ones=
tensor([[0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 1., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 1., 0., 0., 0., 0.],
        [0., 0., 0., 0., 1., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 1., 0.],
        [0., 0., 0., 0., 0., 0., 0., 1.]])
matrix=
tensor([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11., 12., 13., 14., 15.],
        [16., 17., 18., 19., 20., 21., 22., 23.],
        [24., 25., 26., 27., 28., 29., 30., 31.],
        [32., 33., 34., 35., 36., 37., 38., 39.],
        [40., 41., 42., 43., 44., 45., 46., 47.]])
my_permute=tensor([0, 5, 2])
my_result=
tensor([[ 0.,  1.,  0.,  3.,  4.,  0.,  6.,  7.],
        [ 0.,  9.,  0., 11., 12.,  0., 14., 15.],
        [ 0., 17.,  0., 19., 20.,  0., 22., 23.],
        [ 0., 25.,  0., 27., 28.,  0., 30., 31.],
        [ 0., 33.,  0., 35., 36.,  0., 38., 39.],
        [ 0., 41.,  0., 43., 44.,  0., 46., 47.]])
相关推荐
晓131323 分钟前
第四章 OpenCV篇—图像梯度与边缘检测—Python
人工智能·python·opencv·计算机视觉·pycharm
蹦蹦跳跳真可爱58938 分钟前
Python----神经网络(《Going deeper with convolutions》论文解读和GoogLeNet网络)
网络·人工智能·pytorch·python·神经网络
reasonsummer1 小时前
【办公类-99-05】20250508 D刊物JPG合并PDF便于打印
python·pdf
坐吃山猪1 小时前
WebFlux与HttpStreamable关系解析
python
soso(找工作版1 小时前
【链表扫盲】FROM GPT
python·gpt·链表
编程武士2 小时前
python 闭包获取循环数据经典 bug
开发语言·python·bug
电院大学僧2 小时前
初学python的我开始Leetcode题8-4
python·算法·leetcode
hvinsion2 小时前
【Python开源】深度解析:一款高效音频封面批量删除工具的设计与实现
python·开源·音视频
坐吃山猪3 小时前
Python-JsonRPC
开发语言·python
小毛驴8503 小时前
Windows环境,Python实现对本机处于监听状态的端口,打印出端口,进程ID,程序名称
开发语言·windows·python