Pytorch预备知识之Tensor

酥酥 发布于 2022-04-12 93 次阅读


1.数据操作

				
					import torch

torch.manual_seed(0)
torch.cuda.manual_seed(0)
print(torch.__version__)
				
			
				
					x = torch.empty(5, 3)
print(x)
				
			
				
					x = torch.rand(5, 3)
print(x)
				
			
				
					x = torch.zeros(5, 3, dtype=torch.long)
print(x)
				
			
				
					x = torch.tensor([5.5, 3])
print(x)
				
			
				
					x = x.new_ones(5, 3, dtype=torch.float64)      # 返回的tensor默认具有相同的torch.dtype和torch.device
print(x)

x = torch.randn_like(x, dtype=torch.float)    # 指定新的数据类型
print(x) 
				
			
				
					print(x.size())
print(x.shape)
				
			
				
					y = torch.rand(5, 3)
print(x + y)
				
			
				
					print(torch.add(x, y))
				
			
tensor([[ 1.3967,  1.0892,  0.4369],
        [ 1.6995,  2.0453,  0.6539],
        [-0.1553,  3.7016, -0.3599],
        [ 0.7536,  0.0870,  1.2274],
        [ 2.5046, -0.1913,  0.4760]])
				
					result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
				
			
				
					# adds x to y
y.add_(x)
print(y)
				
			
				
					y = x[0, :]
y += 1
print(y)
print(x[0, :]) # 源tensor也被改了
				
			
				
					y = x.view(15)
z = x.view(-1, 5)  # -1所指的维度可以根据其他维度的值推出来
print(x.size(), y.size(), z.size())
				
			
				
					x += 1
print(x)
print(y) # 也加了1
				
			
				
					x_cp = x.clone().view(15)
x -= 1
print(x)
print(x_cp)
				
			
				
					x = torch.randn(1)
print(x)
print(x.item())
				
			
				
					x = torch.arange(1, 3).view(1, 2)
print(x)
y = torch.arange(1, 4).view(3, 1)
print(y)
print(x + y)
				
			
				
					x = torch.tensor([1, 2])
y = torch.tensor([3, 4])
id_before = id(y)
y = y + x
print(id(y) == id_before)
				
			
False
				
					x = torch.tensor([1, 2])
y = torch.tensor([3, 4])
id_before = id(y)
y[:] = y + x
print(id(y) == id_before)
				
			
True
				
					x = torch.tensor([1, 2])
y = torch.tensor([3, 4])
id_before = id(y)
torch.add(x, y, out=y) # y += x, y.add_(x)
print(id(y) == id_before)
				
			
				
					a = torch.ones(5)
b = a.numpy()
print(a, b)

a += 1
print(a, b)
b += 1
print(a, b)
				
			
				
					import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
print(a, b)

a += 1
print(a, b)
b += 1
print(a, b)
				
			
				
					# 用torch.tensor()转换时不会共享内存
c = torch.tensor(a)
a += 1
print(a, c)
				
			
				
					# 以下代码只有在PyTorch GPU版本上才会执行
if torch.cuda.is_available():
    device = torch.device("cuda")          # GPU
    y = torch.ones_like(x, device=device)  # 直接创建一个在GPU上的Tensor
    x = x.to(device)                       # 等价于 .to("cuda")
    z = x + y
    print(z)
    print(z.to("cpu", torch.double))       # to()还可以同时更改数据类型