文章目录
- 1. 举例:
- 2. python 代码
- 3. 邻接矩阵
1. 举例:
在深度学习过程中,我们经常会用到掩码矩阵,比如我们有一个矩阵A表示如下,希望得到矩阵B,在矩阵A的非零位置表示为1,零位置表示为0,
A = [ 2 3 0 0 0 1 3 4 2 0 ] → B = [ 1 1 0 0 0 1 1 1 1 0 ] \begin{equation} A=\begin{bmatrix} 2&3&0&0&0\\\\ 1&3&4&2&0 \end{bmatrix}\to B=\begin{bmatrix} 1&1&0&0&0\\\\ 1&1&1&1&0 \end{bmatrix} \end{equation} A= 2133040200 →B= 1111010100
2. python 代码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName :MatrixNoneZero2OnesLike.py
# @Time :2024/12/22 16:20
# @Author :Jason Zhang
import torch
import torch.nn as nnclass MatrixNoneZero2OnesLike(object):def __init__(self, in_matrix):self.in_matrix = in_matrixself._result = torch.zeros_like(self.in_matrix)@propertydef result(self):my_result = torch.zeros_like(self.in_matrix)my_result_bool = self.in_matrix.to(torch.bool)self._result = my_result.masked_fill(my_result_bool, 1)return self._resultif __name__ == "__main__":run_code = 0matrix_mya = torch.tensor([[2, 3, 0, 0, 0], [1, 3, 4, 2, 0]])my_matrix_test = MatrixNoneZero2OnesLike(matrix_mya)my_matrix_test_result = my_matrix_test.resultprint(f"matrix_mya=\n{matrix_mya}")print(f"my_matrix_test_result=\n{my_matrix_test_result}")
- 结果:
matrix_mya=
tensor([[2, 3, 0, 0, 0],[1, 3, 4, 2, 0]])
my_matrix_test_result=
tensor([[1, 1, 0, 0, 0],[1, 1, 1, 1, 0]])
3. 邻接矩阵
B = [ 1 1 0 0 0 1 1 1 1 0 ] → C = [ [ 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ] [ 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 ] ] \begin{equation} B=\begin{bmatrix} 1&1&0&0&0\\\\ 1&1&1&1&0 \end{bmatrix}\to C= \begin{bmatrix} \begin{bmatrix} 1&1&0&0&0\\\\ 1&1&0&0&0\\\\ 0&0&0&0&0\\\\ 0&0&0&0&0\\\\ 0&0&0&0&0 \end{bmatrix}\\\\ \begin{bmatrix} 1&1&1&1&0\\\\ 1&1&1&1&0\\\\ 1&1&1&1&0\\\\ 0&0&0&0&0\\\\ 0&0&0&0&0 \end{bmatrix} \end{bmatrix} \end{equation} B= 1111010100 →C= 1100011000000000000000000 1110011100111001110000000
- python code
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName :MatrixNoneZero2OnesLike.py
# @Time :2024/12/22 16:20
# @Author :Jason Zhang
import torch
import torch.nn as nnclass MatrixNoneZero2OnesLike(object):def __init__(self, in_matrix):self.in_matrix = in_matrixself._result = torch.zeros_like(self.in_matrix)@propertydef result(self):my_result = torch.zeros_like(self.in_matrix)my_result_bool = self.in_matrix.to(torch.bool)self._result = my_result.masked_fill(my_result_bool, 1)return self._resultclass AdjacencyMatrix(object):def __init__(self, in_matrix):self.in_matrix = in_matrixself.row, self.column = self.in_matrix.shapeself._result = torch.zeros((self.row, self.column, self.column))@propertydef result(self):self._result = torch.zeros((self.row, self.column, self.column))matrix_dim1 = torch.unsqueeze(self.in_matrix, dim=1)matrix_bmm = torch.bmm(matrix_dim1.transpose(-1, -2), matrix_dim1)self._result = matrix_bmmreturn self._resultif __name__ == "__main__":run_code = 0matrix_mya = torch.tensor([[2, 3, 0, 0, 0], [1, 3, 4, 2, 0]])my_matrix_test = MatrixNoneZero2OnesLike(matrix_mya)my_matrix_test_result = my_matrix_test.resultmy_adjacency = AdjacencyMatrix(my_matrix_test_result)my_adjacency_result = my_adjacency.resultprint(f"matrix_mya=\n{matrix_mya}")print(f"my_matrix_test_result=\n{my_matrix_test_result}")print(f"my_adjacency_result=\n{my_adjacency_result}")
- 结果:
matrix_mya=
tensor([[2, 3, 0, 0, 0],[1, 3, 4, 2, 0]])
my_matrix_test_result=
tensor([[1, 1, 0, 0, 0],[1, 1, 1, 1, 0]])
my_adjacency_result=
tensor([[[1, 1, 0, 0, 0],[1, 1, 0, 0, 0],[0, 0, 0, 0, 0],[0, 0, 0, 0, 0],[0, 0, 0, 0, 0]],[[1, 1, 1, 1, 0],[1, 1, 1, 1, 0],[1, 1, 1, 1, 0],[1, 1, 1, 1, 0],[0, 0, 0, 0, 0]]])