一、张量的科学运算
1 进行数值调整
t = torch.randn(5)
t
# tensor([ 0.3806, 0.9064, -1.9179, 2.0816, -0.4153])
1.1 返回绝对值
torch.abs(t)
# tensor([0.3806, 0.9064, 1.9179, 2.0816, 0.4153])
1.2 返回相反数
torch.neg(t)
# tensor([-0.3806, -0.9064, 1.9179, -2.0816, 0.4153])
1.3 四舍五入
torch.round(t)
# tensor([ 0., 1., -2., 2., -0.])
1.4 向上取整
torch.ceil(t)
# tensor([ 1., 1., -1., 3., -0.])
1.5 向下取整
torch.floor(t)
# tensor([ 0., 0., -2., 2., -1.])
注意
虽然此类型函数并不会对原对象进行调整,而是输出新的结果。
# t本身并未发生变化
t
# tensor([ 0.3806, 0.9064, -1.9179, 2.0816, -0.4153])
若要对原对象本身进行修改,可使用方法_()
。
# 使用方法_()
t.round_()
# tensor([ 0., 1., -2., 2., -0.])
# 原对象也进行了改变
t
# tensor([ 0., 1., -2., 2., -0