Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Custom Models, Layers, and Loss Functions with TensorFlow/Week 3 - Custom Layers/C1W3_Assignment.ipynb
Views: 13373
Week 3 Assignment: Implement a Quadratic Layer
In this week's programming exercise, you will build a custom quadratic layer which computes . Similar to the ungraded lab, this layer will be plugged into a model that will be trained on the MNIST dataset. Let's get started!
Imports
Define the quadratic layer (TODO)
Implement a simple quadratic layer. It has 3 state variables: , and . The computation returned is . Make sure it can also accept an activation function.
__init__
call
super(my_fun, self)
to access the base class ofmy_fun
, and call the__init__()
function to initialize that base class. In this case,my_fun
isSimpleQuadratic
and its base class isLayer
.self.units: set this using one of the function parameters.
self.activation: The function parameter
activation
will be passed in as a string. To get the tensorflow object associated with the string, please usetf.keras.activations.get()
build
The following are suggested steps for writing your code. If you prefer to use fewer lines to implement it, feel free to do so. Either way, you'll want to set self.a
, self.b
and self.c
.
a_init: set this to tensorflow's
random_normal_initializer()
a_init_val: Use the
random_normal_initializer()
that you just created and invoke it, setting theshape
anddtype
.The
shape
ofa
should have its row dimension equal to the last dimension ofinput_shape
, and its column dimension equal to the number of units in the layer.This is because you'll be matrix multiplying x^2 * a, so the dimensions should be compatible.
set the dtype to 'float32'
self.a: create a tensor using tf.Variable, setting the initial_value and set trainable to True.
b_init, b_init_val, and self.b: these will be set in the same way that you implemented a_init, a_init_val and self.a
c_init: set this to
tf.zeros_initializer
.c_init_val: Set this by calling the tf.zeros_initializer that you just instantiated, and set the
shape
anddtype
shape: This will be a vector equal to the number of units. This expects a tuple, and remember that a tuple
(9,)
includes a comma.dtype: set to 'float32'.
self.c: create a tensor using tf.Variable, and set the parameters
initial_value
andtrainable
.
call
The following section performs the multiplication x^2a + xb + c. The steps are broken down for clarity, but you can also perform this calculation in fewer lines if you prefer.
x_squared: use tf.math.square()
x_squared_times_a: use tf.matmul().
If you see an error saying
InvalidArgumentError: Matrix size-incompatible
, please check the order of the matrix multiplication to make sure that the matrix dimensions line up.
x_times_b: use tf.matmul().
x2a_plus_xb_plus_c: add the three terms together.
activated_x2a_plus_xb_plus_c: apply the class's
activation
to the sum of the three terms.
Test your implementation
All public tests passed
Train your model with the SimpleQuadratic
layer that you just implemented.