class Module(object):
"""
Basically, you can think of a module as of a something (black box)
which can process input data and produce ouput data.
This is like applying a function which is called forward:
code
Assistant
output = module.forward(input)
The module should be able to perform a backward pass: to differentiate the `forward` function.
Moreover, it should be able to differentiate it if is a part of chain (chain rule).
The latter implies there is a gradient from previous step of a chain rule.
input_grad = module.backward(input, output_grad)
"""
def __init__ (self):
self._output = None
self._input_grad = None
self.training = True
def forward(self, input):
"""
Takes an input object, and computes the corresponding output of the module.
"""
self._output = self._compute_output(input)
return self._output
def backward(self, input, output_grad):
"""
Performs a backpropagation step through the module, with respect to the given input.
This includes
- computing a gradient w.r.t. `input` (is needed for further backprop),
- computing a gradient w.r.t. parameters (to update parameters while optimizing).
"""
self._input_grad = self._compute_input_grad(input, output_grad)
self._update_parameters_grad(input, output_grad)
return self._input_grad
def _compute_output(self, input):
"""
Computes the output using the current parameter set of the class and input.
This function returns the result which will be stored in the `_output` field.
Example: in case of identity operation:
output = input
return output
"""
raise NotImplementedError
def _compute_input_grad(self, input, output_grad):
"""
Returns the gradient of the module with respect to its own input.
The shape of the returned value is always the same as the shape of `input`.
Example: in case of identity operation:
input_grad = output_grad
return input_grad
"""
raise NotImplementedError
def _update_parameters_grad(self, input, output_grad):
"""
Computing the gradient of the module with respect to its own parameters.
No need to override if module has no parameters (e.g. ReLU).
"""
pass
def zero_grad(self):
"""
Zeroes `gradParams` variable if the module has params.
"""
pass
def get_parameters(self):
"""
Returns a list with its parameters.
If the module does not have parameters return empty list.
"""
return []
def get_parameters_grad(self):
"""
Returns a list with gradients with respect to its parameters.
If the module does not have parameters return empty list.
"""
return []
def train(self):
"""
Sets training mode for the module.
Training and testing behaviour differs for Dropout, BatchNorm.
"""
self.training = True
def evaluate(self):
"""
Sets evaluation mode for the module.
Training and testing behaviour differs for Dropout, BatchNorm.
"""
self.training = False
def __repr__(self):
"""
Pretty printing. Should be overrided in every module if you want
to have readable description.
"""
return "Module"
Conv2d
input: batch_size x in_channels x h x w
output: batch_size x out_channels x h x w
You should implement something like pytorch Conv2d layer with stride=1 and zero-padding outside of image using scipy.signal.correlate function.
Practical notes:
While the layer name is "convolution", the most of neural network frameworks (including tensorflow and pytorch) implement operation that is called correlation in signal processing theory. So don't usescipy.signal.convolve since it implements convolution in terms of signal processing.
It may be convenient to use numpy.pad for zero-padding.
It's rather ok to implement convolution over 4d array using 2 nested loops: one over batch size dimension and another one over output filters dimension
Нужно дописать следующий модуль:
import scipy as sp
import scipy.signal
import skimage
Here is the completed implementation for the Conv2d module based on the provided instructions and practical notes:
python
import numpy as np
import scipy.signal
class Conv2d(Module):
def __init__(self, in_channels, out_channels, kernel_size):
super(Conv2d, self).__init__()
assert kernel_size % 2 == 1, kernel_size
stdv = 1. / np.sqrt(in_channels * kernel_size * kernel_size)
self.W = np.random.uniform(-stdv, stdv, size=(out_channels, in_channels, kernel_size, kernel_size))
self.b = np.random.uniform(-stdv, stdv, size=(out_channels,))
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.gradW = np.zeros_like(self.W)
self.gradb = np.zeros_like(self.b)
def _compute_output(self, input):
"""
Compute the forward pass of the convolution operation.
"""
pad_size = self.kernel_size // 2
batch_size, in_channels, h, w = input.shape
assert in_channels == self.in_channels, "Input channels mismatch"
# Zero-padding the input
padded_input = np.pad(input,
pad_width=((0, 0), (0, 0), (pad_size, pad_size), (pad_size, pad_size)),
mode='constant',
constant_values=0)
# Initialize the output tensor
output = np.zeros((batch_size, self.out_channels, h, w))
# Perform correlation (or cross-correlation)
for n in range(batch_size):
for oc in range(self.out_channels):
for ic in range(self.in_channels):
output[n, oc] += scipy.signal.correlate(
padded_input[n, ic],
self.W[oc, ic],
mode='valid'
)
output[n, oc] += self.b[oc] # Add bias
self._output = output
return self._output
def _compute_input_grad(self, input, gradOutput):
"""
Compute the gradient of the input with respect to the loss during backpropagation.
"""
pad_size = self.kernel_size // 2
batch_size, in_channels, h, w = input.shape
# Zero-pad gradOutput
padded_grad_output = np.pad(gradOutput,
pad_width=((0, 0), (0, 0), (pad_size, pad_size), (pad_size, pad_size)),
mode='constant',
constant_values=0)
# Initialize the input gradient tensor
input_grad = np.zeros_like(input)
# Compute the input gradient
for n in range(batch_size):
for ic in range(self.in_channels):
for oc in range(self.out_channels):
input_grad[n, ic] += scipy.signal.correlate(
padded_grad_output[n, oc],
self.W[oc, ic][::-1, ::-1], # Flip the kernel
mode='valid'
)
self._input_grad = input_grad
return self._input_grad
def accGradParameters(self, input, gradOutput):
"""
Accumulate the gradients with respect to the weights and biases.
"""
pad_size = self.kernel_size // 2
batch_size, in_channels, h, w = input.shape
# Zero-pad input
padded_input = np.pad(input,
pad_width=((0, 0), (0, 0), (pad_size, pad_size), (pad_size, pad_size)),
mode='constant',
constant_values=0)
# Compute weight gradients
for oc in range(self.out_channels):
for ic in range(self.in_channels):
for n in range(batch_size):
self.gradW[oc, ic] += scipy.signal.correlate(
padded_input[n, ic],
gradOutput[n, oc], # Gradients from the next layer
mode='valid'
)
# Compute bias gradients (sum over batch and all spatial dimensions)
self.gradb = np.sum(gradOutput, axis=(0, 2, 3))
def zeroGradParameters(self):
"""
Zero the stored gradients for weights and biases.
"""
self.gradW.fill(0)
self.gradb.fill(0)
def getParameters(self):
"""
Return the list of parameters (weights and biases).
"""
return [self.W, self.b]
def getGradParameters(self):
"""
Return the list of gradients for parameters.
"""
return [self.gradW, self.gradb]
def __repr__(self):
"""
Pretty representation of the Conv2d module.
"""
s = self.W.shape
return f'Conv2d {s[1]} -> {s[0]}'
Explanation of Key Steps:
Forward Pass (_compute_output):
Zero-pad the input to maintain the spatial dimensions (same padding).
Use scipy.signal.correlate to perform cross-correlation/correlation. This is effectively the "convolution-like" operation, but without flipping the kernel.
Backward Pass (_compute_input_grad):
Compute the gradient of the input using the flipped kernel and gradOutput.
Zero-pad the gradOutput during calculations to ensure proper correlation computation.
Accumulate Gradients (accGradParameters):
Compute gradients for W by correlating the (zero-padded) input with gradOutput.
Compute gradients for b by summing gradOutput over batch and spatial dimensions.
Efficiency:
This implementation uses loops over batches and output channels, but avoids unnecessary computations for efficiency.
This should emulate the behavior of PyTorch's Conv2d layer with stride=1 and padding='same'.