pytorch中文官方教程(一)——张量


https://pytorch.apachecn.org/#/

1、代码

import torch
import numpy as np


data = [[1, 2], [3, 4]]
print(f"data:{data}")
x_data = torch.tensor(data)
print(f"x_data:{x_data}")


np_array = np.array(data)
x_np = torch.from_numpy(np_array)
print(f"np_array:{np_array}")
print(f"x_np:{x_np}")

x_ones = torch.ones_like(x_data)   # 保留 x_data 的属性
print(f"Ones Tensor: \n {x_ones} \n")

x_rand = torch.rand_like(x_data, dtype=torch.float)   # 重写 x_data 的数据类型:int -> float
print(f"Random Tensor: \n {x_rand} \n")

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}")


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}")  # 存储设备

# 判断当前环境GPU是否可用, 然后将tensor导入GPU内运行
if torch.cuda.is_available():
  tensor = tensor.to('cuda')
  print(f"tensor:{tensor}")

print(".....................................................")
tensor = torch.ones(4, 4)
print(tensor)
tensor[:,1] = 0            # 将第1列(从0开始)的数据全部赋值为0
print(tensor)

# 你可以通过torch.cat方法将一组张量按照指定的维度进行拼接, 也可以参考torch.stack方法。这个方法也可以实现拼接操作, 但和torch.cat稍微有点不同。
t1 = torch.cat([tensor, tensor, tensor], dim=1)
print(t1)

# 逐个元素相乘结果
print(f"tensor.mul(tensor): \n {tensor.mul(tensor)} \n")
# 等价写法:
print(f"tensor * tensor: \n {tensor * tensor}")

# 下面写法表示张量与张量的矩阵乘法:
print(f"tensor.matmul(tensor.T): \n {tensor.matmul(tensor.T)} \n")
# 等价写法:
print(f"tensor @ tensor.T: \n {tensor @ tensor.T}")

# 自动赋值运算通常在方法后有 _ 作为后缀, 例如: x.copy_(y), x.t_()操作会改变 x 的取值。
print(tensor, "\n")
tensor.add_(5)
print(tensor)

"""
Tensor与Numpy的转化
张量和Numpy array数组在CPU上可以共用一块内存区域, 改变其中一个另一个也会随之改变。 1. 由张量变换为Numpy array数组
"""
t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")

# 修改张量的值,则Numpy array数组值也会随之改变。
t.add_(1)
print(f"t: {t}")
print(f"n: {n}")

# 由Numpy array数组转为张量
n = np.ones(5)
print(f"n: {n}")
t = torch.from_numpy(n)
print(f"t: {t}")

# 修改Numpy array数组的值,则张量值也会随之改变。
np.add(n, 1, out=n)
print(f"t: {t}")
print(f"n: {n}")

2、输出结果实例

F:\GoogLeNet-PyTorch-main\envs\Scripts\python.exe F:/my_pytorch/pytorch_official/1_张量.py
data:[[1, 2], [3, 4]]
x_data:tensor([[1, 2],
[3, 4]])
np_array:[[1 2]
[3 4]]
x_np:tensor([[1, 2],
[3, 4]], dtype=torch.int32)
Ones Tensor:
tensor([[1, 1],
[1, 1]])

Random Tensor:
tensor([[0.0373, 0.4222],
[0.7082, 0.6928]])

Random Tensor:
tensor([[0.5024, 0.0342, 0.7680],
[0.5739, 0.0388, 0.0202]])

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

Zeros Tensor:
tensor([[0., 0., 0.],
[0., 0., 0.]])
Shape of tensor: torch.Size([3, 4])
Datatype of tensor: torch.float32
Device tensor is stored on: cpu
tensor:tensor([[0.7233, 0.1380, 0.4732, 0.7056],
[0.1913, 0.0636, 0.6214, 0.7385],
[0.9993, 0.7697, 0.8402, 0.2310]], device='cuda:0')
.....................................................
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])
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.]])
tensor.mul(tensor):
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])

tensor * tensor:
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])
tensor.matmul(tensor.T):
tensor([[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]])

tensor @ tensor.T:
tensor([[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]])
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])

tensor([[6., 5., 6., 6.],
[6., 5., 6., 6.],
[6., 5., 6., 6.],
[6., 5., 6., 6.]])
t: tensor([1., 1., 1., 1., 1.])
n: [1. 1. 1. 1. 1.]
t: tensor([2., 2., 2., 2., 2.])
n: [2. 2. 2. 2. 2.]
n: [1. 1. 1. 1. 1.]
t: tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
t: tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
n: [2. 2. 2. 2. 2.]

进程已结束,退出代码0

posted @ 2022-11-05 17:11  JaxonYe  阅读(44)  评论(0编辑  收藏  举报