新手小白的pytorch学习第四弹-------张量上GPU以及对一到四弹的练习

1 导入包

import torch

2 查看是否有cuda, GPU

GPU就是一种能够让我们的计算更快速的设备
device = torch.cuda.is_available()

True

如果有 cuda 就使用 cuda, 没有就使用cpu
device =  "cuda" if torch.cuda.is_available() else "cpu"
device

‘cuda’

3 查看计算机中 GPU 的数量

torch.cuda.device_count()

1

4 把张量放到 GPU 上

首先创建一个张量
tensor_on_cpu = torch.tensor([1, 2, 3])
tensor_on_cpu, tensor_on_cpu.device

(tensor([1, 2, 3]), device(type=‘cpu’))

把张量放到 GPU 上
tensor_to_GPU = tensor_on_cpu.to(device)
tensor_to_GPU

tensor([1, 2, 3], device=‘cuda:0’)

一个的 device(type=‘cpu’) 一个 device=‘cuda:0’
我们成功的实现了将张量从 cpu 移到 gpu 上了
让我们看看在gpu上能不能将张量转换为numpy的数据
tensor_to_GPU.numpy()

在这里插入图片描述

这个 TypeError 提示我们不能将GPU上的张量转换为数组, 首先使用Tensor.cpu()复制张量到主机内存
tensor_from_GPU_to_CPU = tensor_to_GPU.cpu()
tensor_from_GPU_to_CPU, tensor_from_GPU_to_CPU.device

(tensor([1, 2, 3]), device(type=‘cpu’))

tensor_from_GPU_to_CPU.numpy()

array([1, 2, 3], dtype=int64)

转换为array了, 记得我们之前讲过,numpy默认数据类型是64是不
OK,第一part学习完了,接着做练习题啦!

5 练习题

  1. Documentation reading - A big part of deep learning (and learning to code in general) is getting familiar with the documentation of a certain framework you’re using. We’ll be using the PyTorch documentation a lot throughout the rest of this course. So I’d recommend spending 10-minutes reading the following (it’s okay if you don’t get some things for now, the focus is not yet full understanding, it’s awareness). See the documentation on torch.Tensor and for torch.cuda.

torch.Tensor 来自官方文档

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.
多维矩阵只包含单一数据类型
  1. Create a random tensor with shape (7, 7).
import torch
tensor = torch.rand(7, 7)
tensor

tensor([[0.9457, 0.6653, 0.7683, 0.6087, 0.2437, 0.0335, 0.8820],
[0.5867, 0.2306, 0.1701, 0.2795, 0.6827, 0.9029, 0.3403],
[0.1972, 0.7419, 0.9710, 0.3048, 0.1066, 0.6410, 0.8453],
[0.6417, 0.3950, 0.6943, 0.9015, 0.9228, 0.0816, 0.0207],
[0.4027, 0.3563, 0.1924, 0.0747, 0.7147, 0.5893, 0.7296],
[0.4755, 0.6003, 0.9358, 0.1524, 0.1705, 0.6169, 0.1740],
[0.7713, 0.1890, 0.5496, 0.0436, 0.5376, 0.6875, 0.6269]])

  1. Perform a matrix multiplication on the tensor from 2 with another random tensor with shape (1, 7) (hint: you may have to transpose the second tensor).
mat1 = torch.randn(1,7)
mat2 = torch.randn(1,7)
mat1.matmul(mat2.T)

tensor([[0.4029]])

  1. Set the random seed to 0 and do exercises 2 & 3 over again.
SEEDNUM = 0
torch.manual_seed(SEEDNUM)
tensor = torch.rand(7, 7)
tensor

(tensor([0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251, 0.2071, 0.6297, 0.3653,
0.8513]),
torch.Size([10]))
(tensor([[[[0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251, 0.2071, 0.6297,
0.3653, 0.8513]]]]),
torch.Size([3, 3]))
tensor([[0.9457, 0.6653, 0.7683, 0.6087, 0.2437, 0.0335, 0.8820],
[0.5867, 0.2306, 0.1701, 0.2795, 0.6827, 0.9029, 0.3403],
[0.1972, 0.7419, 0.9710, 0.3048, 0.1066, 0.6410, 0.8453],
[0.6417, 0.3950, 0.6943, 0.9015, 0.9228, 0.0816, 0.0207],
[0.4027, 0.3563, 0.1924, 0.0747, 0.7147, 0.5893, 0.7296],
[0.4755, 0.6003, 0.9358, 0.1524, 0.1705, 0.6169, 0.1740],
[0.7713, 0.1890, 0.5496, 0.0436, 0.5376, 0.6875, 0.6269]])
tensor([[0.4029]])
tensor([[0.4963, 0.7682, 0.0885, 0.1320, 0.3074, 0.6341, 0.4901],
[0.8964, 0.4556, 0.6323, 0.3489, 0.4017, 0.0223, 0.1689],
[0.2939, 0.5185, 0.6977, 0.8000, 0.1610, 0.2823, 0.6816],
[0.9152, 0.3971, 0.8742, 0.4194, 0.5529, 0.9527, 0.0362],
[0.1852, 0.3734, 0.3051, 0.9320, 0.1759, 0.2698, 0.1507],
[0.0317, 0.2081, 0.9298, 0.7231, 0.7423, 0.5263, 0.2437],
[0.5846, 0.0332, 0.1387, 0.2422, 0.8155, 0.7932, 0.2783]])

