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

相关推荐
Eloudy20 分钟前
一键构建 pytorch cpp lib 前端和 wheel
人工智能·pytorch·gpu
FriendshipT2 小时前
Ultralytics:解读 YOLO26 知识蒸馏
人工智能·pytorch·python·深度学习·yolo
hhzz1 天前
《深度学习框架PyTorch入门与实践》系列:04-nn模块详解:用积木搭建神经网络
pytorch·深度学习·神经网络
阳光开朗男孩1 天前
Pytorch的安装与配置
人工智能·pytorch·python
等一朵映山红1 天前
动态图 vs 静态图:PyTorch 与 TensorFlow 架构底层对比
人工智能·pytorch·python
老马聊技术1 天前
Pytorch深度学习环境配置与测试
人工智能·pytorch·python
hhzz2 天前
《深度学习框架PyTorch入门与实践》系列:03-autograd自动微分:反向传播的引擎
人工智能·pytorch·深度学习
青春不败 177-3266-05202 天前
CNN-Transformer:PyTorch遥感【含无人机】影像的分类、检测、语义分割和点云分类
pytorch·目标检测·cnn·transformer·生态学·遥感·地物分类
Lee_jerome2 天前
python神经网络编程入门(二十七)——RNN IMBD搭建情感分类器与基础训练
pytorch·rnn·深度学习·gru·词嵌入·情感分析·imdb
ZHOU_WUYI2 天前
7. fastwam 模型 _predict_action_noise_with_cache部分
人工智能·pytorch·深度学习