大什么的网站建设公司好,百度竞价的优势和劣势,青岛网站设计多少钱,静态网页建站分类目录#xff1a;《深入浅出PaddlePaddle函数》总目录 Tensor是Paddle中最为基础的数据结构#xff0c;有几种创建Tensor的不同方式#xff1a;
用预先存在的数据创建1个Tensor#xff0c;请参考paddle.to_tensor创建一个指定shape的Tensor#xff0c;请参考paddle.on…分类目录《深入浅出PaddlePaddle函数》总目录 Tensor是Paddle中最为基础的数据结构有几种创建Tensor的不同方式
用预先存在的数据创建1个Tensor请参考paddle.to_tensor创建一个指定shape的Tensor请参考paddle.ones、paddle.zeros、paddle.full等创建一个与其他Tensor具有相同shape与dtype的Tensor请参考paddle.ones_like、paddle.zeros_like 、paddle.full_like
属性
clear_grad将当前Tensor的梯度设为0仅适用于具有梯度的Tensor通常我们将其用于参数因为其他临时Tensor没有梯度。
import paddle
input paddle.uniform([10, 2])
linear paddle.nn.Linear(2, 3)
out linear(input)
out.backward()
print(Before clear_grad, linear.weight.grad: {}.format(linear.weight.grad))
linear.weight.clear_grad()
print(After clear_grad, linear.weight.grad: {}.format(linear.weight.grad))clear_gradient与clear_grad功能相同dtype查看一个Tensor的数据类型支持bool、float16、float32、float64、uint8、int8、int16、int32、int64类型
import paddle
x paddle.to_tensor([1.0, 2.0, 3.0])
print(tensors type is: {}.format(x.dtype))grad查看一个Tensor的梯度数据类型为numpy.ndarray
import paddle
x paddle.to_tensor([1.0, 2.0, 3.0], stop_gradientFalse)
y paddle.to_tensor([4.0, 5.0, 6.0], stop_gradientFalse)
z x * y
z.backward()
print(tensors grad is: {}.format(x.grad))is_leaf判断Tensor是否为叶子Tensor。对于stop_gradient为True的Tensor它将是叶子Tensor。对于stop_gradient为False的Tensor 如果它是由用户创建的它也会是叶子 Tensor。
import paddle
x paddle.to_tensor(1.)
print(x.is_leaf) # True
x paddle.to_tensor(1., stop_gradientTrue)
y x 1
print(x.is_leaf) # True
print(y.is_leaf) # True
x paddle.to_tensor(1., stop_gradientFalse)
y x 1
print(x.is_leaf) # True
print(y.is_leaf) # Falseitem(*args)将Tensor中特定位置的元素转化为Python标量如果未指定位置则该 Tensor必须为单元素Tensor。
import paddle
x paddle.to_tensor(1)
print(x.item()) #1
print(type(x.item())) #class int
x paddle.to_tensor(1.0)
print(x.item()) #1.0
print(type(x.item())) #class float
x paddle.to_tensor(True)
print(x.item()) #True
print(type(x.item())) #class bool
x paddle.to_tensor(11j)
print(x.item()) #(11j)
print(type(x.item())) #class complex
x paddle.to_tensor([[1.1, 2.2, 3.3]])
print(x.item(2)) #3.3
print(x.item(0, 2)) #3.3name查看一个Tensor的nameTensor的name是其唯一标识符为python的字符串类型。
import paddle
print(Tensor name: , paddle.to_tensor(1).name) # Tensor name: generated_tensor_0ndim查看一个Tensor的维度也称作rank。
import paddle
print(Tensors number of dimensition: , paddle.to_tensor([[1, 2], [3, 4]]).ndim) # Tensors number of dimensition: 2persistable查看一个Tensor的persistable属性该属性为True时表示持久性变量持久性变量在每次迭代之后都不会删除。模型参数、学习率等Tensor都是持久性变量。
import paddle
print(Whether Tensor is persistable: , paddle.to_tensor(1).persistable) # Whether Tensor is persistable: falseplace查看一个Tensor的设备位置Tensor可能的设备位置有三种CPU/GPU/固定内存其中固定内存也称为不可分页内存或锁页内存 其与GPU之间具有更高的读写效率并且支持异步传输这对网络整体性能会有进一步提升但其缺点是分配空间过多时可能会降低主机系统的性能 因为其减少了用于存储虚拟内存数据的可分页内存。
import paddle
cpu_tensor paddle.to_tensor(1, placepaddle.CPUPlace())
print(cpu_tensor.place)shape查看一个Tensor的shapeshape是Tensor的一个重要的概念其描述了Tensor 在每个维度上的元素数量。
import paddle
print(Tensors shape: , paddle.to_tensor([[1, 2], [3, 4]]).shape) # Tensors shape: [2, 2]stop_gradient查看一个Tensor是否计算并传播梯度如果stop_gradient为True则该Tensor不会计算梯度并会阻绝Autograd的梯度传播。 反之则会计算梯度并传播梯度。用户自行创建的的Tensor默认是True模型参数的stop_gradient都为False。
import paddle
print(Tensors stop_gradient: , paddle.to_tensor([[1, 2], [3, 4]]).stop_gradient) # Tensors stop_gradient: True
文章转载自: http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.jrsgs.cn.gov.cn.jrsgs.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.lmknf.cn.gov.cn.lmknf.cn http://www.morning.trrd.cn.gov.cn.trrd.cn http://www.morning.qkxnw.cn.gov.cn.qkxnw.cn http://www.morning.npcxk.cn.gov.cn.npcxk.cn http://www.morning.bsghk.cn.gov.cn.bsghk.cn http://www.morning.wqtzs.cn.gov.cn.wqtzs.cn http://www.morning.bxsgl.cn.gov.cn.bxsgl.cn http://www.morning.dhckp.cn.gov.cn.dhckp.cn http://www.morning.yltnl.cn.gov.cn.yltnl.cn http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.rnrwq.cn.gov.cn.rnrwq.cn http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn http://www.morning.dbhnx.cn.gov.cn.dbhnx.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.jbshh.cn.gov.cn.jbshh.cn http://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.ykmg.cn.gov.cn.ykmg.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.ktlfb.cn.gov.cn.ktlfb.cn http://www.morning.yxwnn.cn.gov.cn.yxwnn.cn http://www.morning.dqkrf.cn.gov.cn.dqkrf.cn http://www.morning.gbfuy28.cn.gov.cn.gbfuy28.cn http://www.morning.qllcp.cn.gov.cn.qllcp.cn http://www.morning.nbsfb.cn.gov.cn.nbsfb.cn http://www.morning.txmlg.cn.gov.cn.txmlg.cn http://www.morning.mltsc.cn.gov.cn.mltsc.cn http://www.morning.ftgwj.cn.gov.cn.ftgwj.cn http://www.morning.lgphx.cn.gov.cn.lgphx.cn http://www.morning.btgxf.cn.gov.cn.btgxf.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.mmosan.com.gov.cn.mmosan.com http://www.morning.jqpyq.cn.gov.cn.jqpyq.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.hqlnp.cn.gov.cn.hqlnp.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.pffqh.cn.gov.cn.pffqh.cn http://www.morning.kyflr.cn.gov.cn.kyflr.cn http://www.morning.flqbg.cn.gov.cn.flqbg.cn http://www.morning.qyhcm.cn.gov.cn.qyhcm.cn http://www.morning.sxfnf.cn.gov.cn.sxfnf.cn http://www.morning.dtrcl.cn.gov.cn.dtrcl.cn http://www.morning.byywt.cn.gov.cn.byywt.cn http://www.morning.ptysj.cn.gov.cn.ptysj.cn http://www.morning.bfybb.cn.gov.cn.bfybb.cn http://www.morning.wjtwn.cn.gov.cn.wjtwn.cn http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.rkkh.cn.gov.cn.rkkh.cn http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn http://www.morning.kllzy.com.gov.cn.kllzy.com http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.nqlnd.cn.gov.cn.nqlnd.cn http://www.morning.zcrjq.cn.gov.cn.zcrjq.cn http://www.morning.bxhch.cn.gov.cn.bxhch.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.zqcdl.cn.gov.cn.zqcdl.cn http://www.morning.hhqtq.cn.gov.cn.hhqtq.cn http://www.morning.owenzhi.com.gov.cn.owenzhi.com http://www.morning.wspyb.cn.gov.cn.wspyb.cn http://www.morning.yfcyh.cn.gov.cn.yfcyh.cn http://www.morning.fssjw.cn.gov.cn.fssjw.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn http://www.morning.bfgpn.cn.gov.cn.bfgpn.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.npqps.cn.gov.cn.npqps.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.kcwkt.cn.gov.cn.kcwkt.cn http://www.morning.jbxmb.cn.gov.cn.jbxmb.cn http://www.morning.fxkgp.cn.gov.cn.fxkgp.cn http://www.morning.pmjw.cn.gov.cn.pmjw.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.ddjp.cn.gov.cn.ddjp.cn http://www.morning.tkrpt.cn.gov.cn.tkrpt.cn http://www.morning.qlwfz.cn.gov.cn.qlwfz.cn