Untitled
unknown
plain_text
25 days ago
2.2 kB
3
Indexable
Never
class Layer: """Implement a class for a neural network layer. The class should implement the forward and backward passes of the layer. """ def __init__(self): """Implement the initialization of the layer parameters. Arguments: None Returns: None """ # Parameters of the layer. Should be initialized randomly. self.w = np.random.randn() self.b = np.random.randn() # Activation function for the layer. self.activation = relu self.activation_derivative = relu_derivative # Attributes to store the input and output of a layer during forward pass, for use in the backward passes. self.last_x = None self.last_a = None def forward(self, x): """Implement the forward pass of the layer. Arguments: x - the input to the layer, scalar of type 'float' Returns: a - the output of the layer, scalar of type 'float' """ # Compute the linear transformation a = self.w * x + self.b # Store the input and output for the backward pass self.last_x = x self.last_a = a return a def backward(self, de_da, step_size=0.01): """Implement the backward pass of the layer. The backward pass should update the parameters of the layer and return the derivative of the error with respect to the input of the layer. Arguments: de_da - the derivative of the error with respect to the output of the layer, scalar of type 'float' step_size - the step size for the update step, scalar of type 'float' Returns: de_dx - the derivative of the error with respect to the input of the layer, scalar of type 'float' """ # Compute the gradient with respect to w, b, and x dw = de_da * self.last_x # Derivative w.r.t. w db = de_da # Derivative w.r.t. b de_dx = de_da * self.w # Derivative w.r.t. input x # Update weights and biases self.w -= step_size * dw self.b -= step_size * db return de_dx
Leave a Comment