-
행렬연산을 위한 자료구조, Tensor, Torch 를 자바로 재구현 해보자!Computer Basis/Algorithms 2021. 10. 14. 13:11
본문은 아직 완성되지 않았음.
# 행렬연산을 위한 자료구조 Tensor, Torch
- 1차원 배열로 구성되어있다.
- offset, strides 의 개념으로 모든 것을 해결
- 전치행렬을 만들때 압도적인 성능
- 행렬의 곱셈연산은 O(n^2) 로 추정 (직접 구현하고 추후 수정예정)
# 크기가 n인 정방행렬을 전치행렬로 만들때 Brute-force 시간복잡도
- 3중포문, O(n^3)
# Tensor 구조로 전치행렬 시간복잡도
- O(1)
- 2차원 배열로 바꿀때, O(n^2) - 데이터 복사 시간
# Tensor.class
public class Tensor { public int[] tensor; public int offset; public int[] strides; public int rowLen; public int colLen; public int rowOriginLen; public int colOriginLen; public Tensor(int[][] arr) { this.rowLen = arr.length; this.rowOriginLen = arr.length; this.colLen = arr[0].length; this.colOriginLen = arr[0].length; this.tensor = new int[this.rowLen*this.colLen]; this.offset = 0; this.strides = new int[]{colLen,1}; for(int i=0, cnt=0; i<this.rowLen;i++){ for(int j=0;j<this.colLen;j++){ this.tensor[cnt++]=arr[i][j]; } } } public void printTensorStorage(){ System.out.print("tf.Storage["); for(int i=0; i<tensor.length; i++){ System.out.print(tensor[i]+" "); } System.out.println("]"); } @Override public String toString() { return getTensorToString(); } public String getTensorToString(){ int i=0; String result=""; int idx; for(i=0; i<rowLen;i++){ result+="("; for(int j=0; j<colLen; j++){ idx = this.offset + strides[0]*i +strides[1]*j; if(j==colLen-1 && i!=rowLen-1) result+=tensor[idx]+")\n"; else if(j==colLen-1 && i==rowLen-1) result+=tensor[idx]+""; else result+=tensor[idx]+" "; } } result+=")"; return result; } public int trickSwap(int a, int b){ return a; } public void slicing(int rowStart, int rowEnd, int colStart, int colEnd){ if(colEnd>colLen) colEnd=colOriginLen; if(rowEnd>rowLen) rowEnd=rowOriginLen; this.offset = strides[0]*(rowStart) +strides[1]*(colStart); this.colLen = colEnd-colStart; this.rowLen = rowEnd-rowStart; } public int[][] to2DArray(){ int i=0; int[][] result=new int[rowLen][colLen]; int idx; for(i=0; i<rowLen;i++){ for(int j=0; j<colLen; j++){ idx = this.offset + strides[0]*i +strides[1]*j; result[i][j]=tensor[idx]; } } return result; } public void transpose(){ strides[1]=trickSwap(strides[0],strides[0]=strides[1]); colLen = trickSwap(rowLen,rowLen=colLen); colOriginLen =colLen; rowOriginLen =rowLen; } }
# Test
public class TensorTest { public static void print2DArray(int[][] arr){ for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[0].length;j++){ System.out.print(arr[i][j]+ " "); } System.out.println(); } } public static void main(String[] args) { int[][] arr1 = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, }; Tensor tf = new Tensor(arr1); System.out.println("# This is a storage of tensor."); tf.printTensorStorage(); System.out.println("# Offset : "+tf.offset); System.out.println("# Strides : ("+tf.strides[0]+","+tf.strides[1]+")"); System.out.println("# This is tensor."); System.out.println(tf); //transpose System.out.println("# Transpose"); tf.transpose(); tf.printTensorStorage(); System.out.println("# Strides : ("+tf.strides[0]+","+tf.strides[1]+")"); System.out.println(tf); //slicing 1 tf.slicing(1,3,1,3); System.out.println("# Slicing(tf[1:3, 1:3])"); tf.printTensorStorage(); System.out.println("# SlicingOffset : "+tf.offset); System.out.println("# Strides : ("+tf.strides[0]+","+tf.strides[1]+")"); System.out.println("# len(row,col) : ("+tf.rowLen+","+tf.colLen+")"); System.out.println(tf); //slicing 2 tf.slicing(2,100,1,4); System.out.println("# Slicing(tf[2:100, 1:4])"); tf.printTensorStorage(); System.out.println("# SlicingOffset : "+tf.offset); System.out.println("# Strides : ("+tf.strides[0]+","+tf.strides[1]+")"); System.out.println("# len(row,col) : ("+tf.rowLen+","+tf.colLen+")"); System.out.println(tf); //transpose again System.out.println("# Transpose"); tf.transpose(); tf.printTensorStorage(); System.out.println("# Strides : ("+tf.strides[0]+","+tf.strides[1]+")"); System.out.println(tf); //to2DArray System.out.println("# 2D array Transformation"); int[][] arr = tf.to2DArray(); print2DArray(arr); } }
728x90'Computer Basis > Algorithms' 카테고리의 다른 글
[Algorithms] 정렬 알고리즘의 종류와 시간복잡도, 사용처 (+ Java 구현코드) (0) 2021.11.08 [Algorithms] 백준 DP 문제모음 (bottom-up 유형) (0) 2021.11.01 [Algorithms] 백준 DP 문제모음 (Top-down 유형) (0) 2021.10.30