gmalivenko / pytorch2keras

PyTorch to Keras model convertor
https://pytorch2keras.readthedocs.io/en/latest/
MIT License
858 stars 143 forks source link

Dense layer conversion bug with multiple inputs #52

Closed calclavia closed 4 years ago

calclavia commented 5 years ago

Describe the bug Multiple linear layers with multiple inputs do not work. When the input shapes are identical, it works (e.g both inputs expect 5 units or both expect 10 units, but when one is 5 and the other is 10 it does not work).

To Reproduce

class LayerTest(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(10, 10)
        self.fc2 = nn.Linear(5, 10)

    def forward(self, x, y):
        x = self.fc1(x)
        y = self.fc2(y)
        return x + y

model = LayerTest().eval()
input_x = torch.FloatTensor(np.random.uniform(0, 1, (1, 10)))
input_y = torch.FloatTensor(np.random.uniform(0, 1, (1, 5)))
output = model(input_x, input_y)
k_model = pytorch_to_keras(model, [input_x, input_y], [(10,), (5,)], verbose=True)

Expected behavior This should convert properly.

Logs

Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/Cellar/python/3.6.5_1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/henry/code/altum/model-trainer/export/export.py", line 54, in <module>
    k_model = pytorch_to_keras(model, [input_x, input_y], [(10,), (5,)], verbose=True)
  File "/usr/local/lib/python3.6/site-packages/pytorch2keras/converter.py", line 332, in pytorch_to_keras
    names
  File "/usr/local/lib/python3.6/site-packages/pytorch2keras/linear_layers.py", line 49, in convert_gemm
    layers[scope_name] = dense(layers[inputs[0]])
  File "/usr/local/lib/python3.6/site-packages/keras/engine/base_layer.py", line 436, in __call__
    self.set_weights(self._initial_weights)
  File "/usr/local/lib/python3.6/site-packages/keras/engine/base_layer.py", line 1057, in set_weights
    'provided weight shape ' + str(w.shape))
ValueError: Layer weight shape (5, 10) not compatible with provided weight shape (10, 10)
gmalivenko commented 5 years ago

Hello @calclavia. I reproduced the bug.

gmalivenko commented 5 years ago

Upgrade your converter version to the latest or pull the master. I've fixed problems related to inputs (they were matched wrong).