Question: This challenge task is quite difficult and will really test your mastery of PyTorch and nn . Linear layers. We can manually assign weights to

This challenge task is quite difficult and will really test your mastery of PyTorch and nn.Linear layers.
We can manually assign weights to an nn.Linear like this:
import torch
import torch.nn as nn
lin = nn.Linear(10,20)
manual_weights = torch.arange(20*10).reshape(lin.weight.shape)
lin.weight.data[:]= manual_weights
lin.bias.data[:]=0
But this does not calculate anything useful. A Linear layer simply performs a weighted sum (plus bias). We can choose weights/biases to perform known operations.
INSTRUCTIONS:
Given an nn.Linear(1,1) layer, set the weights such that the layer adds 1 to it's input.
Given an nn.Linear(1,1) layer, set the weights such that the layer calculates y =3x +2.
Given an nn.Linear(4,1) layer, set the weights such that the layer calculates the average of it's inputs.
Given an nn.Linear(4,2) layer, set the weights such that the layer calculates both the average of it's inputs and the sum of the inputs.
Given an nn.Linear(3,3) layer, set the weights such that the layer returns the inputs, but in reverse order.
Given an nn.Linear(5,2) layer, set the weights such that the layer always returns (4,2)
Note: We would never use this in a deep learning model; this challenge is to prove that you understand the mathematics and coding mechanics of the nn.Linear layer.
import sc1
sc1.test_1(sc1.modify_lin_1)
sc1.test_2(sc1.modify_lin_2)
sc1.test_3(sc1.modify_lin_3)
sc1.test_4(sc1.modify_lin_4)
sc1.test_5(sc1.modify_lin_5)
sc1.test_6(sc1.modify_lin_6)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!