随机置矩阵列为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.]])
相关推荐
fydw_71513 分钟前
深入解析 Flask 命令行工具与 flask run命令的使用
后端·python·flask
先做个垃圾出来………19 分钟前
Flask中secret_key设置解析
后端·python·flask
weixin_5176621421 分钟前
DAY 20 奇异值SVD分解
python
西猫雷婶1 小时前
深度学习|pytorch基本运算-乘除法和幂运算
人工智能·pytorch·深度学习
YYXZZ。。1 小时前
PyTorch——线性层及其他层介绍(6)
pytorch·python·深度学习
哆啦A梦的口袋呀1 小时前
基于Python学习《Head First设计模式》第三章 装饰者模式
python·学习·设计模式
哆啦A梦的口袋呀1 小时前
基于Python学习《Head First设计模式》第五章 单件模式
python·学习·设计模式
love530love2 小时前
【笔记】Windows 下载并安装 ChromeDriver
人工智能·windows·笔记·python·深度学习
Dxy12393102162 小时前
DrissionPage 异常处理实战指南:构建稳健的网页自动化防线
运维·爬虫·python·自动化·drissionpage
chao_7893 小时前
链表题解——反转链表【LeetCode】
开发语言·python·算法