机器学习 - Reproducibility

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()

看到这了,给个赞呗~

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值