Implementing an Autoencoder in PyTorch
Building an autoencoder model for reconstruction
Jan 26 ·5min read
T his is the PyTorch equivalent of my previous article on implementing an autoencoder in TensorFlow 2.0, which you may read through the following link,
First, to install PyTorch, you may use the following pip
command,
pip install torch torchvision
The torchvision
package contains the image data sets that are ready for use in PyTorch.
More details on its installation through this guide from pytorch.org .
Autoencoder
Since the linked article above already explains what is an autoencoder, we will only briefly discuss what it is.
An autoencoder is a type of neural network that finds the function mapping the features x to itself. This objective is known as reconstruction , and an autoencoder accomplishes this through the following process: (1) an encoder learns the data representation in lower-dimension space, i.e. extracting the most salient features of the data, and (2) a decoder learns to reconstruct the original data based on the learned representation by the encoder.
Mathematically, process (1) learns the data representation z from the input features x , which then serves as an input to the decoder.
Then, process (2) tries to reconstruct the data based on the learned data representation z .
The encoder and the decoder are neural networks that build the autoencoder model, as depicted in the following figure,
To simplify the implementation, we write the encoder and decoder layers in one class as follows,
Explaining some of the components in the code snippet above,
- The
torch.nn.Linear
layer creates a linear function (θx + b
), with its parameters initialized (by default) with He/Kaiming uniform initialization , as it can be confirmed here . This means we will call an activation/non-linearity for such layers. - The
in_features
parameter dictates the feature size of the input tensor to a particular layer, e.g. inself.encoder_hidden_layer
, it accepts an input tensor with the size of[N, input_shape]
whereN
is the number of examples, andinput_shape
is the number of features in one example. - The
out_features
parameter dictates the feature size of the output tensor of a particular layer. Hence, in theself.decoder_output_layer
, the feature size iskwargs["input_shape"]
, denoting that it reconstructs the original data input. - The
forward()
function defines the forward pass for a model, similar tocall
intf.keras.Model
. This is the function invoked when we pass input tensors to an instantiated object of atorch.nn.Module
class.
To optimize our autoencoder to reconstruct data, we minimize the following reconstruction loss,
We instantiate an autoencoder class, and move (using the to()
function) its parameters to a torch.device
, which may be a GPU ( cuda
device, if one exists in your system) or a CPU (lines 2 and 6 in the code snippet below).
Then, we create an optimizer object (line 10) that will be used to minimize our reconstruction loss (line 13).
For this article, let’s use our favorite dataset, MNIST. In the following code snippet, we load the MNIST dataset as tensors using the torchvision.transforms.ToTensor()
class. The dataset is downloaded ( download=True
) to the specified directory ( root=<directory>
) when it is not yet present in our system.
After loading the dataset, we create a torch.utils.data.DataLoader
object for it, which will be used in model computations.
Finally, we can train our model for a specified number of epochs as follows,
In our data loader, we only need to get the features since our goal is reconstruction using autoencoder (i.e. an unsupervised learning goal). The features loaded are 3D tensors by default, e.g. for the training data, its size is [60000, 28, 28]
. Since we defined our in_features
for the encoder layer above as the number of features, we pass 2D tensors to the model by reshaping batch_features
using the .view(-1, 784)
function (think of this as np.reshape()
in NumPy), where 784 is the size for a flattened image with 28 by 28 pixels such as MNIST.
At each epoch, we reset the gradients back to zero by using optimizer.zero_grad()
, since PyTorch accumulates gradients on subsequent passes. Of course, we compute a reconstruction on the training examples by calling our model on it, i.e. outputs = model(batch_features)
. Subsequently, we compute the reconstruction loss on the training examples, and perform backpropagation of errors with train_loss.backward()
, and optimize our model with optimizer.step()
based on the current gradients computed using the .backward()
function call.
To see how our training is going, we accumulate the training loss for each epoch ( loss += training_loss.item()
), and compute the average training loss across an epoch ( loss = loss / len(train_loader)
).
Results
For this article, the autoencoder model was trained for 20 epochs, and the following figure plots the original (top) and reconstructed (bottom) MNIST images.
matplotlib
. Results on MNIST handwritten digit dataset. Images at the top row are the original ones while images at the bottom row are the reconstructed ones.
In case you want to try this autoencoder on other datasets, you can take a look at the available image datasets from torchvision
.
Closing Remarks
I hope this has been a clear tutorial on implementing an autoencoder in PyTorch. To further improve the reconstruction capability of our implemented autoencoder, you may try to use convolutional layers ( torch.nn.Conv2d
) to build a convolutional neural network-based autoencoder.
The corresponding notebook to this article is available here . In case you have any feedback, you may reach me through Twitter .
References
- A.F. Agarap, Implementing an Autoencoder in TensorFlow 2.0 (2019). Towards Data Science.
- I. Goodfellow, Y. Bengio, & A. Courville, Deep learning (2016). MIT press.
- A. Paszke, et al. PyTorch: An imperative style, high-performance deep learning library (2019). Advances in Neural Information Processing Systems.
- PyTorch Documentation. https://pytorch.org/docs/stable/nn.html .
以上所述就是小编给大家介绍的《Implementing an Autoencoder in PyTorch》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
.NET框架程序设计
(美)Jeffrey Richter、(美)Francesco Balena / 李建忠 / 华中科技大学出版社 / 2004-1 / 54.00元
Microsoft.NET框架为简化开发与卫联网无缝连接的应用程序和组件提供了强大的技术支持,如ASP.NET Web窗体、XML Web服务以及Windows窗体。本书的目的在于展示.NET框架中公共语言运行库存的核心内容。全书由两位广受尊敬的开发者/作者完成,并假设读者理解面向对象程序设计的基本概念,如数据抽象、继承和多态。书中内容清楚地解释了CLR的扩展类型系统,CLR如何管理类型的行为,以......一起来看看 《.NET框架程序设计》 这本书的介绍吧!