Part 3 (10 points, coding task)
In this part, you are asked to build an affine transformation module from scratch by using NumPy, NOT PyTorch or TensorFlow.
Define such a class as My_Linear_NumPy
.
-
Attributes
-
in_features
: Number of input features -
out_features
: Number of output features -
weight
: This refers to matrix \mathbf{W} in Part 1. The shape is(out_features, in_features)
. -
bias
: This refers to vector \mathbf{b} in Part 1. The shape is(out_features,)
. -
random_seed
: The NumPy random seed number used to generate initial values ofweight
andbias
.
-
-
Method
__init__
:-
To initialize an object in this class, you need to specify
in_features
andout_features
. -
You may initialize the object by specifying a value for
random_seed
. If it is not specified, then its default value is 42. -
The initial values of
weight
andbias
are random that follow standard normal distributions generated with the seed number attributerandom_seed
.
-
-
Method
forward
:-
Input
x
: numpy array with shape(n_0, n_1, ..., n_{d-1}, in_features)
with an arbitrary dimension d = 0, 1, \cdots. -
Output
y
: numpy array with shape(n_0, n_1, ..., n_{d-1}, out_features)
. -
The affine transformation works in a way that given the first d indices in
x
andy
, it does affine transformation along the last axis ofx
andy
.
-
-
Do not use any loop in your code.