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)

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

Step: 3

blur-text-image

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

Expert Performance Indexing In SQL Server

Authors: Jason Strate, Grant Fritchey

2nd Edition

1484211189, 9781484211182

More Books

Students also viewed these Databases questions

Question

Explain the relationship between thoughts, feelings, and actions.

Answered: 1 week ago