[ML2021] Pytorch Tutorial 2

Hung-yi Lee
12 Mar 202109:21

Summary

TLDR视频脚本主要介绍了如何使用PyTorch框架进行机器学习。首先,脚本提到了可以在机器学习网站上点击协作(Collab)来运行代码,验证结果。接着,介绍了PyTorch文档网站,指导用户如何根据函数类型(神经网络、优化算法或数据集)查找相关文档。以torch.max函数为例,详细说明了如何查阅文档,理解参数类型(位置参数和关键字参数),以及参数的默认值。然后,通过三个不同的例子展示了torch.max函数的用法:返回张量的最大值、选择特定维度上的最大值并返回索引、以及比较两个张量并返回元素级别的最大值。此外,还讨论了在训练模型时可能遇到的常见错误,如模型和输入在不同设备上、维度不匹配、CUDA内存不足、张量类型不匹配等问题,并提供了相应的解决方案。最后,脚本提到了在Collab中可以运行关于数据加载器的代码段,以进一步探索PyTorch的功能。

Takeaways

  • 📚 在机器学习网站上,可以通过点击协作(Collab)来运行PyTorch教程中的代码,以验证结果。
  • 🔍 如果在使用PyTorch函数时遇到问题,可以访问PyTorch文档网站,根据功能类型访问不同的子域名。
  • 📝 torch.max函数的使用方法可以在PyTorch文档中找到,注意输入类型和输出类型。
  • 👀 torch.max函数有多个变体,每个变体有不同的参数和关键字参数,以及默认值。
  • 📈 第一个torch.max变体返回张量的最大值,第二个变体选择特定维度的最大值及其索引,第三个变体比较两个张量并返回逐元素的最大值。
  • ❌ 在使用torch.max函数时,要注意参数的位置和类型,关键字参数需要指定名称。
  • 💻 训练模型时可能会遇到的常见错误包括模型和输入不在同一个地方(CPU/GPU不匹配)、维度不匹配、CUDA内存不足以及张量类型不匹配。
  • 🚀 将数据发送到GPU可以解决CPU/GPU不匹配的问题。
  • 🔄 使用转置(transpose)、压缩(squeeze)和取消压缩(unsqueeze)等操作可以解决维度不匹配的问题。
  • 🔩 减少批量大小或使用数据加载器(data loader)可以解决CUDA内存不足的问题。
  • ⚙️ 当使用交叉熵损失函数时,确保标签是长整型(tensor类型为long),以避免类型不匹配的错误。
  • 📈 在Collab的底部部分,还有关于数据加载器的代码示例,可以自行运行以加深理解。

Q & A

  • 在机器学习网站上如何运行PyTorch教程中的代码?

    -在机器学习网站上,你可以点击'collab',在'pi torch tutorial 2 column'中,然后你可以运行代码以验证结果。

  • 如果遇到有关PyTorch函数使用的问题,应该访问哪个网站?

    -如果遇到问题,可以访问PyTorch的官方文档网站。具体来说,如果函数与神经网络相关,访问torch.org;如果与优化算法相关,访问torch.optim;如果与数据集或数据加载器相关,访问torch.utils.data。

  • 如何理解PyTorch文档中函数参数的类型和用法?

    -在PyTorch文档中,首先查看函数的输入(input)和输出(output),输入的类型会在'parameters'部分指定。此外,文档会展示函数的不同变体,包括它们各自的参数和关键字参数。参数和关键字参数的区别在于,关键字参数需要指定名称。通过查找星号(*)可以确定参数是位置参数还是关键字参数,星号前的是位置参数,星号后的则是关键字参数。

  • torch.max函数有什么作用,它有哪些不同的变体?

    -torch.max函数用于找出张量中的最值。它有三种变体:第一种简单返回张量的最大值;第二种从指定的维度选择最大值,并返回该值及对应的索引;第三种比较两个张量的对应元素,并返回一个新的张量,其中的元素是两个张量对应元素的最大值。

  • 如果在运行PyTorch代码时遇到了'out'参数的使用错误,应该怎么做?

    -如果遇到'out'参数的使用错误,需要将其作为关键字参数使用,因为它不是位置参数。在调用函数时,必须指定参数名称,而不能仅输入值。

  • 在PyTorch中,如果需要指定操作的维度,应该如何修改代码?

    -如果需要指定操作的维度,应确保在调用函数时明确指出'dimension'参数。如果未指定,可能会导致运行错误。

  • 在训练模型时,如果模型和输入数据不在同一个设备上,比如一个在CPU上,另一个在GPU上,会有什么错误提示?

    -如果模型和输入数据不在同一个设备上,错误信息会提示比如'tensorflow'或类似的库在CPU上运行,但期望它们在GPU上。解决这个问题,需要将张量发送到GPU上。

  • 如果输入数据和模型的维度不匹配,应该如何解决?

    -如果输入数据和模型的维度不匹配,可以使用转置(transpose)、压缩(squeeze)和扩展(unsqueeze)等操作来调整张量的维度,使其与模型的期望输入维度一致。

  • 当GPU内存不足时,通常会出现什么错误,应该如何处理?

    -当GPU内存不足时,会出现'cuda out of memory'错误。解决这个问题可以通过减少批量大小(batch size)或者使用数据加载器(data loader)分批次处理数据。

  • 如果在使用交叉熵损失函数时,标签数据类型不匹配,会有什么错误提示?

    -如果标签数据类型不匹配,比如将标签设为整数但实际上是浮点数类型,会出现错误提示,因为标签必须是长整型(long type)。需要将标签转换为长整型张量。

  • 在PyTorch中,如何使用数据加载器(data loader)?

    -在PyTorch中,可以使用torch.utils.data.DataLoader类来创建数据加载器。数据加载器可以帮助高效地批量加载数据,并可以与迭代器一起使用,以便于在训练模型时逐批次地访问数据。

  • 在提供的脚本中,有哪些常见的错误和解决方法?

    -脚本中提到的常见错误包括:设备不匹配错误(模型在CPU而数据期望在GPU)、维度不匹配错误(需要调整张量维度)、CUDA内存不足错误(减少批量大小或使用数据加载器)和张量类型不匹配错误(确保标签是长整型)。每个错误都有相应的解决方法,如调整设备、修改张量维度、调整批量大小或更改数据类型。

