ValueError: Fan in and fan out can not be computed for tensor with fewer than 2 dimensions

  • Basically I want to provide my bias and weight values from the file

  • I am using Xavier iniialization to initialize the weights and bias

  • I am facing error in bias initialization
    have a look into my code

import torch.nn as nn
import csv
import numpy
import numpy as np
class PrintLayer(nn.Module):
    def __init__(self):
        super(PrintLayer, self).__init__()
        m = nn.Linear(4096, 1000)
        
    
    def forward(self, x):
        # Do your print / debug stuff here
        #print("x is",x)
        return x

def init_weights(m):
      if type(m) == nn.Linear:
        print('hi')
        torch.nn.init.xavier_uniform(m.weight)
        
        torch.nn.init.xavier_uniform(m.bias)

        #for weight
        a_file1 = open("8bit_lastlayer_weight_round.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(1000,4096)
        m.weight.data = ten

        #for bias
        a_file12 = open("8bit_lastlayer_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(1,1000)
        print(ten1.get_shape())
        m.bias.data = ten1
       
        #print("actual weight is",m.weight)
        #print(m.bias)
      
model = nn.Sequential(
        nn.Linear(4096, 1000),
        PrintLayer(),
         # Add Print layer for debug
        )
model.apply(init_weights)

#for input
a_file = open("8bit_lastlayer_input.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()
b = torch.from_numpy(numpy.array(list_of_lists,dtype= 'float32'))
x = torch.tensor(b)
x = x.reshape(1,4096)
output = model(x)

values,indices = torch.max(output,1)
print(values,indices)
np.set_printoptions(threshold=50000000,formatter={'float_kind':'{:f}'.format})
output = output.detach().numpy()
#print(output)
file = open("8bit_final_layer_output.txt","w")
file.write(str(output))
file.close()

#print(model)
#for weight and bias
#print(list(model.named_parameters()))
#print(model.state_dict())```
1 Like

Xavier uniform isn’t for the bias… A typical thing to use might be 0.

3 Likes

can you please show us the code snippet

torch.nn.init.zeros_(m.bias)