随机置矩阵列为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.]])
相关推荐
这里有鱼汤3 小时前
小白必看:QMT里的miniQMT入门教程
后端·python
TF男孩13 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
该用户已不存在18 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
站大爷IP20 小时前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
用户8356290780511 天前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
c8i1 天前
python中类的基本结构、特殊属性于MRO理解
python
liwulin05061 天前
【ESP32-CAM】HELLO WORLD
python
Doris_20231 天前
Python条件判断语句 if、elif 、else
前端·后端·python
Doris_20231 天前
Python 模式匹配match case
前端·后端·python
这里有鱼汤1 天前
Python量化实盘踩坑指南:分钟K线没处理好,小心直接亏钱!
后端·python·程序员