YohanRoth
YohanRoth

Reputation: 3253

Affine Maps PyTorch

I do not understand the following part of a tutorial (see pic below). I am not clear about this part of code

lin = nn.Linear(5, 3)  # maps from R^5 to R^3, parameters A, b
# data is 2x5.  A maps from 5 to 3... can we map "data" under A?
data = autograd.Variable(torch.randn(2, 5))
print(lin(data))  # yes

We defined Linear layer that has 5 inputs and 3 output? and then we feed matrix of size 2x5? I do not understand what is going here...

and the idea that they want to deliver in general

enter image description here

Upvotes: 1

Views: 393

Answers (1)

mbpaulus
mbpaulus

Reputation: 7691

The code snippet you provided is fairly simple.

The input to a nn.Linear()is of dimension batch_size x feat_dim. If you feed in data = Variable(torch.randn(2, 5)), you feed in a mini-batch that contains 2 data instances. Each data instance is a five-dimensional vector. For each data instance, you use the same weight matrix of your linear layer, to map their five-dimensional vector to a 3-dimensional vector. Thus your linear layer returns a matrix of dimension 2 x 3, i.e. batch_size x new_feat_dim

You can feed mini-batches of any size to your linear layer.

Upvotes: 1

Related Questions