Pytorch

PyTorch is a machine learning library based on the Torch library, used for applications such as computer vision and natural language processing.

Resources

Running PyTorch on GPU

Tensors

Dimensions of Tensors

tensor https://hackmd.io/@NCCU111356040/rkZiVbYcs

Linear layers

nn.Linear

Applies an affine linear transformation y = W * x + b

# Create a linear layer with 10 in features and out features
linear_layer = nn.Linear(in_features=10,
                         out_features=10)

Linear Layer Dimensions

In PyTorch, a linear layer is defined as:

nn.Linear(in_features, out_features)
  • in_features is the size of each input sample.
  • out_features is the size of each output sample.

picture 1

Ex) For a nn.Linear layer with dimensions (4, 3): picture 3

The operation is y = W * x + b:
where ( W ) is (3, 4) and ( x ) is (4,), resulting in ( y ) of shape (3,).

Embededding Layer

  • torch.nn.Embedding | A simple lookup table that stores embeddings of a fixed dictionary and size.
    • outputs a tensor with the shape (batch_size, input_size, embed_dim)

Embeddings, in the context of machine learning and natural language processing, are dense vector representations of categorical data, such as words, that capture their semantic meaning. Embeddings map words to a continuous,low-dimensional space, continuous vector of real numbers, where words with similar meanings are close to each other.