paddle2.0之 paddle Tensor 第一集

这大概是全网最全的paddle Tensor 的文章吧【蜜汁自信】
摘要由CSDN通过智能技术生成


paddlepaddle2.0新的起航,是不是有新手小伙伴对这些很迷茫呢?

传说中的:可以new一个对象的python还没有到创建对象的能力。

听说对象在paddle的加持下已经是过去,只需百度和谷歌加上paddle的熟练操作,和paddle实现人机对话指日可待

作为一名paddle小白怎么办呢?

今天由三岁带大家一起开启paddle学习新梦想!

本文对应AIstudio地址:https://aistudio.baidu.com/aistudio/projectdetail/1275635

三岁白话paddle2.0系列历史地址

三岁白话paddle2.0系列第一话:起航新征程(项目):https://aistudio.baidu.com/aistudio/projectdetail/1270135
三岁白话paddle2.0系列第一话:起航新征程(csdn):https://blog.csdn.net/weixin_45623093/article/details/110198594

参考资料:

paddlepaddle官方文档:

https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-rc/guides/01_paddle2.0_introduction/basic_concept/tensor_introduction_cn.html

百度:https://www.baidu.com/s?wd=Tensor

pytorch官方文档:https://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html#sphx-glr-beginner-blitz-tensor-tutorial-py

Tensor->张量

在paddle中和大多数的深度学习框架一样Tensor是运算的基础,那么什么是Tensor呢?让我们一起来看看吧!

这是paddle提供的一种数据结构和python的几种内置结构类型有所不同,他更类似于C语言的多维数组,和Numpy的array相类似

我们可以非常方便的读取到位置上的内容,但是不能够轻易的给已经生成的Tensor添加成员或者生成维度(优缺点

所有的修改都需要通过新建在把数据处理后复制进去(paddle对此作了一定程度的封装,便于使用)

数学上的张量(图解)

在数学上张量就是向量这个大家就有数了,但也可能是多维的向量,向量的向量,向量的向量的向量,……反复套娃

paddle Tensor的创建

# 导入paddle
import paddle

创建类似于vector的1-D Tensor,其rank为1

创建一个向量维度是一

# 可通过dtype来指定Tensor数据类型,否则会创建float32类型的Tensor
rank_1_tensor = paddle.to_tensor([2.0, 3.0, 4.0], dtype='float64')
print(rank_1_tensor)
Tensor(shape=[3], dtype=float64, place=CPUPlace, stop_gradient=True,
       [2., 3., 4.])

结果解析

shape=[3]:一维长度为3;

dtype=float64:类型是64位的;

place=CPUPlace:使用的是cpu;

stop_gradient=True:不求导,不参加梯度更新;

[2., 3., 4.]内容是[2., 3., 4.]

创建一个标量

这里使用两种办法得到的结果是一样的!

int_0 = paddle.to_tensor(2)
int_1 = paddle.to_tensor([2])
print(f"整型标量:{int_0},{int_1}")
float_0 = paddle.to_tensor(2.0, dtype='float64')
float_1 = paddle.to_tensor([2.0], dtype='float64')
print(f"浮点型标量:{float_0},{float_1}")
整型标量:Tensor(shape=[1], dtype=int64, place=CPUPlace, stop_gradient=True,
       [2]),Tensor(shape=[1], dtype=int64, place=CPUPlace, stop_gradient=True,
       [2])
浮点型标量:Tensor(shape=[1], dtype=float64, place=CPUPlace, stop_gradient=True,
       [2.]),Tensor(shape=[1], dtype=float64, place=CPUPlace, stop_gradient=True,
       [2.])

创建一个二维的张量(矩阵)

法方法和一维的一样,但是可以对比结果,看到一维和二维的差距明显

rank_2_tensor = paddle.to_tensor([[1.0, 2.0, 3.0],
                                  [4.0, 5.0, 6.0]])
print(rank_2_tensor)
Tensor(shape=[2, 3], dtype=float32, place=CPUPlace, stop_gradient=True,
       [[1., 2., 3.],
        [4., 5., 6.]])

创建多维矩阵

方法一样,就是把里面的内容进行一定程度的修改,把矩阵的维度进行一定的提高

# Tensor可以有任意数量的轴(也称为维度)
rank_3_tensor 
  • 9
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三岁学编程

感谢支持,更好的作品会继续努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值