You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Firstly, I want to thank all the ones doing the good work on the project. Anyway, I am a newbee, I might not understand the ONNX LSTM concept rightly.
Solved:
That was the issue: I did not read the documents: LSTM Operator
There it is clearly spelled out, the order is Riofc and not Rifco, as with Keras.
I assumed, that the weights of the keras LSTM building block and the weights of the ONNX are equal, but that seem to be wrong. What do I wrong, or what is the wrong assumption?
Please have a look at the following code:
import os
import tensorflow as tf
import tf2onnx
import numpy as np
import numpy.testing as npt
import array
##################### Keras Model #####################
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(20, input_shape=(1,10), batch_size=1))
model.compile(loss='mean_absolute_error', optimizer='adam',
metrics=['mean_squared_error'])
nweights = []
np.random.seed(0)
for W in model.get_weights():
nweights.append((np.random.randint(20, size=W.shape)-10)/10)
model.set_weights(nweights)
input_signature = [tf.TensorSpec(model.input_shape, tf.float32, name='x')]
onnx_model, _ = tf2onnx.convert.from_keras(model, input_signature, opset=16)
weights_keras = model.layers[0].get_weights()
initializers = onnx_model.graph.initializer
weights_onnx = {}
for val in initializers:
if val.data_type == 1:
print(val.name, val.dims)
weights_onnx[val.name] = np.array(array.array('f', val.raw_data)).reshape(val.dims)
layer_0_reccurent_keras = weights_keras[1]
layer_0_recurrent_onnx = weights_onnx["R0__28"][0,:,:].T
layer_0_reccurent_keras.shape == layer_0_recurrent_onnx.shape
try:
npt.assert_array_equal(layer_0_reccurent_keras, layer_0_recurrent_onnx)
except AssertionError as ae:
print("AssertionError:\n", ae)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Firstly, I want to thank all the ones doing the good work on the project. Anyway, I am a newbee, I might not understand the ONNX LSTM concept rightly.
Solved:
That was the issue: I did not read the documents: LSTM Operator
There it is clearly spelled out, the order is Riofc and not Rifco, as with Keras.
I assumed, that the weights of the keras LSTM building block and the weights of the ONNX are equal, but that seem to be wrong. What do I wrong, or what is the wrong assumption?
Please have a look at the following code:
The output shows the results:
Thanks for reading the stuff.
Beta Was this translation helpful? Give feedback.
All reactions