[DL] Pytorch Tensor vs Numpy Array

SujiKim-hattoo·2025년 8월 10일
1

DL Basic

목록 보기
2/4

* The following content is written in English, then in Korean.
* 아래 글은 영어 -> 한국어 순으로 작성되어 있습니다.

1. Definition

1.1 PyTorch Tensor

  • import torch; torch.tensor()
  • The fundamental data structure in PyTorch.
  • An n-dimensional array optimized for deep learning operations.
  • Primary Use: Building and training deep learning models.

1.2 NumPy Array

  • import numpy as np; np.array()
  • The fundamental data structure for numerical computing in Python.
  • An n-dimensional array for general-purpose scientific computation.
  • Primary Use: Scientific computing (data analysis, linear algebra, etc.).

2. Comparison

2.1 Common Points

  • Represent n-dimensional numerical data.
  • Homogeneous (store elements of a single data type).
  • Easily converted between each other (.numpy(), torch.from_numpy()).

2.2 Differences

  • Hardware Acceleration (GPU Usage):
    - Tensor: Optimized for GPU operations (accelerates deep learning).
    - Array: Limited to CPU operations.
  • Automatic Differentiation:
    - Tensor: Built-in autograd engine (essential for training neural networks).
    - Array: No built-in autograd functionality.
  • Ecosystem Integration:
    - Tensor: Integrated with the PyTorch ecosystem (e.g., torch.nn, optimizers).
    - Array: Integrated with the scientific computing ecosystem (e.g., Scikit-learn, Pandas).

3. Conclusion

Use PyTorch Tensors for deep learning models and GPU-accelerated tasks; use NumPy Arrays for general-purpose scientific computing and data analysis.


1. 정의

1.1 PyTorch Tensor

  • import torch; torch.tensor()
  • 파이토치의 기본 데이터 구조.
  • 딥러닝 연산에 최적화된 N차원 배열.
  • 주로 딥러닝 모델 구축 및 훈련에 사용

1.2 NumPy Array

  • import numpy as np; np.array()
  • 파이썬 수치 연산의 기본 데이터 구조.
  • 범용 과학 계산을 위한 N차원 배열.
  • 주로 계산 과학(scientific computic - 데이터 분석, 선형대수 등)에 사용

2. 비교

2.1 공통점

  • N차원 수치 데이터 표현.
  • 동일한 데이터 타입만 저장 (Homogeneous).
  • 상호 변환 용이 (.numpy(), torch.from_numpy()).

2.2 차이점

  • 하드웨어 가속 (GPU 사용 여부):
    - Tensor: CPU 연산도 가능하지만 GPU 연산도 가능(GPU 연산에 최적화 -> 딥러닝 가속).
    - Array: CPU 연산에 제한됨.
  • 자동 미분:
    - Tensor: 자동 미분 기능(autograd) 내장 (신경망 학습에 필수).
    - Array: 자동 미분 기능 없음.
  • 생태계:
    - Tensor: PyTorch 생태계와 연결 (torch.nn, 옵티마이저 등).
    - Array: Scientific 생태계와 연결 (Scikit-learn, Pandas 등).

3. 결론

  • 딥러닝 모델과 GPU 가속 작업 -> PyTorch Tensor
  • 일반적인 Sceintific computing 및 데이터 분석 -> NumPy Array를 사용


Reference Link 참고 링크
https://www.kaggle.com/code/amirmotefaker/pytorch-vs-numpy
https://www.geeksforgeeks.org/deep-learning/pytorch-tensor-vs-numpy-array/

profile
On a journey to develop myself and my world.

0개의 댓글