Outlines

00:00

😀 探索PyTorch文档和torch.max函数的使用

本段落主要介绍了如何通过PyTorch的官方网站来访问和使用torch.max函数。首先,提到了通过机器学习网站的collab功能可以运行代码以验证结果。接着,详细说明了PyTorch文档的访问方式,包括针对神经网络、优化算法和数据集或数据加载器的特定文档链接。以torch.max为例,解释了如何在文档中查找输入参数、输出类型以及不同变体的使用。特别指出了参数和关键字参数的区别,并通过星号(*)的位置来判断。最后,概述了torch.max函数的三种不同用途:返回张量的最大值、从特定维度选择最大值及其索引、以及比较两个张量并返回元素级别的最大值。

05:02

😉 PyTorch代码示例和常见错误处理

第二段落首先提供了与前述torch.max函数相关的代码示例,并指出了代码中存在的两个错误:一是将关键字参数out错误地用作位置参数,二是在使用函数时未指定维度。然后,讨论了在模型训练过程中可能遇到的一些常见错误,包括模型和输入位于不同的设备(CPU或GPU)、张量维度不匹配、CUDA内存不足以及张量类型不匹配。对于这些问题,提供了相应的解决方案,如将张量发送到GPU、调整张量维度、减少批量大小以及将标签转换为长整型(long type)。此外,还提到了数据加载器的使用,以及如何在collab中运行相关代码。

Mindmap

Keywords

💡机器学习

机器学习是一种使计算机系统利用数据来不断改进性能的技术。在视频中,它作为主题,围绕如何使用PyTorch进行机器学习模型的构建和训练展开讨论。

💡PyTorch

PyTorch是一个开源的机器学习库,广泛用于计算机视觉和自然语言处理等应用。视频中提到了如何通过PyTorch的官方网站和文档来学习其功能和使用方法。

💡文档

文档是提供关于软件、库或系统如何使用和实现的详细说明。在视频中,文档被用来查找PyTorch函数的使用方法,如torch.max,以及解决常见问题。

💡torch.max

torch.max是PyTorch中用于找出张量中最大值的函数。视频中通过torch.max的不同变体来展示如何获取最大值、选择最大值的索引以及比较两个张量的元素。

💡参数和关键字参数

参数和关键字参数是函数调用时传入的值。参数通常按顺序传入,而关键字参数则需要指定名称。视频中解释了如何根据文档中的星号(*)来区分它们,并说明了如何正确使用。

💡默认值

默认值是函数参数在未指定时使用的预设值。视频中提到了torch.max函数中的keepdim参数具有默认值,意味着在不改变函数行为的情况下可以省略该参数。

💡错误处理

错误处理是编程中识别和响应错误的过程。视频提到了在模型训练中可能遇到的几种错误,如设备不匹配、维度不匹配、CUDA内存不足和张量类型不匹配,并提供了相应的解决方案。

💡GPU

