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

**URL:** <https://discuss.pytorch.org/t/regarding-weight-and-bias-initialization-for-sequential-model-from-the-text-file/88069>\
**Category:** Uncategorized\
**Created:** [July 6, 2020, 5:30am UTC](https://discuss.pytorch.org/t/regarding-weight-and-bias-initialization-for-sequential-model-from-the-text-file/88069 "2020-07-06T05:30:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ganga](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ganga/32/28549_2.png) [@Ganga](https://discuss.pytorch.org/u/Ganga)\
**Post date:** [July 6, 2020, 5:30am UTC](https://discuss.pytorch.org/t/regarding-weight-and-bias-initialization-for-sequential-model-from-the-text-file/88069/1 "2020-07-06T05:30:11Z")

</div>

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)

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)  
here is my code…  
“”"import torch  
import torch.nn as nn  
import csv  
import torch  
import pandas as pd  
import math  
import numpy

class PrintLayer(nn.Module):  
def **init** (self):  
super(PrintLayer, self). **init** ()  
selfconv = nn.Sequential(  
nn.Conv2d(96, 256, kernel\_size=5, stride=1, padding=2)  
)

```
    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

```

selfconv = nn.Sequential(  
nn.Conv2d(96, 256, kernel\_size=5, stride=1, padding=2),  
PrintLayer()  
)

a\_file = open(“input\_27\_27\_96.txt”,“r”)  
list\_of\_lists = []  
for line in a\_file:  
stripped\_line = line.strip()  
line\_list = stripped\_line.split()  
list\_of\_lists.extend(line\_list)  
a\_file.close()  
#print(len(list\_of\_lists))  
b = torch.from\_numpy(numpy.array(list\_of\_lists,dtype= ‘float32’))  
x = torch.tensor(b)  
x = x.reshape(1,96,27,27)  
x =x.double()  
#print(x.size())  
#print(x.size())  
state\_dict = selfconv.state\_dict()  
output = selfconv(x.float())  
#print(output.size())  
#print(output)  
#print(selfconv.state\_dict())  
#print(list(selfconv.named\_parameters()))  
print((selfconv[0].weight == state\_dict[‘0.weight’]).all())  
print((selfconv[0].bias == state\_dict[‘0.bias’]).all())  
print(list(selfconv.named\_parameters()))"""

@ptrblck can you please provide the solution for it…i am very new to python and pytorch
