其他分享
首页 > 其他分享> > [Pytorch]Tensor

[Pytorch]Tensor

作者:互联网

Tensors 张量

import torch 
import numpy as np 

1 初始化张量

1.1 直接从python数据

data=[[1,2],[3,4]]
x_data=torch.tensor(data)
print(x_data)
print(x_data.type())
tensor([[1, 2],
        [3, 4]])
torch.LongTensor
x_data=torch.tensor(data,dtype=torch.float)
x_data.type()
'torch.FloatTensor'

1.2 从numpy数组

np_array = np.array(data)
x_np = torch.from_numpy(np_array)
x_np
tensor([[1, 2],
        [3, 4]], dtype=torch.int32)

1.3 从其他张量

x_ones = torch.ones_like(x_data) # 保留x_data的属性,类似于size、dtype等
print("Ones Tensor:\n{}\n".format(x_ones))
x_rand = torch.rand_like(x_data, dtype=torch.float) # 覆盖原有x_data的数据类型
print(f"Random Tensor: \n {x_rand} \n")
Ones Tensor:
tensor([[1., 1.],
        [1., 1.]])

Random Tensor: 
 tensor([[0.3120, 0.7532],
        [0.8578, 0.6995]]) 

1.4 从常量或者随机数

shape = (2,3,)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)

print(f"Random Tensor: \n {rand_tensor} \n")
print(f"Ones Tensor: \n {ones_tensor} \n")
print(f"Zeros Tensor: \n {zeros_tensor}")
Random Tensor: 
 tensor([[0.8017, 0.0391, 0.3893],
        [0.6150, 0.4361, 0.8481]]) 

Ones Tensor: 
 tensor([[1., 1., 1.],
        [1., 1., 1.]]) 

Zeros Tensor: 
 tensor([[0., 0., 0.],
        [0., 0., 0.]])

2 张量属性

tensor = torch.rand(3,4)

print(f"Shape of tensor: {tensor.shape}")  # 张量形状
print(f"Datatype of tensor: {tensor.dtype}") # 张量数据类型
print(f"Device tensor is stored on: {tensor.device}")  # 使用存储张量的设备 CPU or GPU
Shape of tensor: torch.Size([3, 4])
Datatype of tensor: torch.float32
Device tensor is stored on: cpu

3 张量操作

3.1 将张量存储(计算)至GPU

d=torch.ones(3)
print(d.device)
d=d.to("cuda")
print(d.device)
cpu
cuda:0

3.2 张量切片

tensor = torch.ones(4, 4) # 2维度的张量
print(f"First row: {tensor[0]}")
print(f"First column: {tensor[:, 0]}")
print(f"Last column: {tensor[..., -1]}")
tensor[:,1] = 0
print(tensor)
First row: tensor([1., 1., 1., 1.])
First column: tensor([1., 1., 1., 1.])
Last column: tensor([1., 1., 1., 1.])
tensor([[1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.]])

3.3 张量连接

t1 = torch.cat([tensor, tensor, tensor], dim=1)
print(t1)
tensor([[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
        [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
        [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
        [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.]])

3.4 算术运算

# 矩阵相乘
tensor = torch.ones(4, 4) # 2维度的张量
y1 = tensor @ tensor.T # @矩阵相乘运算符
y2 = tensor.matmul(tensor.T)
y3 = torch.rand_like(y1) # 先赋值,然后通过matmul计算相乘
torch.matmul(tensor, tensor.T, out=y3)
print(y3)

# 矩阵点乘
z1 = tensor * tensor
z2 = tensor.mul(tensor)
z3 = torch.rand_like(tensor) # 先赋值,然后点乘输出
torch.mul(tensor, tensor, out=z3)
tensor([[4., 4., 4., 4.],
        [4., 4., 4., 4.],
        [4., 4., 4., 4.],
        [4., 4., 4., 4.]])





tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]])

可以用.sum()方法对张量求和,使用item()转化为python数值

agg = tensor.sum()
agg_item = agg.item()
print(agg_item, type(agg_item))
16.0 <class 'float'>

In-place operations 将计算的值直接存储在变量中用对应_方法

print(f"{tensor} \n")
tensor.add_(5)
print(tensor)
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]]) 

tensor([[6., 6., 6., 6.],
        [6., 6., 6., 6.],
        [6., 6., 6., 6.],
        [6., 6., 6., 6.]])

4 与numpy之间的转化

4.1 张量转化为numpy数组

t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")
t: tensor([1., 1., 1., 1., 1.])
n: [1. 1. 1. 1. 1.]

在张量上的更改会直接改变numpy的值

t.add_(1)
print(f"t: {t}")
print(f"n: {n}")
t: tensor([2., 2., 2., 2., 2.])
n: [2. 2. 2. 2. 2.]

4.2 numpy数组转化为Tensor

n = np.ones(5)
t = torch.from_numpy(n)

改变numpy的值会直接改变张量的值

np.add(n, 1, out=n)
print(f"t: {t}")
print(f"n: {n}")
t: tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
n: [2. 2. 2. 2. 2.]

标签:Tensor,torch,张量,Pytorch,print,data,tensor
来源: https://www.cnblogs.com/Vandaci/p/16461041.html