qkeras
qkeras copied to clipboard
QGRU Error when compiling with input features not the same as units
Building a simple GRU model using Keras:
gru = Sequential(GRU(16, input_shape=(2,4)))
gru.compile(loss='mse', optimizer='adam')
gru.summary()
Produces output:
Model: "sequential_332"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
gru_3 (GRU) (None, 16) 1056
=================================================================
Total params: 1,056
Trainable params: 1,056
Non-trainable params: 0
Trying to build same model with QGRU
layer:
gru = Sequential(QGRU(16, input_shape=(2,4)))
gru.compile(loss='mse', optimizer='adam')
gru.summary()
Produces the following error:
ValueError: in user code:
/lib/python3.7/site-packages/qkeras/qrecurrent.py:1304 call *
inputs, mask=mask, training=training, initial_state=initial_state)
/lib/python3.7/site-packages/qkeras/qrecurrent.py:1129 call *
recurrent_z = K.dot(h_tm1_z, quantized_recurrent[:, :self.units])
/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py:201 wrapper **
return target(*args, **kwargs)
/lib/python3.7/site-packages/tensorflow/python/keras/backend.py:1898 dot
out = math_ops.matmul(x, y)
/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:3315 matmul
a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py:5550 mat_mul
name=name)
/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:750 _apply_op_helper
attrs=attr_protos, op_def=op_def)
/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py:592 _create_op_internal
compute_device)
/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:3536 _create_op_internal
op_def=op_def)
/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:2016 __init__
control_input_ops, op_def)
/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:1856 _create_c_op
raise ValueError(str(e))
ValueError: Dimensions must be equal, but are 16 and 4 for '{{node qgru_13/qgru_cell_13/MatMul_3}} = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false](qgru_13/zeros, qgru_13/qgru_cell_13/strided_slice_6)' with input shapes: [?,16], [4,16].
Setting units
equal to input features produces no error. The issue is only present in QGRU
. Other recurrent layers QLSTM
or QSimpleRNN
do not have this issue.
Facing similar error while using QGRU
~~Still happens in the qkeras 0.9.0~~
ValueError: Dimensions must be equal, but are 32 and 2 for '{{node qgru/qgru_cell/MatMul_3}} = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false](qgru/zeros, qgru/qgru_cell/strided_slice_6)' with input shapes: [?,32], [2,32].
Call arguments received:
• inputs=tf.Tensor(shape=(None, 2), dtype=float32)
• states=('tf.Tensor(shape=(None, 32), dtype=float32)',)
• training=None
Call arguments received:
• inputs=tf.Tensor(shape=(None, 25, 2), dtype=float32)
• mask=None
• training=None
• initial_state=None`
EDIT: I fixed by specifying all the quantizers inside QGRU
Example:
QGRU(32, kernel_quantizer="binary(alpha=1)", bias_quantizer="binary(alpha=1)", state_quantizer="binary(alpha=1)", recurrent_quantizer="binary(alpha=1)")