-
1.tensors 讲解了tensors相关操作
- 生成张量
- 从数组转换到张量
-
2.datasets & dataloaders :PyTorch provides two data primitives:
torch.utils.data.DataLoader
:wraps an iterable around the Dataset to enable easy access to the samplestorch.utils.data.Dataset
:stores the samples and their corresponding labels- Loading a Dataset
root
is the path where the train/test data is stored,train
specifies training or test dataset,download=True
downloads the data from the internet if it’s not available atroot
.transform
andtarget_transform
specify the feature and label transformations
-
- transforms 是pytorch 提供的用来操作数据的模块
transform
:to modify the featurestarget_transform
:to modify the labelsfrom torchvision.transforms import ToTensor, Lambda
ToTensor()
ToTensor
converts a PIL image or NumPyndarray
into aFloatTensor
. and scales the image’s pixel intensity values in the range [0., 1.]
Lambda()
- Lambda transforms apply any user-defined lambda function.
- transforms 是pytorch 提供的用来操作数据的模块
-
- import lib
- Get Device for Training
- Define the Model Class
- Model Layers
- Model Parameters
- When training neural networks, the most frequently used algorithm is
back propagation
where parameters are adjusted according to the gradient of the loss function with respect to the given parameter.To compute those gradients, PyTorch providestorch.autograd
. - 6.Optimizing Model Parameters
Hyperparameters
:需要人为调节的参数称为超参数Number of Epochs
:the number times to iterate over the dataset- Training a model is an iterative process; In each iteration (called an
epoch
)- guess the output
- calculates loss
- collects the derivatives of the error (with respect to its parameters)
- optimizes parameters*(using gradient descent)
- Training a model is an iterative process; In each iteration (called an
Batch Size
:the number of data samples propagated through the network before the parameters are updatedLearning Rate
how much to update models parameters at each batch/epoch. Smaller values yield slow learning speed
Optimization Loop
:Each epoch consists of two main parts:- The Train Loop : iterate over the training dataset and try to converge(收敛) to optimal parameters.
- The Validation/Test Loop : iterate over the test dataset to check if model performance is improving.
- Loss Function:三种常用的损失函数
nn.MSELoss
(Mean Square Error):for regression tasksnn.NLLLoss
(Negative Log Likelihood) for classification.nn.CrossEntropyLoss
combinesnn.LogSoftmax
andnn.NLLLoss
.
- Optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
- Save & Load Model