Last active
March 3, 2022 18:55
-
-
Save vihar/22e1cd25e57641794fdb6eaaf90fbc45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.nn as nn | |
import torch.nn.functional as F | |
class Net(nn.Module): | |
def __init__(self, weight): | |
super(Net, self).__init__() | |
# initializes the weights of the convolutional layer to be the weights of the 4 defined filters | |
k_height, k_width = weight.shape[2:] | |
# assumes there are 4 grayscale filters | |
self.conv = nn.Conv2d(1, 4, kernel_size=(k_height, k_width), bias=False) | |
# initializes the weights of the convolutional layer | |
self.conv.weight = torch.nn.Parameter(weight) | |
# define a pooling layer | |
self.pool = nn.MaxPool2d(2, 2) | |
def forward(self, x): | |
# calculates the output of a convolutional layer | |
# pre- and post-activation | |
conv_x = self.conv(x) | |
activated_x = F.relu(conv_x) | |
# applies pooling layer | |
pooled_x = self.pool(activated_x) | |
# returns all layers | |
return conv_x, activated_x, pooled_x | |
# instantiate the model and set the weights | |
weight = torch.from_numpy(filters).unsqueeze(1).type(torch.FloatTensor) | |
model = Net(weight) | |
# print out the layer in the network | |
print(model) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From the above code, when implemented
weight = torch.from_numpy(filters).unsqueeze(1).type(torch.FloatTensor) from the following code:
from warnings import filters
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
class Net(nn.Module):
def init(self, weight): #Declaring a constructor to initialize the class variables
super(Net, self).init()
Initializes the weights of the convolutional layer to be the weights of the 4 defined filters
k_height, k_width = weight.shape[2:]
Assumes there are 4 grayscale filters; We declare the CNN layer here. Size of the kernel equals size of the filter
Usually the Kernels are smaller in size
self.conv = nn.Conv2d(1, 4, kernel_size=(k_height, k_width), bias=False)
self.conv.weight = torch.nn.Parameter(weight)
def forward(self, x):
Calculates the output of a convolutional layer pre- and post-activation
conv_x = self.conv(x)
activated_x = fn.relu(conv_x)
Returns both layers
return conv_x, activated_x
Instantiate the model and set the weights
weight = torch.from_numpy(filters).unsqueeze(1).type(torch.FloatTensor)
model = Net(weight)
Print out the layer in the network
print(model)
ERROR Displayed is as follows:
TypeError Traceback (most recent call last)
in ()
21 return conv_x, activated_x
22 # Instantiate the model and set the weights
---> 23 weight = torch.from_numpy(filters).unsqueeze(1).type(torch.float64)
24 model = Net(weight)
25 # Print out the layer in the network
TypeError: expected np.ndarray (got list)
Please, where am i getting it wrong and what should i do to correct the error. Thank you very much