Regarding weight and bias initialization for sequential model from the text file

username: Ganga
cooked: <p>i have reference model which is evaluated using pretrained model,i have weights,bias and input to the function…to check individually i took single conv2d has a sequential model. and i have initialized the input,weights and bias from the text file(values obtained from the refernce model)</p>
<p>but the problem is output from the Conv2d is not matching with the refernce model output (which is also a Conv2d with all same attributes)<br>
here is my code…<br>
“”"import torch<br>
import torch.nn as nn<br>
import csv<br>
import torch<br>
import pandas as pd<br>
import math<br>
import numpy</p>
<p>class PrintLayer(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(PrintLayer, self).<strong>init</strong>()<br>
selfconv = nn.Sequential(<br>
nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2)<br>
)</p>
<pre><code>    a_file1 = open("2nd layer weight.txt","r")
    list_of_lists1 = []
    for line in a_file1:
      stripped_line1 = line.strip()
      line_list1 = stripped_line1.split()
      list_of_lists1.extend(line_list1)
    a_file1.close()

    b = torch.from_numpy(numpy.array(list_of_lists1,dtype= 'float32'))
    w = torch.tensor(b)
    ten = w.reshape(256,96,5,5)
    selfconv[0].weight.data = ten

    a_file12 = open("bias.txt","r")
    list_of_lists12 = []
    for line in a_file12:
      stripped_line12 = line.strip()
      line_list12 = stripped_line12.split()
      list_of_lists12.extend(line_list12)
    a_file12.close()

    c = torch.from_numpy(numpy.array(list_of_lists12,dtype= 'float32'))
    d = torch.tensor(c)
    ten1 = d.reshape(256)
    print(ten1.size())
    selfconv[0].bias.data = ten1
    

    #print(selfconv[0].weight)
    #print(selfconv[0].weight.data)
    #m.weight.data = m.weight.data.fill(m.weight.data)
    
    
    
def forward(self, x):
    # Do your print / debug stuff here
    #print("x is",x)
    return x
</code></pre>
<p>selfconv = nn.Sequential(<br>
nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2),<br>
PrintLayer()<br>
)</p>
<p>a_file = open(“input_27_27_96.txt”,“r”)<br>
list_of_lists = []<br>
for line in a_file:<br>
stripped_line = line.strip()<br>
line_list = stripped_line.split()<br>
list_of_lists.extend(line_list)<br>
a_file.close()<br>
<span class="hashtag">#print</span>(len(list_of_lists))<br>
b = torch.from_numpy(numpy.array(list_of_lists,dtype= ‘float32’))<br>
x = torch.tensor(b)<br>
x = x.reshape(1,96,27,27)<br>
x =x.double()<br>
<span class="hashtag">#print</span>(x.size())<br>
<span class="hashtag">#print</span>(x.size())<br>
state_dict = selfconv.state_dict()<br>
output = selfconv(x.float())<br>
<span class="hashtag">#print</span>(output.size())<br>
<span class="hashtag">#print</span>(output)<br>
<span class="hashtag">#print</span>(selfconv.state_dict())<br>
<span class="hashtag">#print</span>(list(selfconv.named_parameters()))<br>
print((selfconv[0].weight == state_dict[‘0.weight’]).all())<br>
print((selfconv[0].bias == state_dict[‘0.bias’]).all())<br>
print(list(selfconv.named_parameters()))"""</p>
<p><a class="mention" href="/u/ptrblck">@ptrblck</a> can you please provide the solution for it…i am very new to python and pytorch</p>