In neural networks, it starts with random numbers to describe patterns in data (these numbers are poor descriptions) and try to improve those random numbers using tensor operations to better describe patterns in data.

下面是代码

import torch

random_tensor_A = torch.rand(3, 4)
random_tensor_B = torch.rand(3, 4)

print(f"Tensor A:\n {random_tensor_A}\n")
print(f"Tensor B:\b {random_tensor_B}\n")
print(f"Tensor A same as Tensor B?\n")
print(random_tensor_A == random_tensor_B)

# 结果如下
Tensor A:
 tensor([[0.8016, 0.3649, 0.6286, 0.9663],
        [0.7687, 0.4566, 0.5745, 0.9200],
        [0.3230, 0.8613, 0.0919, 0.3102]])

Tensor B tensor([[0.9536, 0.6002, 0.0351, 0.6826],
        [0.3743, 0.5220, 0.1336, 0.9666],
        [0.9754, 0.8474, 0.8988, 0.1105]])

Tensor A same as Tensor B?

tensor([[False, False, False, False],
        [False, False, False, False],
        [False, False, False, False]])

如果通过添加seed,两个Tensor就一样了

import torch
import random 

RANDOM_SEED = 30 
torch.manual_seed(seed = RANDOM_SEED)
random_tensor_C = torch.rand(3, 4)

torch.manual_seed(seed = RANDOM_SEED)
random_tensor_D = torch.rand(3, 4)

print(f"Tensor C:\n {random_tensor_C}\n")
print(f"Tensor D:\n {random_tensor_D}\n")
print(f"Does Tensor C equal D?")
print(random_tensor_C == random_tensor_D)

# 结果如下:
Tensor C:
 tensor([[0.9007, 0.7464, 0.4716, 0.8738],
        [0.7403, 0.7840, 0.8946, 0.6238],
        [0.4276, 0.8421, 0.7454, 0.6181]])

Tensor D:
 tensor([[0.9007, 0.7464, 0.4716, 0.8738],
        [0.7403, 0.7840, 0.8946, 0.6238],
        [0.4276, 0.8421, 0.7454, 0.6181]])

Does Tensor C equal D?
tensor([[True, True, True, True],
        [True, True, True, True],
        [True, True, True, True]])


这里稍微提一下CUDA
CUDA is a computing platform and API that helps allow GPUs be used for general purpose computing & not just graphics.


如果想要将tensor放到GPU上运行,可以通过 to(device) 的方法来操作,其中的device是 target device you’d like the tensor (or model) to go to.

# 默认在 CPU 上
tensor = torch.tensor([1,2,3])

# Move tensor to GPU (if available)
tensor_on_gpu = tensor.to(device)


如果想将tensor back to CPU来使用numpy,可以使用 tensor.cpu() 来操作。

tensor_back_on_cpu = tensor_on_gpu.cpu().numpy()

看到这了,给个赞呗~

03-20 16:15