apple / apple/coremltools

Gru implementation version in CoreML?

Open
#1,124 1 comment 0 reactions 1 assignee Claimed by @jakesabathia2 View on GitHub
LSTM/RNN PyTorch (traced) question
Dominant language
Python
Stars
5.4k
Forks
850
Avg merge
4d 5h
Merged PRs (30d)
10

Description

## ❓Question

We were trying to convert an rnn model from torch to coreml, which was using the [uisrnn](https://github.com/google/uis-rnn) package. Though the conversion went through fine, we found an issue which rendered the conversion stale. It was the difference in the implementation of the GRU layer in CoreML and Torch, especially in the way biases are applied. In torch the formula used for the output gate is:
**n = tanh(W_{in} x + b_{in} + r * (W_{hn} h + b_{hn}))** [(Reference)](https://pytorch.org/docs/stable/generated/torch.nn.GRUCell.html)
However in CoreML the output gate formula seems to be:
**n = tanh(W_{in} x + r * (W_{hn} h) + b_{n})**, where b(n) = b_{hn} + b_{in}

Unfortunately we did not find any flexibility in Torch or CoreML for making the formula consistent.

However Keras, seemed to have this flexibility. We have the 'reset_after' flag in the Keras GRU layer, with which we can use either formulae. A separate issue has already been raised for the inability of coremltools to handle 'reset_after=False' setting, [link]( https://github.com/apple/coremltools/issues/1123).

The question is when we do a Keras to coreml conversion, which version of the GRU implementation does coreml follow? Will it be consistent with Keras (assuming reset_after=True).

We have already seen that PyTorch and coreml are not consistent in the GRU formula. If Keras to coreml also have this limitation, then is there any other alternative left for porting a GRU model to coreml?

## System Information
- coremltools: 4.1
- Tensorflow: 2.3.1
- Keras: 2.2.4
- Torch: 1.7.1

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.