python深度学习基于pytorch——tensor中逐元素计算addcdiv()、clamp()

        对tensor中的元素按个计算操作,方法如下图所示:

主要就是tensor之间的运算,已经附加代码输出部分,很容易理解。

import torch
#coco
#逐元素操作
torch.manual_seed(0)
t = torch.randn(1, 3)
print(t)
#tensor([[ 1.5410, -0.2934, -2.1788]])
t1 = torch.randn(3, 1)
print(t1)
# tensor([[ 0.5684],
#         [-1.0845],
#         [-1.3986]])
t2 = torch.randn(1, 3)
print(t2)
# tensor([[ 0.4033,  0.8380, -0.7193]])
aa=t1/t2
# tensor([[ 1.4093,  0.6783, -0.7903],
#         [-2.6888, -1.2941,  1.5078],
#         [-3.4675, -1.6689,  1.9445]])
print(aa)
# t+0.1*(t1/t2)
tt=torch.addcdiv(t, 0.1, t1, t2)
print(tt)
# tensor([[ 1.6819, -0.2256, -2.2578],
#         [ 1.2721, -0.4228, -2.0280],
#         [ 1.1942, -0.4603, -1.9843]])

# 计算sigmoid
st=torch.sigmoid(t)
print(st)
# tensor([[0.8236, 0.4272, 0.1017]])
# 将t限制在[0,1]之间
bb=torch.clamp(t, 0, 1)
print(bb)
# tensor([[1., 0., 0.]])
# t+2进行就地运算
cc=t.add_(2)
print(cc)
# tensor([[ 3.5410,  1.7066, -0.1788]])
# 开根号
dd=torch.zeros_like(t)
torch.sqrt(t, out=dd)
print(dd)
# tensor([[1.8818, 1.3064,    nan]])
# 因为第三个数是负数

夏天来了,人异常疲惫,每当想偷懒的时候,我就想想自己偶像们,坚持下来接着学习。

我并不讨厌逆境,我坚信越过它看到的景色会更美好。

——羽生结弦

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2022年5月20日
下一篇 2022年5月21日

相关推荐