随机置矩阵列为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.]])
相关推荐
用户813441182361几秒前
Python基础
python
ZHOU_WUYI21 分钟前
旋转位置编码 (2)
pytorch·python·深度学习
qq_2739002326 分钟前
AF3 squeeze_features函数解读
人工智能·pytorch·深度学习·生物信息学
程序员~小强41 分钟前
让知识触手可及!基于Neo4j的机械设备知识图谱问答系统
人工智能·python·django·知识图谱·neo4j
DanCheng-studio1 小时前
智科 机器学习毕业设计题目指导
python·毕业设计·毕设
java1234_小锋2 小时前
一周学会Flask3 Python Web开发-SQLAlchemy定义数据库模型
python·flask·sqlalchemy·flask3
Light602 小时前
CSnakes vs Python.NET:跨语言集成的巅峰对决与架构解密
python·性能优化·.net·跨语言集成·双向互操作
gallonyin2 小时前
免root运行python保活守护进程supervisor
linux·开发语言·python
lisw052 小时前
【PyCharm】Python和PyCharm的相互关系和使用联动介绍
ide·python·pycharm
Wis4e2 小时前
基于PyTorch的深度学习5——神经网络工具箱
pytorch·深度学习·神经网络