Initialize weights to CNN

Flock1: <p>Hello,</p>
<p>I have some initial weights that I want to initialize a CNN layer with. The weights are of the shape <code>(3,8,12)</code>. I defined a CNN layer as</p>
<p><code>conv1 = nn.Conv2d(3,8,kernel_size=(3,3), stride = 2)</code></p>
<p>I just guessed this initialization and that’s why I am getting the following error:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>My input is of size <code>(3,244,244)</code>. Kindly let me know how can I initialize the CNN layer so that it accepts the weights.</p>

Programmer-RD-AI: <p>Hi,<br>
Can you check</p><aside class="quote quote-modified" data-post="2" data-topic="65982">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/runtimeerror-given-weight-of-size-3-1-5-5-expected-bias-to-be-1-dimensional-with-3-elements-but-got-bias-of-size-6-instead/65982/2">RuntimeError: Given weight of size 3 1 5 5, expected bias to be 1-dimensional with 3 elements, but got bias of size [6] instead</a> 
  </div>
  <blockquote>
    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…
  </blockquote>
</aside>

<aside class="quote quote-modified" data-post="8" data-topic="12765">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/runtimeerror-given-groups-1-weight-64-3-3-3-so-expected-input-16-64-256-256-to-have-3-channels-but-got-64-channels-instead/12765/8">RuntimeError: Given groups=1, weight[64, 3, 3, 3], so expected input[16, 64, 256, 256] to have 3 channels, but got 64 channels instead</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    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 = …
  </blockquote>
</aside>

<p>if the above doesn’t work<br>
can you check the links below</p>
<p><a href="https://www.google.com/search?q=RuntimeError%3A+Given+weight+of+size+%5B1%2C+3%2C+8%2C+12%5D%2C+expected+bias+to+be+1-dimensional+with+1+elements%2C+but+got+bias+of+size+%5B8%5D+instead&amp;oq=RuntimeError%3A+Given+weight+of+size+%5B1%2C+3%2C+8%2C+12%5D%2C+expected+bias+to+be+1-dimensional+with+1+elements%2C+but+got+bias+of+size+%5B8%5D+instead&amp;aqs=chrome..69i57j69i58&amp;sourceid=chrome&amp;ie=UTF-8" class="onebox" target="_blank" rel="noopener nofollow ugc">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&amp;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&amp;aqs=chrome..69i57j69i58&amp;sourceid=chrome&amp;ie=UTF-8</a></p>
<p>With best regards,<br>
Programmer-RD-AI</p>

my3bikaht: <aside class="quote no-group" data-username="Flock1" data-post="1" data-topic="134974">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/flock1/48/12220_2.png" class="avatar"> Flock1:</div>
<blockquote>
<p>conv1 = nn.Conv2d(3,8,kernel_size=(3,3), stride = 2)</p>
</blockquote>
</aside>
<p>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</p>
<p>Use conv1.bias.shape and conv1.weight.shape after initialization to get correct dimensions.</p>