GPU是图形处理单元,常用于并行处理和图形渲染,也被用于机器学习中进行快速计算。视频提到了当模型和输入数据位于不同的设备(如CPU和GPU)时可能遇到的问题,并展示了如何将数据发送到GPU。

💡数据加载器

数据加载器是机器学习中用于高效加载和预处理数据流的工具。视频最后提到了数据加载器的使用,它可以帮助分批次地加载数据,以适应GPU内存限制并提高训练效率。

💡维度对齐

维度对齐是指在进行数学运算(如加法)之前,确保参与运算的张量具有兼容的维度。视频中通过示例说明了如果不对齐维度,将导致错误,并展示了如何使用转置、压缩和扩展等操作来解决这个问题。

💡张量类型

张量类型指的是数据在内存中的存储方式和解释方式。在视频中,提到了在使用交叉熵损失函数时,标签必须是长整型(long type)而不是浮点型(float),以避免类型不匹配错误。

Highlights

通过机器学习网站可以点击Collab来运行代码验证结果

PyTorch文档位于特定网站,遇到函数使用问题可以访问该网站

torch.max函数的输入输出类型在文档中有明确说明

torch.max函数的不同变体及其参数和关键字参数的区别

参数和关键字参数的区别在于是否需要指定名称

torch.max函数的三种不同用法及其对应的代码示例

文档中参数前的星号标记表示参数是位置参数还是关键字参数

torch.max函数的三种用法分别返回最大值、最大值及其索引、比较两个张量元素的最大值

在执行torch.max函数时,需要注意输入的正确性,如维度和关键字参数的使用

模型训练时可能遇到的常见错误包括设备不匹配、维度不匹配、CUDA内存不足和张量类型不匹配

解决设备不匹配错误的方法是将张量发送到GPU

解决维度不匹配错误的方法是通过转置、压缩和扩展操作来对齐维度

解决CUDA内存不足错误的方法是减少批量大小

解决张量类型不匹配错误的方法是根据需要将数据类型转换为正确的类型

在Collab的底部部分,还有关于数据加载器的说明和代码示例

文档中还提供了torch.max函数的详细用法和参数说明

文档中的参数和关键字参数的区分对于正确使用函数至关重要

文档中的默认参数值意味着在某些情况下可以省略某些输入

Transcripts

play00:02

and

play00:03

you can also access

play00:07

there's also a collab associated with it

play00:10

so

play00:11

if you are on the machine learning

play00:14

website you can click on the collab

play00:16

in in the pi torch tutorial 2 column

play00:20

and then you can you can run the code

play00:24

to verify the results okay

play00:29

so the pi torch documentation is at

play00:32

this website and if you encounter any

play00:35

problems regarding the usage of

play00:38

functions you can go to this website if

play00:43

if the function is related to neural

play00:45

network you go to torch dot and then

play00:48

and if your function is related to

play00:51

optimization algorithms you go to

play00:53

torch.optin

play00:54

and if your function is related

play00:58

to data set or data loaders you go to

play01:00

torch.udos.data

play01:04

and let's take torch.max as an example

play01:08

so if i were to

play01:12

search in the documentation and i want

play01:15

to know how to use this

play01:16

function so first thing i the first

play01:19

thing i

play01:20

i would look is the top row and

play01:24

it says that it has an input named input

play01:28

and it outputs and tensor

play01:30

a tensor and the in

play01:33

input the type of the input is specified

play01:36

below

play01:37

as you can see under parameters it says

play01:39

that input is also a tensor so

play01:41

you need to make sure that your data is

play01:44

in a correct form

play01:50

and if i if you scroll further down

play01:54

on the torch dot max web page then you

play01:58

will see that

play01:59

torch.max has different variations for

play02:02

example this variation

play02:03

will take in not only input but also

play02:06

dimensions

play02:06

and keep dimension and it also has an

play02:10

optional an out vector to a tensor to

play02:14

specify

play02:14

and you can see that under the

play02:19

under the documentation the parameters

play02:22

are split into

play02:23

parameters and keyword arguments these

play02:26

two

play02:26

are different by in that

play02:30

the for keyword arguments when you feed

play02:32

in

play02:33

keyword arguments to the function you

play02:35

have to specify the name as well you

play02:37

cannot just

play02:38

just input the value of

play02:42

the keyword arguments and for parameters

play02:44

you don't need to do so you can simply

play02:46

line the position up with the

play02:50

with the name on on the

play02:54

on the function and to know whether

play02:57

a parameter is a parameters or

play03:01

is a keyword argument you can search for

play03:04

the

play03:06

the star mark and if the

play03:11

parameters appears before the star mark

play03:13

