Home > Enterprise >  How to correctly define weights and biases in a PyTorch neural network to get output of correct shap
How to correctly define weights and biases in a PyTorch neural network to get output of correct shap

Time:08-11

I'm attempting to pass some data of shape [70,1], [70,1,1], and [70,1] into a neural network of linear layers for which I have assigned weights and biases. I am expecting an output of shape [70,1], but I am constantly getting the following error:

RuntimeError                              Traceback (most recent call last)
Input In [78], in <cell line: 1>()
----> 1 output = net(s, h, ep)

File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py:1130, in Module._call_impl(self, *input, **kwargs)
   1126 # If we don't have any hooks, we want to skip the rest of the logic in
   1127 # this function, and just call forward.
   1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1129         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130     return forward_call(*input, **kwargs)
   1131 # Do not call functions when jit is used
   1132 full_backward_hooks, non_full_backward_hooks = [], []

Input In [68], in NeuralNetHardeningModel.forward(self, s, h, ep)
    101     y = torch.stack((s_eval, h_eval, ep_eval), 1)
    103     print(y.shape, 'y')
--> 105     y1 = self.nn(y)
    107     print(y1.shape, 'y1')
    109 return y1

File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py:1130, in Module._call_impl(self, *input, **kwargs)
   1126 # If we don't have any hooks, we want to skip the rest of the logic in
   1127 # this function, and just call forward.
   1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1129         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130     return forward_call(*input, **kwargs)
   1131 # Do not call functions when jit is used
   1132 full_backward_hooks, non_full_backward_hooks = [], []

File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/container.py:139, in Sequential.forward(self, input)
    137 def forward(self, input):
    138     for module in self:
--> 139         input = module(input)
    140     return input

File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/module.py:1130, in Module._call_impl(self, *input, **kwargs)
   1126 # If we don't have any hooks, we want to skip the rest of the logic in
   1127 # this function, and just call forward.
   1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1129         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130     return forward_call(*input, **kwargs)
   1131 # Do not call functions when jit is used
   1132 full_backward_hooks, non_full_backward_hooks = [], []

File ~/opt/anaconda3/lib/python3.9/site-packages/torch/nn/modules/linear.py:114, in Linear.forward(self, input)
    113 def forward(self, input: Tensor) -> Tensor:
--> 114     return F.linear(input, self.weight, self.bias)

RuntimeError: mat2 must be a matrix, got 1-D tensor

After some inspection, I could not figure out how to solve the error, but I suspect it might have something to do with the way I assign the weights to each layer in the neural net.

Here is the code that I use to define my PyTorch neural network:

# define the NN
def __init__(self, weight, bias, weight_last, bias_last):
        
    # weight.shape = [3,3,3]
    # bias.shape = [3,3]
    # weight_last = [3], last layer
    # bias_last = [1], last layer
    
    super(NeuralNetHardeningModel, self).__init__()
        
        
    self.weight = weight
    self.bias = bias
        
    self.weight_last = weight_last
    self.bias_last = bias_last
        
        
    self.nn = nn.Sequential(
                
       nn.Linear(3, 3),
       nn.ReLU(),
                
       nn.Linear(3, 3),
       nn.ReLU(),
               
       nn.Linear(3, 3),
       nn.ReLU(),
                
       nn.Linear(3, 1)
            )
        
    if len(weight.shape) == 3:

       with torch.no_grad():

           self.nn[0].weight = nn.Parameter(weight[0])
           self.nn[0].bias = nn.Parameter(bias[0])

           self.nn[2].weight = nn.Parameter(weight[1])
           self.nn[2].bias = nn.Parameter(bias[1])

           self.nn[4].weight = nn.Parameter(weight[2])
           self.nn[4].bias = nn.Parameter(bias[2])

           self.nn[6].weight = nn.Parameter(weight_last)
           self.nn[6].bias = nn.Parameter(bias_last)

Here is the code that I use to define my forward pass for my PyTorch neural network:

# forward method for the NN
def forward(self, a, b, c):

  for i in range(a.shape[0]): 

     a_eval = torch.flatten(a)  # [70,1]
     b_eval = torch.flatten(b)  # [70,1,1]
     c_eval = torch.flatten(c) # [70,1]
                
     y = torch.stack((a_eval, b_eval, c_eval), 1)
                
     y1 = self.nn(y)
                        
  return y1

CodePudding user response:

The shape of weight_last must be [1,3] instead of just [3] to prevent the matrix multiplication error.

  • Related