欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python中x +=1和x = x + 1的区别

程序员文章站 2022-10-28 10:26:20
x = torch.rand((5, 3))print(x)# 改变形状y = x.view(15)z = x.view(-1,5) # -1所指的维度可以根据其他维度的值推出来print(x)print(y)print(x.size(),y.size(),z.size())x = x + 1print(x)print(y)运行结果:tensor([[0.7148, 0.5680, 0.9139], [0.9242, 0.9893, 0.9999], ....
x = torch.rand((5, 3))
print(x)
# 改变形状
y = x.view(15)
z = x.view(-1,5) #  -1所指的维度可以根据其他维度的值推出来
print(x)
print(y)
print(x.size(),y.size(),z.size())
x = x + 1
print(x)
print(y)

运行结果:

tensor([[0.7148, 0.5680, 0.9139],
        [0.9242, 0.9893, 0.9999],
        [0.8309, 0.3248, 0.5042],
        [0.7662, 0.6790, 0.9625],
        [0.1181, 0.3736, 0.5293]])
tensor([[0.7148, 0.5680, 0.9139],
        [0.9242, 0.9893, 0.9999],
        [0.8309, 0.3248, 0.5042],
        [0.7662, 0.6790, 0.9625],
        [0.1181, 0.3736, 0.5293]])
tensor([0.7148, 0.5680, 0.9139, 0.9242, 0.9893, 0.9999, 0.8309, 0.3248, 0.5042,
        0.7662, 0.6790, 0.9625, 0.1181, 0.3736, 0.5293])
torch.Size([5, 3]) torch.Size([15]) torch.Size([3, 5])
tensor([[1.7148, 1.5680, 1.9139],
        [1.9242, 1.9893, 1.9999],
        [1.8309, 1.3248, 1.5042],
        [1.7662, 1.6790, 1.9625],
        [1.1181, 1.3736, 1.5293]])
tensor([0.7148, 0.5680, 0.9139, 0.9242, 0.9893, 0.9999, 0.8309, 0.3248, 0.5042,
        0.7662, 0.6790, 0.9625, 0.1181, 0.3736, 0.5293])

将上面的 x = x + 1 换成 x +=1

x = torch.rand((5, 3))
print(x)
# 改变形状
y = x.view(15)
z = x.view(-1,5) #  -1所指的维度可以根据其他维度的值推出来
print(x)
print(y)
print(x.size(),y.size(),z.size())
x += 1
print(x)
print(y)

运行结果:

tensor([[0.3179, 0.8790, 0.1822],
        [0.8152, 0.1586, 0.3999],
        [0.3411, 0.7993, 0.3838],
        [0.0352, 0.3884, 0.9328],
        [0.6239, 0.4884, 0.4452]])
tensor([[0.3179, 0.8790, 0.1822],
        [0.8152, 0.1586, 0.3999],
        [0.3411, 0.7993, 0.3838],
        [0.0352, 0.3884, 0.9328],
        [0.6239, 0.4884, 0.4452]])
tensor([0.3179, 0.8790, 0.1822, 0.8152, 0.1586, 0.3999, 0.3411, 0.7993, 0.3838,
        0.0352, 0.3884, 0.9328, 0.6239, 0.4884, 0.4452])
torch.Size([5, 3]) torch.Size([15]) torch.Size([3, 5])
tensor([[1.3179, 1.8790, 1.1822],
        [1.8152, 1.1586, 1.3999],
        [1.3411, 1.7993, 1.3838],
        [1.0352, 1.3884, 1.9328],
        [1.6239, 1.4884, 1.4452]])
tensor([1.3179, 1.8790, 1.1822, 1.8152, 1.1586, 1.3999, 1.3411, 1.7993, 1.3838,
        1.0352, 1.3884, 1.9328, 1.6239, 1.4884, 1.4452])

(1)不可变元素的类型有:整数、浮点数、字符串、元组
(2)可变元素的类型有:列表、字典

不可变元素的类型,“x +=1"与"x=x+1"作用一样,没有区别
可变元素的类型中,“x +=1"与"x=x+1"作用不同,区别如下:
      x +=1,表示:修改x自身的值(前提是:x必须是可变对象)
      x = x + 1,表示:创建一个新的"同名”对象x,并将x + 1,赋值给新创建的同名变量x(它的含义和y = x + 1相同,只不过这里将y取名为x了),x + 1赋值给新x后,旧x的指向就会被释放


所以如果我们想返回⼀个真正新的副本(即不共享内存)该怎么办呢?Pytorch还提供了⼀ 个 reshape() 可以改变形状,但是此函数并不能保证返回的是其拷贝,所以不推荐使用。推荐先 ⽤ clone 创造一个副本然后再使⽤ view 。

x_cp = x.clone().view(15)
x -=1
print(x)
print(x_cp)

运行结果:


tensor([[0.3179, 0.8790, 0.1822],
        [0.8152, 0.1586, 0.3999],
        [0.3411, 0.7993, 0.3838],
        [0.0352, 0.3884, 0.9328],
        [0.6239, 0.4884, 0.4452]])
tensor([1.3179, 1.8790, 1.1822, 1.8152, 1.1586, 1.3999, 1.3411, 1.7993, 1.3838,
        1.0352, 1.3884, 1.9328, 1.6239, 1.4884, 1.4452])

 

本文地址:https://blog.csdn.net/FeNGQiHuALOVE/article/details/107569733

相关标签: PyTorch