Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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)
sc1.py script:
import torch
import torch.nn as nn
# ==== Put your solutions here ====
# Each function receives an nn.Linear.
# Hint #1: Make sure you print out the shape of the weights
# Hint #2: You can fill multiple weights at once by assigning
# the weights to a tensor. e.g.
# lin.weight.data[:]= torch.tensor([
# [1,0],
# [0,1],
# ], dtype=lin.weight.dtype, device=lin.weight.device)
# ==== Testing code: Tests your solutions ====
def test_1(fnc):
inp = torch.tensor([1.,5,11,20,21]).reshape([-1,1])
tar = torch.tensor([2.,6,12,21,22]).reshape([-1,1])
layer = nn.Linear(1,1)
fnc(layer)
out = layer(inp)
torch.allclose(out, tar)
def test_2(fnc):
inp = torch.tensor([1.,5,11,20,21]).reshape([-1,1])
tar = torch.tensor([5.,17,35,62,65]).reshape([-1,1])
layer = nn.Linear(1,1)
fnc(layer)
out = layer(inp)
torch.allclose(out, tar)
def test_3(fnc):
inp = torch.tensor([
[1.,1,1,1],
[5,10,15,20],
[11,20,21,25]
])
tar = inp.mean(dim=1, keepdim=True)
layer = nn.Linear(4,1)
fnc(layer)
out = layer(inp)
torch.allclose(out, tar)
def test_4(fnc):
inp = torch.tensor([
[1.,1,1,1],
[5,10,15,20],
[11,20,21,25]
])
tar = torch.stack([
inp.mean(dim=1),
inp.sum(dim=1)
], dim=1)
layer = nn.Linear(4,2)
fnc(layer)
out = layer(inp)
torch.allclose(out, tar)
def test_5(fnc):
inp = torch.tensor([
[1.,1,1],
[5,10,15],
[11,20,21],
[4,12,2],
[6,5,4],
])
tar = torch.tensor([
[1.,1,1],
[15,10,5],
[21,20,11],
[2,12,4],
[4,5,6],
])
layer = nn.Linear(3,3)
fnc(layer)
out = layer(inp)
torch.allclose(out, tar)
def test_6(fnc):
inp = torch.tensor([
[1.,2,3,4,5],
[1e5,2e10,3e15,4e20,5e25],
[-150,150,15,-15,0.1]
])
tar = torch.tensor([
[4.,2],
[4,2],
[4,2],
])
layer = nn.Linear(5,2)
fnc(layer)
out = layer(inp)
torch.allclose(out, tar)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions