Open yuvalshachaf opened 4 years ago
_" Drilling down into the error leads to a specific line in the Attention Class: weighted_input = x * K.expanddims(a) "
I don't see why it is a converter issue here?
Keras wize has no issues whatsoever with this attention class. Back to onnx. When we bypassed this line of code the model converted successfuly. We think the error we are getting is something to do with the question mark in the second dim i.e axis 1. And as a result the expand dims fails. The shape of "a" in that line is (none,200) "x" shape is (none,none,128) Thanks
Hi there, I have been trying to convert a simple Keras BiLSTM (or LSTM) with Attention model to ONNX.
It keeps failing during onnx model save.
The error message I am getting is TypeError: object of type 'NoneType' has no len()
Drilling down into the error leads to a specific line in the Attention Class: weighted_input = x * K.expand_dims(a)
I am using the latest onnx however with TF 1.4.0 and Keras 2.1.5
code custom layer is:
Attention layer code taken from: https://gist.github.com/cbaziotis/6428df359af27d58078ca5ed9792bd6d
AttentionWithContext layer code taken from https://gist.github.com/cbaziotis/7ef97ccf71cbc14366835198c09809d2
import numpy as np from keras import backend as K, initializers, regularizers, constraints
from keras.layers import Layer from sklearn import metrics import tensorflow as tf from sentiment_parallel.configuration_manager.config_manager import config
def dot_product(x, kernel): """ Wrapper for dot product operation, in order to be compatible with both Theano and Tensorflow Args: x (): input kernel (): weights Returns: """ if K.backend() == 'tensorflow':
todo: check that this is correct
class Attention(Layer): def init(self, W_regularizer=None, b_regularizer=None, W_constraint=None, b_constraint=None, bias=True, return_attention=False, **kwargs):
def create_dummy_bilstm_model(): import tensorflow as tf from keras.optimizers import Adam from keras.activations import relu, sigmoid from keras.layers import Embedding, Dense, Input, Dropout, concatenate,, Bidirectional, LSTM from keras.models import Model
def save_keras_model_as_onnx(keras_model): import onnxmltools onnx_model = onnxmltools.convert_keras(keras_model) onnxmltools.utils.save_model(onnx_model, 'yuval.onnx') print("model saved as onnx")
if name == 'main': model = create_dummy_bilstm_model() save_keras_model_as_onnx(model)