Size mismatch error in converting TensorFlow model to Pytorch
I´m trying to convert a TensorFlow model to Pytorch but got stuck in this error. Can anyone help me?

#getting weights and biases from tensorflow model
weights, biases = model.layers[0].get_weights()
#[1] is the dropout layer
weights2, biases2 = model.layers[2].get_weights()

#initializing pytorch
class TwoLayerNet(torch.nn.Module):
def __init__(self, weights, biases, weights2, biases2):

    super(TwoLayerNet, self).__init__()
    #created the model in the same dimensions as tensorflow´s model
    self.linear1 = torch.nn.Linear(9, 2048)
    self.hidden1 = nn.Dropout(0.2)
    self.linear2 = torch.nn.Linear(2048,5)

    weights = torch.from_numpy(weights)
    biases = torch.from_numpy(biases)
    weights2 = torch.from_numpy(weights2)
    biases2 = torch.from_numpy(biases2)

    self.linear1.weight = torch.nn.Parameter(weights)
    self.linear1.bias = torch.nn.Parameter(biases)
    self.linear2.weight.data = weights2
    self.linear2.bias.data = biases2
    #in this print the dimensions are ok (Linear(in_features=9, out_features=2048, bias=True))
    print(self.linear1)

def forward(self, x):
  print(self.linear1)
  x = self.linear1(x)
  x = self.hidden1(x)
  x = self.linear2(x)
  return x

model_pytorch = TwoLayerNet(weights, biases, weights2, biases2)

model_pytorch.eval()
exemplo_input_torch = torch.from_numpy(exemplo_input)
exemplo_input_torch = exemplo_input_torch.float()
print(exemplo_input_torch)
result = model_pytorch(exemplo_input_torch)


The error is:

RuntimeError: size mismatch, m1: [1 x 9], m2: [2048 x 9] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:41

You need to transpose the weights and biases:

weights = torch.from_numpy(weights).T
biases = torch.from_numpy(biases).T
weights2 = torch.from_numpy(weights2).T
biases2 = torch.from_numpy(biases2).T

