Flock1
(Flock Anizak)
October 24, 2021, 6:11am
#1
Hello,

I have some initial weights that I want to initialize a CNN layer with. The weights are of the shape `(3,8,12)`

. I defined a CNN layer as

`conv1 = nn.Conv2d(3,8,kernel_size=(3,3), stride = 2)`

I just guessed this initialization and that’s why I am getting the following error:

```
RuntimeError: Given weight of size [1, 3, 8, 12], expected bias to be 1-dimensional with 1 elements, but got bias of size [8] instead
```

My input is of size `(3,244,244)`

. Kindly let me know how can I initialize the CNN layer so that it accepts the weights.

Hi,
Can you check

Your model seems to work fine with bias=False and bias=True:
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5,bias=False)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
out = self.conv1(x)
out = F.relu(out)
out = F.max_pool2d(out, 2)
out = F.rel…
Your input contains 4 channels, while the first conv layer expects an input with 3 channels.
If you are dealing with RGB images, the 4th channel might be the alpha channel, which could just be removed.
If you are using a custom DataLoader, you could probably just use:
def __getitem__(self, index):
img = Image.open(self.paths[index]).convert('RGB')
...
# Alternatively remove the alpha channel from the tensor
img = Image.open(self.paths[index])
x = TF.to_tensor(img)
x = …
if the above doesn’t work
can you check the links below

https://www.google.com/search?q=RuntimeError%3A+Given+weight+of+size+[1%2C+3%2C+8%2C+12]%2C+expected+bias+to+be+1-dimensional+with+1+elements%2C+but+got+bias+of+size+[8]+instead&oq=RuntimeError%3A+Given+weight+of+size+[1%2C+3%2C+8%2C+12]%2C+expected+bias+to+be+1-dimensional+with+1+elements%2C+but+got+bias+of+size+[8]+instead&aqs=chrome..69i57j69i58&sourceid=chrome&ie=UTF-8

With best regards,
Programmer-RD-AI

my3bikaht
(Sergey)
October 24, 2021, 9:45am
#3

Flock1:

conv1 = nn.Conv2d(3,8,kernel_size=(3,3), stride = 2)

Not sure where 12 came from. Your conv1 layer has 3 input channels, 8 output channels, kernel size of 3x3 and stride 2. Thus, you have 8 kernels, each has 3x(3x3) weights and 1 bias

Use conv1.bias.shape and conv1.weight.shape after initialization to get correct dimensions.