-
Notifications
You must be signed in to change notification settings - Fork 0
Tutorial 4: Car Neural Network
Important
This tutorial starts at the workshop-tutorial-3 branch and the complete solution can be found at workshop-tutorial-4 branch.
This tutorial focuses on the implementation of the CarNN class, which contains the main logics for the neural network.
Note
This tutorial will implement/update the following modules:
The CarNN class is already partially implemented as we see in the file, particularlly:
-
serialize: turns the neural network's weights into string so that it can be saved to files. -
deserialize: create the neural network from the given activation function, weights string, and the initial mutation noise to be passed tomutatefunction which we will implement later.
But we also need to implement the __init__ function before actually implementing other functions of the neural network.
The prev_fitness and prev_weights members, and the activation function are rather obvious to what they should be initialized to. However, the weights, the layer_sizes, and the hiddens members need a bit more design.
When weights is provided, we can actually find out the layer_sizes from it (by checking its len and shape). On the other hand, if weights is not provided, then the user must provide the layer_sizes so that we can initialize the weights to some specific sizes.
To find out what should the size of weights be, we need to understand how neural network works, but in essense weights are the linkage between each node in the previous layer to each node in the next layer. Due to mathematicaly reasons, the previous layer node will have an additional node than the actual number of hidden node. See below for a pseudocode presenting the use of weights.
for j, prev_node in [prev[0], prev[1], ..., prev[layer_sizes[i - 1]], additional]:
for k, curr_node in [curr[0], curr[1], ..., curr[layer_sizes[i]]]:
weights[i][j][k] is the link of prev_node and curr_nodeNote
The exact mathematical reason is explained in the Feedforward Neural Network section.
Then after finding layer_sizes, the hiddens are just the layer_sizes except the input and the output layer (i.e. the middle layers).
def __init__(
self,
activation: ActivationFunc,
weights: Optional[List[np.ndarray]] = None,
layer_sizes: Optional[List[int]] = None,
):
"""
Initialize the neural network.
Either `weights` or `layer_sizes` must be provided.
:param activation: The activation function
:param weights: The weights of the neural network
:param layer_sizes: The sizes of the layers
"""
# Check if either weights or layer_sizes is provided
if not weights and not layer_sizes:
raise ValueError("Either weights or layer_sizes must be provided")
# There is no previous fitness and weights, so they are None
self.prev_fitness = None
self.prev_weights = None
# Initialize the either with the given weight, or using a normal
# distribution
#
# The width should be the previous size + 1 for (x_0, x_1, ..., x_n, b)
# and the height should be the current layer size
self.weights = weights or [
np.random.normal(size=(_____, _____))
for prev_size, curr_size in zip(layer_sizes, layer_sizes[1:])
]
# Initialize the layer sizes by using the given layer sizes, or
# calculating from the weights
self.layer_sizes = layer_sizes or (
[
weights[0].shape[0] - 1,
*(weight.shape[1] for weight in weights),
]
)
# Initialize the hidden layers
#
# Since the initial values are not important, we can just use zeros
self.hiddens = [
np.array([0] * size, dtype=np.float32)
for size in self.layer_sizes[_____:_____]
]
# Assign the activation function
self.activation = activationSolution Code
def __init__(
self,
activation: ActivationFunc,
weights: Optional[List[np.ndarray]] = None,
layer_sizes: Optional[List[int]] = None,
):
"""
Initialize the neural network.
Either `weights` or `layer_sizes` must be provided.
:param activation: The activation function
:param weights: The weights of the neural network
:param layer_sizes: The sizes of the layers
"""
# Check if either weights or layer_sizes is provided
if not weights and not layer_sizes:
raise ValueError("Either weights or layer_sizes must be provided")
# There is no previous fitness and weights, so they are None
self.prev_fitness = None
self.prev_weights = None
# Initialize the either with the given weight, or using a normal
# distribution
#
# The width should be the previous size + 1 for (x_0, x_1, ..., x_n, b)
# and the height should be the current layer size
self.weights = weights or [
np.random.normal(size=(prev_size + 1, curr_size))
for prev_size, curr_size in zip(layer_sizes, layer_sizes[1:])
]
# Initialize the layer sizes by using the given layer sizes, or
# calculating from the weights
self.layer_sizes = layer_sizes or (
[
weights[0].shape[0] - 1,
*(weight.shape[1] for weight in weights),
]
)
# Initialize the hidden layers
#
# Since the initial values are not important, we can just use zeros
self.hiddens = [
np.array([0] * size, dtype=np.float32)
for size in self.layer_sizes[1:-1]
]
# Assign the activation function
self.activation = activationFeedforward neural network refers to neural networks which obtains the value of the next layer's nodes based on the previous nodes value multiplied by some weights.
More specifically, to find the value of the node in the next layer (
And in reality, we also add a bias (
The most simple form of a feedforward neural network is a linear function in the form of:
Or commonly written as:
Where the
But when there are more
But when there are also multiple
But we can make the formula more simple by adding a
In pseudocode, the above formula will be expressed to:
for j in 0..m
y[j] = 0
for i in 0..n
y[j] += w[i][j] * x[i]Now that we know
If we represent