Numpy array和Pytorch tensor的区别

1.Numpy array和Pytorch tensor的区别

笔记来源:

1.Comparison between Pytorch Tensor and Numpy Array

2.numpy.array

4.Tensors for Neural Networks, Clearly Explained!!!

5.What is a Tensor in Machine Learning?

1.1 Numpy Array

Numpy array can only hold elements of a single data type.

Create NumPy ndarray (1D array)

python 复制代码
import numpy as np
arr_1D = np.array([1,2,3])
print(arr_1D)

Create NumPy ndarray (2D array)

python 复制代码
import numpy as np
arr_2D = np.array([[1,2,3],[1,2,3],[1,2,3]])
print(arr_2D)

Create NumPy ndarray (3D array)

python 复制代码
import numpy as np
arr_3D = np.array([[[1,2,3],[1,2,3],[1,2,3],],[[1,2,3],[1,2,3],[1,2,3],],[[1,2,3],[1,2,3],[1,2,3]]])
print(arr_3D)

1.2 Pytorch Tensor

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.

Pytorch tensors are similar to numpy arrays, but can also be operated on CUDA-capable Nvidia GPU.



0-dimensional Tensor

1-dimensional Tensor

2-dimensional Tensor

n-dimensional Tensor

1.3 Difference

1.Numpy arrays are mainly used in typical machine learning algorithms (such as k-means or Decision Tree in scikit-learn) whereas pytorch tensors are mainly used in deep learning which requires heavy matrix computation.

2.The numpy arrays are the core functionality of the numpy package designed to support faster mathematical operations. Unlike python's inbuilt list data structure, they can only hold elements of a single data type. Library like pandas which is used for data preprocessing is built around the numpy array. Pytorch tensors are similar to numpy arrays, but can also be operated on CUDA-capable Nvidia GPU.The biggest difference between a numpy array and a PyTorch Tensor is that a PyTorch Tensor can run on either CPU or GPU.

3.Unlike numpy arrays, while creating pytorch tensor, it also accepts two other arguments called the device_type (whether the computation happens on CPU or GPU) and the requires_grad (which is used to compute the derivatives).

相关推荐
听风吹等浪起9 小时前
第26章:ViT+InceptionDW+FocalLoss改进|CT骨折影像二分类完整Pytorch实现(含PyQt5推理GUI)
pytorch·qt·分类
songyuc13 小时前
【Manskill3】TableSceneBuilder学习笔记
pytorch·笔记·学习
黑十14 小时前
PyTorch 报 “no CUDA GPUs are available“ 怎么办?3 步排查
服务器·网络·人工智能·pytorch·python
tellmewhoisi19 小时前
深度学习28:PyTorch:参数更新方法(AdaGrad和RMSProp)
人工智能·pytorch·深度学习
燃烧的枫2 天前
Windows 下 YOLO11 Det 的 TensorRT C++ 部署
pytorch·windows·tensorrt·关键点识别·yolo11
ljt27249606612 天前
Python笔记(十一)--Numpy(一)
笔记·python·numpy
田里的水稻2 天前
FA_CNN卷积神经网络十八---感知机到多层感知机PyTorch
人工智能·pytorch·python
听风吹等浪起2 天前
第27章:EfficientNet全家桶|钢材表面6类缺陷分类完整Pytorch实现
pytorch·分类·数据挖掘
Ivanqhz2 天前
MoE + LLM 应用
数据库·人工智能·pytorch·python·深度学习·算法·1024程序员节