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

相关推荐
Victory_orsh11 小时前
“自然搞懂”深度学习(基于Pytorch架构)——010203
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
Geoking.13 小时前
PyTorch 中 model.eval() 的使用与作用详解
人工智能·pytorch·python
nn在炼金13 小时前
图模式分析:PyTorch Compile组件解析
人工智能·pytorch·python
执笔论英雄13 小时前
【大模型训练】zero2 梯度分片
pytorch·python·深度学习
化作星辰18 小时前
深度学习_原理和进阶_PyTorch入门(2)后续语法2
pytorch·深度学习·学习
机器学习ing.1 天前
U-Net保姆级教程:从原理到医学细胞分割实战(PyTorch版)!
人工智能·pytorch·python·深度学习·机器学习
Dev7z2 天前
基于ResNet50和PyTorch的猫狗图像分类系统设计与实现
人工智能·pytorch·分类
天地之于壹炁兮2 天前
PyTorch:AI时代的深度学习利器
pytorch
Francek Chen2 天前
【自然语言处理】预训练04:预训练word2vec
人工智能·pytorch·深度学习·自然语言处理·word2vec
skywalk81632 天前
老显卡老cpu用vllm推理大模型失败Intel(R) Xeon(R) CPU E5-2643 v2
人工智能·pytorch·python·vllm