当前位置: 首页 > news >正文

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)

import numpy as np
arr_1D = np.array([1,2,3])
print(arr_1D)


Create NumPy ndarray (2D array)

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)

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

http://www.lryc.cn/news/386899.html

相关文章:

  • 【面试系列】数据科学家 高频面试题及详细解答
  • mysql是什么
  • 【软件工程】【22.04】p1
  • 简单说下GPT-4
  • 力扣第一道困难题《3. 无重复字符的最长子串》,c++
  • 【ai】tx2 nx :ubuntu查找NvInfer.h 路径及哪个包、查找符号
  • C++ 运算符的优先级和结合性表
  • MySQL中SQL语句的执行过程详解
  • 文心一言4.0免费使用
  • Mongodb安装与配置
  • Java校园跑腿小程序校园代买帮忙外卖源码社区外卖源码
  • MySQL高级-MVCC-基本概念(当前读、快照读)
  • kubernetes给指定用户分配调用k8s的api权限
  • 无人机的弱点和限制
  • ElementUI的基本搭建
  • Modbus TCP与TCP/IP协议间的差异与应用场景
  • Linux Doxygen快速生成文档
  • MobPush REST API的推送 API之批量推送
  • Arthas快速入门
  • python系列30:各种爬虫技术总结
  • PHP和phpSpider:如何应对反爬虫机制的封锁?
  • 学生宿舍管理系统
  • 一分钟彻底掌握Java迭代器Iterator
  • 第三十七篇——麦克斯韦的妖:为什么要保持系统的开放性?
  • 青岛网站建设一般多少钱
  • Linux 进程状态:TASK_INTERRUPTIBLE 和 TASK_UNINTERRUPTIBLE
  • vue3使用vant4的列表vant-list点击进入详情自动滚动到对应位置,踩坑日记(一天半的踩坑经历)
  • Linux的fwrite函数
  • python udsoncan 详解
  • 基于自组织长短期记忆神经网络的时间序列预测(MATLAB)