code/conv.py
def forward(self, X):
        
        if not self.padding:
            img = X
        else:
            img = self.layer_with_padding(X)
            
        
        end = self.kernel_size - 1
        output_dim = img.shape[-2] - end, img.shape[-1] - end
        
        conv_imgs = torch.zeros((X.shape[0], X.shape[1], output_dim[0], output_dim[1])).to(device)
        
        for l in range(img.shape[0]):
            for m in range(img.shape[1]):
                temp_img = img[l][m]
                conv_img = list()

                for i in range(img.shape[2] - end):

                    for j in range(img.shape[3] - end):

                        temp_img_view = temp_img[i : i + self.kernel_size, j : j + self.kernel_size]
                        conv_img.append(torch.sum(torch.mul(temp_img_view, self.kernel)))
                        
                conv_img = torch.stack(conv_img)
                conv_img = torch.reshape(conv_img, output_dim)
                conv_img = torch.unsqueeze(conv_img, 0)
            conv_imgs[l] = conv_img
        return conv_imgs

    # def backward(self):
        """
        Backward pass 
        (leave the function commented out,
        so that autograd will be used.
        No action needed here.)
        :return:
        """

        Backward pass 
        (leave the function commented out,
        so that autograd will be used.
        No action needed here.)
        :return:
        
Backward pass
(leave the function commented out,
so that autograd will be used.
No action needed here.)
Backward pass ( leave the function commented out so that autograd will be used . No action needed here . )
def forward(self, X):
        if not self.padding:
            img = X
        else:
            img = self.layer_with_padding(X)
        end = self.kernel_size - 1
        output_dim = img.shape[-2] - end, img.shape[-1] - end
        conv_imgs = torch.zeros((X.shape[0], X.shape[1], output_dim[0], output_dim[1])).to(device)
        for l in range(img.shape[0]):
            for m in range(img.shape[1]):
                temp_img = img[l][m]
                conv_img = list()
                for i in range(img.shape[2] - end):
                    for j in range(img.shape[3] - end):
                        temp_img_view = temp_img[i : i + self.kernel_size, j : j + self.kernel_size]
                        conv_img.append(torch.sum(torch.mul(temp_img_view, self.kernel)))
                conv_img = torch.stack(conv_img)
                conv_img = torch.reshape(conv_img, output_dim)
                conv_img = torch.unsqueeze(conv_img, 0)
            conv_imgs[l] = conv_img
        return conv_imgs
def forward ( self , X ) : if not self . padding : img = X else : img = self . layer_with_padding ( X ) end = self . kernel_size - 1 output_dim = img . shape [ - 2 ] - end , img . shape [ - 1 ] - end conv_imgs = torch . zeros ( ( X . shape [ 0 ] , X . shape [ 1 ] , output_dim [ 0 ] , output_dim [ 1 ] ) ) . to ( device ) for l in range ( img . shape [ 0 ] ) : for m in range ( img . shape [ 1 ] ) : temp_img = img [ l ] [ m ] conv_img = list ( ) for i in range ( img . shape [ 2 ] - end ) : for j in range ( img . shape [ 3 ] - end ) : temp_img_view = temp_img [ i : i + self . kernel_size , j : j + self . kernel_size ] conv_img . append ( torch . sum ( torch . mul ( temp_img_view , self . kernel ) ) ) conv_img = torch . stack ( conv_img ) conv_img = torch . reshape ( conv_img , output_dim ) conv_img = torch . unsqueeze ( conv_img , 0 ) conv_imgs [ l ] = conv_img return conv_imgs
Backward pass
(leave the function commented out,
so that autograd will be used.
Backward pass ( leave the function commented out so that autograd will be used .
# def backward(self):
 """
        Backward pass 
        (leave the function commented out,
        so that autograd will be used.
        No action needed here.)
        :return:
        """
None
self
X