mlpractical/mlp/conv.py

126 lines
4.3 KiB
Python
Raw Normal View History

2015-11-15 17:33:53 +01:00
# Machine Learning Practical (INFR11119),
# Pawel Swietojanski, University of Edinburgh
import numpy
import logging
from mlp.layers import Layer
logger = logging.getLogger(__name__)
"""
You have been given some very initial skeleton below. Feel free to build on top of it and/or
modify it according to your needs. Just notice, you can factor out the convolution code out of
the layer code, and just pass (possibly) different conv implementations for each of the stages
in the model where you are expected to apply the convolutional operator. This will allow you to
keep the layer implementation independent of conv operator implementation, and you can easily
swap it layer, for example, for more efficient implementation if you came up with one, etc.
"""
def my1_conv2d(image, kernels, strides=(1, 1)):
"""
Implements a 2d valid convolution of kernels with the image
Note: filer means the same as kernel and convolution (correlation) of those with the input space
produces feature maps (sometimes refereed to also as receptive fields). Also note, that
feature maps are synonyms here to channels, and as such num_inp_channels == num_inp_feat_maps
:param image: 4D tensor of sizes (batch_size, num_input_channels, img_shape_x, img_shape_y)
:param filters: 4D tensor of filters of size (num_inp_feat_maps, num_out_feat_maps, kernel_shape_x, kernel_shape_y)
:param strides: a tuple (stride_x, stride_y), specifying the shift of the kernels in x and y dimensions
:return: 4D tensor of size (batch_size, num_out_feature_maps, feature_map_shape_x, feature_map_shape_y)
"""
raise NotImplementedError('Write me!')
class ConvLinear(Layer):
def __init__(self,
num_inp_feat_maps,
num_out_feat_maps,
image_shape=(28, 28),
kernel_shape=(5, 5),
stride=(1, 1),
irange=0.2,
rng=None,
conv_fwd=my1_conv2d,
conv_bck=my1_conv2d,
conv_grad=my1_conv2d):
"""
:param num_inp_feat_maps: int, a number of input feature maps (channels)
:param num_out_feat_maps: int, a number of output feature maps (channels)
:param image_shape: tuple, a shape of the image
:param kernel_shape: tuple, a shape of the kernel
:param stride: tuple, shift of kernels in both dimensions
:param irange: float, initial range of the parameters
:param rng: RandomState object, random number generator
:param conv_fwd: handle to a convolution function used in fwd-prop
:param conv_bck: handle to a convolution function used in backward-prop
:param conv_grad: handle to a convolution function used in pgrads
:return:
"""
super(ConvLinear, self).__init__(rng=rng)
raise NotImplementedError()
def fprop(self, inputs):
raise NotImplementedError()
def bprop(self, h, igrads):
raise NotImplementedError()
def bprop_cost(self, h, igrads, cost):
raise NotImplementedError('ConvLinear.bprop_cost method not implemented')
def pgrads(self, inputs, deltas, l1_weight=0, l2_weight=0):
raise NotImplementedError()
def get_params(self):
raise NotImplementedError()
def set_params(self, params):
raise NotImplementedError()
def get_name(self):
return 'convlinear'
#you can derive here particular non-linear implementations:
#class ConvSigmoid(ConvLinear):
#...
class ConvMaxPool2D(Layer):
def __init__(self,
num_feat_maps,
conv_shape,
pool_shape=(2, 2),
pool_stride=(2, 2)):
"""
:param conv_shape: tuple, a shape of the lower convolutional feature maps output
:param pool_shape: tuple, a shape of pooling operator
:param pool_stride: tuple, a strides for pooling operator
:return:
"""
super(ConvMaxPool2D, self).__init__(rng=None)
raise NotImplementedError()
def fprop(self, inputs):
raise NotImplementedError()
def bprop(self, h, igrads):
raise NotImplementedError()
def get_params(self):
return []
def pgrads(self, inputs, deltas, **kwargs):
return []
def set_params(self, params):
pass
def get_name(self):
return 'convmaxpool2d'