SEEDNUM = 0
torch.manual_seed(SEEDNUM)
mat1 = torch.randn(1,7)

SEEDNUM = 0
torch.manual_seed(SEEDNUM)
mat2 = torch.randn(1,7)

mat1.matmul(mat2.T)

tensor([[10.8260]])

  1. Speaking of random seeds, we saw how to set it with torch.manual_seed() but is there a GPU equivalent? (hint: you’ll need to look into the documentation for torch.cuda for this one). If there is, set the GPU random seed to 1234.
device = "cuda" if torch.cuda.is_available() else "cpu"
device

‘cuda’

SEEDNUM = 1234
torch.manual_seed(SEEDNUM)
torch.rand([2, 4], device=device)

tensor([[0.1272, 0.8167, 0.5440, 0.6601],
[0.2721, 0.9737, 0.3903, 0.3394]], device=‘cuda:0’)

  1. Create two random tensors of shape (2, 3) and send them both to the GPU (you’ll need access to a GPU for this). Set torch.manual_seed(1234) when creating the tensors (this doesn’t have to be the GPU random seed).
SEEDNUM = 1234
torch.manual_seed(SEEDNUM)
tensor_A = torch.rand(2, 3)

torch.manual_seed(SEEDNUM)
tensor_B = torch.rand(2, 3)
tensor_A, tensor_B

(tensor([[0.0290, 0.4019, 0.2598],
[0.3666, 0.0583, 0.7006]]),
tensor([[0.0290, 0.4019, 0.2598],
[0.3666, 0.0583, 0.7006]]))

把这两个张量放到GPU上
tensor_A_to_GPU = tensor_A.to(device)
tensor_B_to_GPU = tensor_B.to(device)
tensor_A_to_GPU.device, tensor_B_to_GPU.device

(device(type=‘cuda’, index=0), device(type=‘cuda’, index=0))

  1. Perform a matrix multiplication on the tensors you created in 6 (again, you may have to adjust the shapes of one of the tensors).
tensor_C = tensor_A_to_GPU.matmul(tensor_B_to_GPU.T)
tensor_C

tensor([[0.2299, 0.2161],
[0.2161, 0.6287]], device=‘cuda:0’)

  1. Find the maximum and minimum values of the output of 7.
max = torch.max(tensor_C)
max

tensor(0.6287, device=‘cuda:0’)

min = torch.min(tensor_C)
min

tensor(0.2161, device=‘cuda:0’)

  1. Find the maximum and minimum index values of the output of 7.
max_index = torch.argmax(tensor_C)
max_index

tensor(3, device=‘cuda:0’)

min_index = torch.argmin(tensor_C)
min_index

tensor(1, device=‘cuda:0’)

  1. Make a random tensor with shape (1, 1, 1, 10) and then create a new tensor with all the 1 dimensions removed to be left with a tensor of shape (10). Set the seed to 7 when you create it and print out the first tensor and it’s shape as well as the second tensor and it’s shape.
SEEDNUM = 7
torch.manual_seed(SEEDNUM)
tensor_D = torch.rand(1, 1, 1, 10)
tensor_D,tensor.shape

(tensor([[[[0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251, 0.2071, 0.6297,
0.3653, 0.8513]]]]),
torch.Size([3, 3]))

SEEDNUM = 7
torch.manual_seed(SEEDNUM)
tensor_D_squeezed = tensor_D.squeeze()
tensor_D_squeezed, tensor_D_squeezed.shape

(tensor([0.5349, 0.1988, 0.6592, 0.6569, 0.2328, 0.4251, 0.2071, 0.6297, 0.3653,
0.8513]),
torch.Size([10]))

妈耶,终于把欠的学习文档都写完了,明天可以好好的开始学习啦,不欠帐的感觉太赞了!

我今天牛得很,一个人炫了一盘金钱蛋,晚上又吃了一个煎蛋, 早上吃了一个水煮蛋,最近太爱吃蛋了,尊嘟很好吃

BB,如果这个文档对你有用的话,记得给我点个赞赞

谢谢,靴靴!

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 00:20:04       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 00:20:04       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 00:20:04       57 阅读
  4. Python语言-面向对象

    2024-07-15 00:20:04       68 阅读

热门阅读

  1. 行人越界检测 越线 越界区域 多边形IOU越界判断

    2024-07-15 00:20:04       22 阅读
  2. Vscode插件推荐——智能切换输入法(Smart IME)

    2024-07-15 00:20:04       19 阅读
  3. Python 学习之字典

    2024-07-15 00:20:04       17 阅读
  4. E12.【C语言】练习:求两个数的最大公约数

    2024-07-15 00:20:04       22 阅读