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

相关推荐
@李思成2 小时前
动手学深度学习(Pytorch版)代码实践 -计算机视觉-40目标检测和边界框
pytorch·深度学习·计算机视觉
weixin_402939995 小时前
【深度学习】pytorch训练中的一个大坑
人工智能·pytorch·深度学习
Midsummer啦啦啦5 小时前
PyTorch中matmul函数的矩阵相乘原则和注意事项
人工智能·pytorch·矩阵
scdifsn8 小时前
动手学深度学习5.6 GPU-笔记&练习(PyTorch)
pytorch·笔记·python·深度学习
庄宝00711 小时前
【PYTORCH,TENSORFLOW环境配置,安装,自用代码】
人工智能·pytorch·python·深度学习·conda·numpy·virtualenv
Thetoicxdude16 小时前
[Day 21] 區塊鏈與人工智能的聯動應用:理論、技術與實踐
人工智能·numpy·web3.py
mkijhwert17 小时前
Pytorch之视频流猫狗识别
人工智能·pytorch·深度学习·神经网络·opencv·机器学习·tensorflow
CRTao17 小时前
pytorch库 03 基础知识
人工智能·pytorch·python
@我们的天空1 天前
【深度学习】python之人工智能应用篇--跨模态生成技术
人工智能·pytorch·python·深度学习·机器学习·tensorflow·transformer
salvation~1 天前
Inception_V2_V3_pytorch
人工智能·pytorch·python·深度学习·inceptionv3