随机置矩阵列为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.]])
相关推荐
好开心啊没烦恼几秒前
Python 数据分析:计算,分组统计1,df.groupby()。听故事学知识点怎么这么容易?
开发语言·python·数据挖掘·数据分析·pandas
lljss20201 小时前
Python11中创建虚拟环境、安装 TensorFlow
开发语言·python·tensorflow
空中湖1 小时前
tensorflow武林志第二卷第九章:玄功九转
人工智能·python·tensorflow
CodeCraft Studio2 小时前
CAD文件处理控件Aspose.CAD教程:使用 Python 将绘图转换为 Photoshop
python·photoshop·cad·aspose·aspose.cad
Python×CATIA工业智造4 小时前
Frida RPC高级应用:动态模拟执行Android so文件实战指南
开发语言·python·pycharm
onceco4 小时前
领域LLM九讲——第5讲 为什么选择OpenManus而不是QwenAgent(附LLM免费api邀请码)
人工智能·python·深度学习·语言模型·自然语言处理·自动化
狐凄5 小时前
Python实例题:基于 Python 的简单聊天机器人
开发语言·python
悦悦子a啊6 小时前
Python之--基本知识
开发语言·前端·python
笑稀了的野生俊8 小时前
在服务器中下载 HuggingFace 模型:终极指南
linux·服务器·python·bash·gpu算力
Naiva8 小时前
【小技巧】Python+PyCharm IDE 配置解释器出错,环境配置不完整或不兼容。(小智AI、MCP、聚合数据、实时新闻查询、NBA赛事查询)
ide·python·pycharm