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).

相关推荐
我是哈哈hh1 天前
【Python数据分析】Numpy总结
开发语言·python·数据挖掘·数据分析·numpy·python数据分析
koo3641 天前
pytorch深度学习笔记5
pytorch·笔记·深度学习
福大大架构师每日一题2 天前
PyTorch v2.9.1 发布:重要 Bug 修复与性能优化详解
人工智能·pytorch·bug
All The Way North-2 天前
PyTorch SmoothL1Loss 全面解析:数学定义、梯度推导、API 规范与 logits 误用纠正
pytorch·深度学习·机器学习·smooth l1损失函数·回归损失函数
冬虫夏草19932 天前
使用householder反射推广ROPE相对位置编码
人工智能·pytorch·python
LCG米2 天前
从训练到部署:基于PyTorch与TensorFlow Lite的端侧AI花卉分类系统完整指南
人工智能·pytorch·tensorflow
技术小黑2 天前
Pytorch学习系列07 | VGG-16算法实现马铃薯病害识别
pytorch·深度学习·神经网络·cnn
李昊哲小课2 天前
WSL Ubuntu 安装GPU版 tensorflow pytorch
pytorch·ubuntu·tensorflow
编程设计3662 天前
Numpy核心功能简易代码
numpy
All The Way North-2 天前
PyTorch 二分类损失函数详解:BCELoss vs BCEWithLogitsLoss 最佳实践指南
人工智能·pytorch·深度学习·机器学习·二分类损失函数