How to correct name weight not define error

Question:

I am learning python by printing the output line by line from this tutorial https://towardsdatascience.com/convolution-neural-network-for-image-processing-using-keras-dc3429056306 and find out what does each line do. At the last line of code I facing error weight not define but it seem the code is running fine without the need to define the weight as inside the tutorial link. What I did wrongly in the code and how to fix it?

 import numpy as np
 import torch
 import torch.nn as nn
 import torch.nn.functional as fn
 filter_vals = np.array([[-1, -1, 1, 2], [-1, -1, 1, 0], [-1, -1, 1, 1], [-1, -1, 1, 
 1]])
 print('Filter shape: ', filter_vals.shape)# Neural network with one convolutional layer 
 and four filters

 # Neural network with one convolutional layer and four filters
 class Net(nn.Module):
 def __init__(self, weight): super(Net, self).__init__()
 k_height, k_width = weight.shape[2:]
Asked By: CKT

||

Answers:

The error is due to an indentation issue. The last line needs to be executed inside the constructor init for it to recognize the weight argument. The code should look like this:

 import numpy as np
 import torch
 import torch.nn as nn
 import torch.nn.functional as fn
 
 filter_vals = np.array([[-1, -1, 1, 2], 
                         [-1, -1, 1, 0], 
                         [-1, -1, 1, 1], 
                         [-1, -1, 1, 1]])

 print('Filter shape: ', filter_vals.shape) # Neural network with one convolutional layer and four filters

 # Neural network with one convolutional layer and four filters
 class Net(nn.Module):
     def __init__(self, weight): 
         super(Net, self).__init__()
         k_height, k_width = weight.shape[2:]
Answered By: AVManerikar