Transforms
Data does not always come in its final processed form that is required for training machine learning algorithms. We use transforms to perform some manipulation of the data and make it suitable for training.
All TorchVision datasets have two parameters -
transform
to modify the features andtarget_transform
to modify the labels - that accept callables containing the transformation logic. The torchvision.transforms module offers several commonly-used transforms out of the box.The FashionMNIST features are in PIL Image format, and the labels are integers. For training, we need the features as normalized tensors, and the labels as one-hot encoded tensors. To make these transformations, we use
ToTensor
andLambda
.我们从磁盘读取图片和标签,这些图片和标签无法直接喂入神经网路中,传入照片以后,会通过Transforme对图片做一些约束和一些变换,使其满足神经网络所需要输入的要求。一般在
DataSet
中定义,在getItem
中应用。target_transform也是一样的,假设我们的标签读取的是整型的,但我们希望神经网络的输出是一个
one-hot
的形式以更方便的计算交叉熵,这个时候,就将从磁盘中读取的整型标签进行一个变换,变为one-hot
编码的格式。
Build the Neural Network
Neural networks comprise of layers/modules that perform operations on data. The torch.nn namespace provides all the building blocks you need to build your own neural network. Every module in PyTorch subclasses the nn.Module. A neural network is a module itself that consists of other modules (layers). This nested structure allows for building and managing complex architectures easily.
Get Device for Training
We want to be able to train our model on a hardware accelerator like the GPU or MPS, if available. Let’s check to see if torch.cuda or torch.backends.mps are available, otherwise we use the CPU.
Define the Class
We define our neural network by subclassingnn.Module
, and initialize the neural network layers in__init__
. Everynn.Module
subclass implements the operations on input data in theforward
method.
We create an instance of NeuralNetwork
, and move it to the device
, and print its structure.
若想查看每一层的参数数目(可训练+不可训练)
sksq96/pytorch-summary: Model summary in PyTorch similar to model.summary()
in Keras (github.com)
To use the model, we pass it the input data. This executes the model’s
forward
, along with some background operations. Do not callmodel.forward()
directly!Calling the model on the input returns a 2-dimensional tensor with dim=0 corresponding to each output of 10 raw predicted values for each class, and dim=1 corresponding to the individual values of each output. We get the prediction probabilities by passing it through an instance of the
nn.Softmax
module.
Model Layers
Let’s break down the layers in the FashionMNIST model. To illustrate it, we will take a sample minibatch of 3 images of size 28x28 and see what happens to it as we pass it through the network.
nn.Flatten
We initialize the nn.Flatten layer to convert each 2D 28x28 image into a contiguous array of 784 pixel values ( the minibatch dimension (at dim=0) is maintained).
CLASStorch.nn.Flatten(start_dim=1, end_dim=-1)[SOURCE]
- start_dim (int) – first dim to flatten (default = 1).
- end_dim (int) – last dim to flatten (default = -1).
Examples::
nn.Linear
The linear layer is a module that applies a linear transformation on the input using its stored weights and biases.
CLASStorch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None)[SOURCE]
Parameters
nn.ReLU
Non-linear activations are what create the complex mappings between the model’s inputs and outputs. They are applied after linear transformations to introduce nonlinearity, helping neural networks learn a wide variety of phenomena.
In this model, we use nn.ReLU between our linear layers, but there’s other activations to introduce non-linearity in your model.(非线性激活,增强模型的表达能力)
CLASStorch.nn.ReLU(inplace=False)[SOURCE]
Parameters
inplace (bool) – can optionally do the operation in-place. Default:
False
Shape:
- Input: (∗)(∗), where ∗∗ means any number of dimensions.
- Output: (∗)(∗), same shape as the input.
nn.Sequential
nn.Sequential is an ordered container of modules. (关于模块的一个有序的容器)The data is passed through all the modules in the same order as defined. You can use sequential containers to put together a quick network like seq_modules
.(当我们把一些model作为Sequential的实例化参数后,这些数据就会有序的经过这些模块,最终算出一个结果)
CLASStorch.nn.Sequential(args: Module)[SOURCE]*
A sequential container.
Modules will be added to it in the order they are passed in the constructor. Alternatively, an
OrderedDict
of modules can be passed in. Theforward()
method ofSequential
accepts any input and forwards it to the first module it contains. It then “chains” outputs to inputs sequentially for each subsequent module, finally returning the output of the last module.The value a
Sequential
provides over manually calling a sequence of modules is that it allows treating the whole container as a single module, such that performing a transformation on theSequential
applies to each of the modules it stores (which are each a registered submodule of theSequential
).What’s the difference between a
Sequential
and atorch.nn.ModuleList
? AModuleList
is exactly what it sounds like–a list for storingModule
s! On the other hand, the layers in aSequential
are connected in a cascading way.Example:
nn.Softmax
The last linear layer of the neural network returns logits - raw values in [-infty, infty] - which are passed to the nn.Softmax module. The logits are scaled to values [0, 1] representing the model’s predicted probabilities for each class. dim
parameter indicates the dimension along which the values must sum to 1.
CLASStorch.nn.Softmax(dim=None)[SOURCE](仅对某一维度进行softmax)
Shape:
- Input: (∗)(∗) where * means, any number of additional dimensions
- Output: (∗)(∗), same shape as the input
Returns
a Tensor of the same dimension and shape as the input with values in the range [0, 1]
Parameters
dim (int) – A dimension along which Softmax will be computed (so every slice along dim will sum to 1).
Return type
None
Examples:
Model Parameters
Many layers inside a neural network are parameterized, i.e. have associated weights and biases that are optimized during training. Subclassing
nn.Module
automatically tracks all fields defined inside your model object, and makes all parameters accessible using your model’sparameters()
ornamed_parameters()
methods.In this example, we iterate over each parameter, and print its size and a preview of its values.
还不快抢沙发