I am trying to execute the backward pass for a convolution network with layers (2 layer conv, relu, max pooling and linear, then softmax loss calculation). I have a hard time to retrieve x, y and probs at backward pass, the code does not recognize them as attributes. Please fix my code and show codes and output. Thank you

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I am trying to execute the backward pass for a convolution network with layers (2 layer conv, relu, max pooling and linear, then softmax loss calculation). I have a hard time to retrieve x, y and probs at backward pass, the code does not recognize them as attributes. Please fix my code and show codes and output. Thank you

 

from .softmax_ce import SoftmaxCrossEntropy
from .relu import ReLU
from .max_pool import MaxPooling
from .convolution import Conv2D
from .linear import Linear


class ConvNet:
   """
   Max Pooling of input
   """
   def __init__(self, modules, criterion):
       self.modules = []
       for m in modules:
           if m['type'] == 'Conv2D':
               self.modules.append(
                   Conv2D(m['in_channels'],
                          m['out_channels'],
                          m['kernel_size'],
                          m['stride'],
                          m['padding'])
               )
           elif m['type'] == 'ReLU':
               self.modules.append(
                   ReLU()
               )
           elif m['type'] == 'MaxPooling':
               self.modules.append(
                   MaxPooling(m['kernel_size'],
                              m['stride'])
               )
           elif m['type'] == 'Linear':
               self.modules.append(
                   Linear(m['in_dim'],
                          m['out_dim'])
               )
       if criterion['type'] == 'SoftmaxCrossEntropy':
           self.criterion = SoftmaxCrossEntropy()
       else:
           raise ValueError("Wrong Criterion Passed")

   def forward(self, x, y):
       """
       The forward pass of the model
       :param x: input data: (N, C, H, W)
       :param y: input label: (N, )
       :return:
         probs: the probabilities of all classes: (N, num_classes)
         loss: the cross entropy loss
       """
       probs = None
       loss = None

       for i,m in enumerate(self.modules):
           
           if i==0:
               x=m.forward(x)
           else:
               x=m.forward(x)
       loss=self.criterion.forward(x,y)
       probs=x
      
       return probs, loss

   def backward(self):
       """
       The backward pass of the model
       :return: nothing but dx, dw, and db of all modules are updated
       """
      
       for i, m in enumerate(self.modules):
           
           if i==0:
               probs, loss = self.criterion.forward(x,y)
               dx = self.criterion.backward(probs,y)
           if i ==1:
               dx, dw, db = self.modules[-1-i].backward(dx)
           else:
             dx, dw, db = self.modules[-1-i].backward(dx)

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Network Transmission Cabling
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education