is it is end

play03:15

it is a positional argument if the

play03:18

parameter appears after the star mark it

play03:20

will be a keyword argument

play03:23

also there are some arguments with

play03:25

default values so

play03:27

if it is a argument with a default value

play03:30

you don't need to

play03:32

have an input to make the function work

play03:35

so for example in this case keep

play03:37

dimension

play03:38

is a argument with default value so this

play03:41

function can

play03:46

you only need to input two

play03:49

two parameters to this function for it

play03:51

to work

play03:54

and so there are a total of three kinds

play03:57

of torch

play03:58

dot in the pytorch documentation

play04:01

and each of it serves a different

play04:04

purpose

play04:06

so in the following slides we will

play04:08

demonstrate

play04:09

what each one means

play04:13

the first one is the most simple simple

play04:15

one and sim simply

play04:17

takes in at a tensor and returns the

play04:20

maximum value of

play04:21

the the tensor and

play04:24

the return value although is it is a

play04:28

number it is also in the tensor form

play04:32

and the second one will select the max

play04:35

from

play04:36

a certain dimension and return that

play04:38

value along with the index corresponding

play04:40

to

play04:41

that value so in this case the

play04:44

the output is different from the

play04:46

previous one so

play04:48

you need to change your code to fit the

play04:51

output as well

play04:54

and for the third one the this torch.max

play04:57

compares two tensors and selects

play04:59

maximum of each individual elements and

play05:02

returns a

play05:03

tensor of the same size

play05:08

and so these are the corresponding code

play05:11

to the

play05:12

three functions and this code is also in

play05:15

collapse so if you would like to

play05:18

execute it feel free to do so and

play05:21

for the second function

play05:25

most of most of these are correct but

play05:29

there are two there are two inputs that

play05:32

are wrong and the first one is wrong

play05:35

because out is a keyword argument so you

play05:38

cannot

play05:39

use it as a positional argument and the

play05:42

second one is wrong

play05:43

is because you did not specify the

play05:50

dimension and

play05:52

the last part is about common errors you

play05:55

might

play05:55

encounter when training the model

play05:59

the first is that the model might be in

play06:01

a different place

play06:03

as your input so in this case the

play06:07

error message would be tensorflow or

play06:09

something is on cpu but expecting them

play06:11

to be on gpu

play06:13

and so all you have to do is send the

play06:14

tensor to gpu and

play06:17

they uh

play06:20

model can can be computed correctly

play06:23

so the the code above is the wrong

play06:26

version and the code below is

play06:28

the correct version and the second one

play06:31

is mismatched

play06:32

dimensions so for example if

play06:36

an x equals a random

play06:40

tensor of size four and four by five

play06:43

and y is five by four and then if you

play06:46

try to add these two

play06:47

together you get this error message and

play06:50

you should use transpose squeeze and

play06:53

unsqueeze

play06:55

etc to align the dimensions and then

play06:58

this

play06:58

error message will disappear and this

play07:02

error mostly comes from um

play07:06

well might might come from

play07:09

when you use a pre-trained model and the

play07:14

specified batch size and data is at

play07:17

different dimensions then you need to

play07:19

align your data with the ones specified

play07:22

on the pre-trained model

play07:25

and and also common error is cuda out of

play07:28

memory error this happens when you feed

play07:31

too many data into gpu at once so the

play07:34

memory is not sufficient and for

play07:37

to solve this problem you reduce your

play07:40

batch size

play07:41

so in this code above a

play07:44

random data is generated and the data is

play07:48

equivalent to 512

play07:51

photos of 244x244

play07:56

and if you feed this data to the model

play08:00

at once the memory is not sufficient

play08:04

so if you change to iterating the data

play08:08

the problem will be solved and you can

play08:10

also use data loader as well

play08:14

and sometimes you will get mismatched

play08:16

tensor type as your error this mostly

play08:19

happens when you use

play08:20

cross entropy loss and

play08:24

in this code if you set the labels to

play08:27

like one two three four it looks like it

play08:29

is an integer but

play08:31

actually this type torch.tensor will

play08:34

result in a float

play08:35

and labels cannot be float so

play08:38

it will occur this error message and so

play08:42

you need to change it to

play08:43

the long type of tensor to make this

play08:45

work

play08:49

and that's it so in the collab at

play08:53

at the very bottom there's also a

play08:56

section about data loaders you can also

play09:00

run that section by yourself

play09:19

yes

Rate This

5.0 / 5 (0 votes)

Related Tags
机器学习PyTorch代码验证神经网络优化算法数据集文档教程函数使用错误处理GPU计算内存管理
Do you need a summary in English?